From 099c16abc942c032f905b021183330ef91053cc7 Mon Sep 17 00:00:00 2001 From: padreug Date: Fri, 5 Sep 2025 15:23:03 +0200 Subject: [PATCH] Refactor PaymentService methods for clarity and consistency - Rename methods in PaymentService for improved readability: payInvoiceWithUserWallet to payWithWallet, openExternalLightningWallet to openExternalWallet, and handlePaymentWithFallback to handlePayment. - Update related composables (useTicketPurchase, useLightningPayment) to reflect method name changes, ensuring consistent usage across the application. - Modify RelayHub initialization process to set relay URLs before calling initialize, enhancing configuration clarity. - Remove legacy compatibility flags in RelayHub to streamline the codebase and improve maintainability. --- src/core/services/PaymentService.ts | 10 ++-- src/modules/base/index.ts | 3 +- src/modules/base/nostr/relay-hub.ts | 47 +++++++++---------- .../events/composables/useTicketPurchase.ts | 4 +- .../market/composables/useLightningPayment.ts | 6 +-- 5 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/core/services/PaymentService.ts b/src/core/services/PaymentService.ts index a3a0227..948c752 100644 --- a/src/core/services/PaymentService.ts +++ b/src/core/services/PaymentService.ts @@ -130,7 +130,7 @@ export class PaymentService extends BaseService { /** * Pay Lightning invoice with user's wallet */ - async payInvoiceWithUserWallet( + async payWithWallet( paymentRequest: string, requiredAmountSats?: number, options: PaymentOptions = {} @@ -194,7 +194,7 @@ export class PaymentService extends BaseService { /** * Open external Lightning wallet */ - openExternalLightningWallet(paymentRequest: string): void { + openExternalWallet(paymentRequest: string): void { if (!paymentRequest) { toast.error('No payment request available') return @@ -223,7 +223,7 @@ export class PaymentService extends BaseService { * Handle payment with automatic fallback * Tries wallet payment first, falls back to external wallet */ - async handlePaymentWithFallback( + async handlePayment( paymentRequest: string, requiredAmountSats?: number, options: PaymentOptions = {} @@ -236,7 +236,7 @@ export class PaymentService extends BaseService { // Try wallet payment first if user has balance if (this.hasWalletWithBalance) { try { - return await this.payInvoiceWithUserWallet( + return await this.payWithWallet( paymentRequest, requiredAmountSats, options @@ -248,7 +248,7 @@ export class PaymentService extends BaseService { } // Fallback to external wallet - this.openExternalLightningWallet(paymentRequest) + this.openExternalWallet(paymentRequest) return null // External wallet payment status unknown } diff --git a/src/modules/base/index.ts b/src/modules/base/index.ts index b3f7757..b4a8d62 100644 --- a/src/modules/base/index.ts +++ b/src/modules/base/index.ts @@ -38,7 +38,8 @@ export const baseModule: ModulePlugin = { container.provide('pwaService', pwaService) // Initialize core services - await relayHub.initialize(options?.config?.nostr?.relays || []) + relayHub.setRelayUrls(options?.config?.nostr?.relays || []) + await relayHub.initialize() await auth.initialize({ waitForDependencies: false, // Auth has no dependencies maxRetries: 1 diff --git a/src/modules/base/nostr/relay-hub.ts b/src/modules/base/nostr/relay-hub.ts index d6be10c..d437456 100644 --- a/src/modules/base/nostr/relay-hub.ts +++ b/src/modules/base/nostr/relay-hub.ts @@ -75,7 +75,6 @@ export class RelayHub extends BaseService { private relayConfigs: Map = new Map() private connectedRelays: Map = new Map() private subscriptions: Map = new Map() - public isInitializedLegacy = false // Keep for backward compatibility private reconnectInterval?: number private healthCheckInterval?: number private mobileVisibilityHandler?: () => void @@ -94,7 +93,7 @@ export class RelayHub extends BaseService { this.setupMobileVisibilityHandling() } - // Forward EventEmitter methods + // Delegate to internal EventEmitter on(event: string, listener: Function): void { this.eventEmitter.on(event, listener) } @@ -149,39 +148,37 @@ export class RelayHub extends BaseService { }) } + private relayUrls: string[] = [] + /** - * Initialize the relay hub with relay configurations - * This is the public API that maintains backward compatibility + * Set relay URLs before initialization */ - async initialize(relayUrls: string[]): Promise - async initialize(options: any): Promise - async initialize(relayUrlsOrOptions: string[] | any): Promise { - // Handle backward compatibility for relayUrls array - if (Array.isArray(relayUrlsOrOptions)) { - this.pendingRelayUrls = relayUrlsOrOptions - // Use BaseService's initialize method - await super.initialize({ - waitForDependencies: false, // RelayHub has no dependencies - maxRetries: 1 - }) - } else { - // This is a call from BaseService or other services - await super.initialize(relayUrlsOrOptions) - } + setRelayUrls(urls: string[]): void { + this.relayUrls = urls } - private pendingRelayUrls: string[] = [] + /** + * Initialize the relay hub + */ + async initialize(options = {}): Promise { + // Use BaseService's initialize method + await super.initialize({ + waitForDependencies: false, // RelayHub has no dependencies + maxRetries: 1, + ...options + }) + } /** * Service-specific initialization (called by BaseService) */ protected async onInitialize(): Promise { - const relayUrls = this.pendingRelayUrls - - if (!relayUrls || relayUrls.length === 0) { - throw new Error('No relay URLs provided for initialization') + if (!this.relayUrls || this.relayUrls.length === 0) { + throw new Error('No relay URLs provided. Call setRelayUrls() before initialize()') } + const relayUrls = this.relayUrls + this.debug(`Initializing with URLs: ${relayUrls.join(', ')}`) // Convert URLs to relay configs @@ -201,7 +198,6 @@ export class RelayHub extends BaseService { this.debug('Starting connection...') await this.connect() this.startHealthCheck() - this.isInitializedLegacy = true // Keep for backward compatibility this.debug('Initialization complete') } @@ -573,7 +569,6 @@ export class RelayHub extends BaseService { // Disconnect and cleanup this.disconnect() this.removeAllListeners() - this.isInitializedLegacy = false this.debug('RelayHub disposed') } diff --git a/src/modules/events/composables/useTicketPurchase.ts b/src/modules/events/composables/useTicketPurchase.ts index 64b304d..ea67531 100644 --- a/src/modules/events/composables/useTicketPurchase.ts +++ b/src/modules/events/composables/useTicketPurchase.ts @@ -68,7 +68,7 @@ export function useTicketPurchase() { // Pay with wallet - delegate to PaymentService async function payWithWallet(paymentRequest: string) { try { - await paymentService.payInvoiceWithUserWallet(paymentRequest, undefined, { + await paymentService.payWithWallet(paymentRequest, undefined, { showToast: false // We'll handle success notifications in the ticket purchase flow }) return true @@ -179,7 +179,7 @@ export function useTicketPurchase() { // Open Lightning wallet - delegate to PaymentService function handleOpenLightningWallet() { if (paymentRequest.value) { - paymentService.openExternalLightningWallet(paymentRequest.value) + paymentService.openExternalWallet(paymentRequest.value) } } diff --git a/src/modules/market/composables/useLightningPayment.ts b/src/modules/market/composables/useLightningPayment.ts index 5109f0c..5e687e0 100644 --- a/src/modules/market/composables/useLightningPayment.ts +++ b/src/modules/market/composables/useLightningPayment.ts @@ -22,7 +22,7 @@ export function useLightningPayment() { async function payInvoice(paymentRequest: string, orderId?: string): Promise { try { // Use PaymentService with custom success message - const paymentResult = await paymentService.payInvoiceWithUserWallet( + const paymentResult = await paymentService.payWithWallet( paymentRequest, undefined, // No specific amount requirement { @@ -61,14 +61,14 @@ export function useLightningPayment() { // Open external Lightning wallet (fallback) - delegate to PaymentService function openExternalLightningWallet(paymentRequest: string) { - paymentService.openExternalLightningWallet(paymentRequest) + paymentService.openExternalWallet(paymentRequest) } // Main payment handler - tries wallet first, falls back to external async function handlePayment(paymentRequest: string, orderId?: string): Promise { try { // Use PaymentService's automatic fallback handling - const result = await paymentService.handlePaymentWithFallback( + const result = await paymentService.handlePayment( paymentRequest, undefined, // No specific amount requirement {