Refactor services to extend BaseService for improved structure and dependency management

- Update AuthService and RelayHub to extend BaseService, introducing standardized initialization and metadata handling.
- Implement service-specific initialization methods in both services, enhancing error handling and logging.
- Modify NostrmarketService to inherit from BaseService, ensuring consistent dependency management and initialization.
- Refactor market module to dynamically import NostrmarketService, improving service registration and initialization flow.
- Enhance debug logging across services for better traceability during initialization and operation.
This commit is contained in:
padreug 2025-09-05 06:41:19 +02:00
parent 8d4c389f71
commit dc4da570a7
5 changed files with 151 additions and 50 deletions

View file

@ -9,8 +9,7 @@ import MarketSettings from './components/MarketSettings.vue'
import MerchantStore from './components/MerchantStore.vue'
import ShoppingCart from './components/ShoppingCart.vue'
// Import services
import { NostrmarketService } from './services/nostrmarketService'
// NostrmarketService will be dynamically imported in install()
// Store will be imported when needed
@ -46,8 +45,19 @@ export const marketModule: ModulePlugin = {
throw new Error('Market module requires configuration')
}
// Create and register services
const nostrmarketService = new NostrmarketService()
// Import the singleton instance
const { nostrmarketService } = await import('./services/nostrmarketService')
// Initialize the service (will handle dependency injection)
await nostrmarketService.initialize({
waitForDependencies: true,
maxRetries: 3
}).catch(error => {
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

View file

@ -1,6 +1,5 @@
import { finalizeEvent, type EventTemplate, nip04 } from 'nostr-tools'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import { auth } from '@/composables/useAuth'
import { BaseService } from '@/core/base/BaseService'
import type { Stall, Product, Order } from '@/stores/market'
export interface NostrmarketStall {
@ -67,13 +66,20 @@ export interface NostrmarketOrderStatus {
shipped?: boolean
}
export class NostrmarketService {
private get relayHub(): any {
const hub = injectService(SERVICE_TOKENS.RELAY_HUB)
if (!hub) {
throw new Error('RelayHub not available. Make sure base module is installed.')
}
return hub as any
export class NostrmarketService extends BaseService {
// Service metadata
protected readonly metadata = {
name: 'NostrmarketService',
version: '1.0.0',
dependencies: ['RelayHub', 'AuthService']
}
/**
* Service-specific initialization (called by BaseService)
*/
protected async onInitialize(): Promise<void> {
this.debug('NostrmarketService initialized')
// Service doesn't need special initialization
}
/**
@ -100,12 +106,12 @@ export class NostrmarketService {
}
private getAuth() {
if (!auth.isAuthenticated.value || !auth.currentUser.value?.prvkey) {
if (!this.authService?.isAuthenticated?.value || !this.authService?.user?.value?.prvkey) {
throw new Error('User not authenticated or private key not available')
}
const pubkey = auth.currentUser.value.pubkey
const prvkey = auth.currentUser.value.prvkey
const pubkey = this.authService.user.value.pubkey
const prvkey = this.authService.user.value.prvkey
if (!pubkey || !prvkey) {
throw new Error('Public key or private key is missing')