Add NostrmarketService to dependency injection container

- Introduce NOSTRMARKET_SERVICE token in the DI container for better service management.
- Update market module to create and register NostrmarketService instance using the new DI pattern.
- Refactor components and composables to inject NostrmarketService via the DI container, enhancing modularity and testability.

These changes improve the architecture by ensuring consistent service injection and eliminating legacy singleton references, aligning with the overall dependency injection strategy.
This commit is contained in:
padreug 2025-09-07 01:31:24 +02:00
parent 31a9c0a9dc
commit 14d6bc6329
6 changed files with 26 additions and 36 deletions

View file

@ -1,7 +1,7 @@
import type { App } from 'vue'
import type { ModulePlugin } from '@/core/types'
import type { RouteRecordRaw } from 'vue-router'
import { container } from '@/core/di-container'
import { container, SERVICE_TOKENS } from '@/core/di-container'
import { eventBus } from '@/core/event-bus'
// Import components
@ -17,9 +17,8 @@ import ShoppingCart from './components/ShoppingCart.vue'
import { useMarket } from './composables/useMarket'
import { useMarketPreloader } from './composables/useMarketPreloader'
// Define service tokens
export const MARKET_SERVICE_TOKEN = Symbol('marketService')
export const NOSTRMARKET_SERVICE_TOKEN = Symbol('nostrmarketService')
// Import services
import { NostrmarketService } from './services/nostrmarketService'
export interface MarketModuleConfig {
defaultCurrency: string
@ -45,8 +44,9 @@ export const marketModule: ModulePlugin = {
throw new Error('Market module requires configuration')
}
// Import the singleton instance
const { nostrmarketService } = await import('./services/nostrmarketService')
// Create and register NostrmarketService instance
const nostrmarketService = new NostrmarketService()
container.provide(SERVICE_TOKENS.NOSTRMARKET_SERVICE, nostrmarketService)
// Initialize the service (will handle dependency injection)
await nostrmarketService.initialize({
@ -56,9 +56,6 @@ export const marketModule: ModulePlugin = {
console.warn('🛒 NostrmarketService initialization deferred:', error)
// Service will auto-initialize when dependencies are available
})
// Register the service
container.provide(NOSTRMARKET_SERVICE_TOKEN, nostrmarketService)
// Register global components
app.component('MarketSettings', MarketSettings)
@ -77,7 +74,7 @@ export const marketModule: ModulePlugin = {
console.log('🗑️ Uninstalling market module...')
// Clean up services
container.remove(NOSTRMARKET_SERVICE_TOKEN)
container.remove(SERVICE_TOKENS.NOSTRMARKET_SERVICE)
console.log('✅ Market module uninstalled')
},
@ -137,7 +134,7 @@ export const marketModule: ModulePlugin = {
},
services: {
nostrmarket: NOSTRMARKET_SERVICE_TOKEN
nostrmarket: SERVICE_TOKENS.NOSTRMARKET_SERVICE
}
}