Migrate InvoiceService to dependency injection pattern

- Add INVOICE_SERVICE token to DI container
- Register InvoiceService in base module with proper lifecycle
- Update market store to use dependency injection instead of singleton
- Remove exported singleton from InvoiceService class
- Add comprehensive migration documentation with examples
- Maintain type safety with proper TypeScript interfaces

This migration eliminates the legacy singleton pattern and improves:
- Testability through service injection
- Modular architecture with clear boundaries
- Single source of truth for service instances
- Consistent dependency injection patterns

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
padreug 2025-09-07 01:10:55 +02:00
parent 6cb10a31db
commit 7a32085ee1
5 changed files with 383 additions and 6 deletions

View file

@ -134,6 +134,9 @@ export const SERVICE_TOKENS = {
// Events services
EVENTS_SERVICE: Symbol('eventsService'),
// Invoice services
INVOICE_SERVICE: Symbol('invoiceService'),
} as const
// Type-safe injection helpers

View file

@ -32,7 +32,7 @@ export interface PaymentStatus {
payment_hash: string
}
class InvoiceService {
export class InvoiceService {
private baseUrl: string
constructor() {
@ -186,6 +186,3 @@ class InvoiceService {
}
}
// Export singleton instance
export const invoiceService = new InvoiceService()

View file

@ -14,6 +14,10 @@ import { paymentService } from '@/core/services/PaymentService'
import { visibilityService } from '@/core/services/VisibilityService'
import { storageService } from '@/core/services/StorageService'
import { toastService } from '@/core/services/ToastService'
import { InvoiceService } from '@/core/services/invoiceService'
// Create service instances
const invoiceService = new InvoiceService()
/**
* Base Module Plugin
@ -44,6 +48,9 @@ export const baseModule: ModulePlugin = {
// Register toast service
container.provide(SERVICE_TOKENS.TOAST_SERVICE, toastService)
// Register invoice service
container.provide(SERVICE_TOKENS.INVOICE_SERVICE, invoiceService)
// Register PWA service
container.provide('pwaService', pwaService)
@ -92,6 +99,7 @@ export const baseModule: ModulePlugin = {
visibilityService,
storageService,
toastService,
invoiceService,
pwaService
},

View file

@ -1,11 +1,10 @@
import { defineStore } from 'pinia'
import { ref, computed, readonly, watch } from 'vue'
import { invoiceService } from '@/core/services/invoiceService'
import { paymentMonitor } from '../services/paymentMonitor'
import { nostrmarketService } from '../services/nostrmarketService'
import { useAuth } from '@/composables/useAuthService'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import type { LightningInvoice } from '@/core/services/invoiceService'
import type { LightningInvoice, InvoiceService } from '@/core/services/invoiceService'
import type {
@ -18,6 +17,7 @@ import type {
export const useMarketStore = defineStore('market', () => {
const auth = useAuth()
const storageService = injectService(SERVICE_TOKENS.STORAGE_SERVICE) as any
const invoiceService = injectService(SERVICE_TOKENS.INVOICE_SERVICE) as InvoiceService
// Core market state
const markets = ref<Market[]>([])
const stalls = ref<Stall[]>([])