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.
This commit is contained in:
padreug 2025-09-05 15:23:03 +02:00
parent 0bced11623
commit 099c16abc9
5 changed files with 33 additions and 37 deletions

View file

@ -130,7 +130,7 @@ export class PaymentService extends BaseService {
/** /**
* Pay Lightning invoice with user's wallet * Pay Lightning invoice with user's wallet
*/ */
async payInvoiceWithUserWallet( async payWithWallet(
paymentRequest: string, paymentRequest: string,
requiredAmountSats?: number, requiredAmountSats?: number,
options: PaymentOptions = {} options: PaymentOptions = {}
@ -194,7 +194,7 @@ export class PaymentService extends BaseService {
/** /**
* Open external Lightning wallet * Open external Lightning wallet
*/ */
openExternalLightningWallet(paymentRequest: string): void { openExternalWallet(paymentRequest: string): void {
if (!paymentRequest) { if (!paymentRequest) {
toast.error('No payment request available') toast.error('No payment request available')
return return
@ -223,7 +223,7 @@ export class PaymentService extends BaseService {
* Handle payment with automatic fallback * Handle payment with automatic fallback
* Tries wallet payment first, falls back to external wallet * Tries wallet payment first, falls back to external wallet
*/ */
async handlePaymentWithFallback( async handlePayment(
paymentRequest: string, paymentRequest: string,
requiredAmountSats?: number, requiredAmountSats?: number,
options: PaymentOptions = {} options: PaymentOptions = {}
@ -236,7 +236,7 @@ export class PaymentService extends BaseService {
// Try wallet payment first if user has balance // Try wallet payment first if user has balance
if (this.hasWalletWithBalance) { if (this.hasWalletWithBalance) {
try { try {
return await this.payInvoiceWithUserWallet( return await this.payWithWallet(
paymentRequest, paymentRequest,
requiredAmountSats, requiredAmountSats,
options options
@ -248,7 +248,7 @@ export class PaymentService extends BaseService {
} }
// Fallback to external wallet // Fallback to external wallet
this.openExternalLightningWallet(paymentRequest) this.openExternalWallet(paymentRequest)
return null // External wallet payment status unknown return null // External wallet payment status unknown
} }

View file

@ -38,7 +38,8 @@ export const baseModule: ModulePlugin = {
container.provide('pwaService', pwaService) container.provide('pwaService', pwaService)
// Initialize core services // Initialize core services
await relayHub.initialize(options?.config?.nostr?.relays || []) relayHub.setRelayUrls(options?.config?.nostr?.relays || [])
await relayHub.initialize()
await auth.initialize({ await auth.initialize({
waitForDependencies: false, // Auth has no dependencies waitForDependencies: false, // Auth has no dependencies
maxRetries: 1 maxRetries: 1

View file

@ -75,7 +75,6 @@ export class RelayHub extends BaseService {
private relayConfigs: Map<string, RelayConfig> = new Map() private relayConfigs: Map<string, RelayConfig> = new Map()
private connectedRelays: Map<string, Relay> = new Map() private connectedRelays: Map<string, Relay> = new Map()
private subscriptions: Map<string, any> = new Map() private subscriptions: Map<string, any> = new Map()
public isInitializedLegacy = false // Keep for backward compatibility
private reconnectInterval?: number private reconnectInterval?: number
private healthCheckInterval?: number private healthCheckInterval?: number
private mobileVisibilityHandler?: () => void private mobileVisibilityHandler?: () => void
@ -94,7 +93,7 @@ export class RelayHub extends BaseService {
this.setupMobileVisibilityHandling() this.setupMobileVisibilityHandling()
} }
// Forward EventEmitter methods // Delegate to internal EventEmitter
on(event: string, listener: Function): void { on(event: string, listener: Function): void {
this.eventEmitter.on(event, listener) this.eventEmitter.on(event, listener)
} }
@ -149,39 +148,37 @@ export class RelayHub extends BaseService {
}) })
} }
private relayUrls: string[] = []
/** /**
* Initialize the relay hub with relay configurations * Set relay URLs before initialization
* This is the public API that maintains backward compatibility
*/ */
async initialize(relayUrls: string[]): Promise<void> setRelayUrls(urls: string[]): void {
async initialize(options: any): Promise<void> this.relayUrls = urls
async initialize(relayUrlsOrOptions: string[] | any): Promise<void> { }
// Handle backward compatibility for relayUrls array
if (Array.isArray(relayUrlsOrOptions)) { /**
this.pendingRelayUrls = relayUrlsOrOptions * Initialize the relay hub
*/
async initialize(options = {}): Promise<void> {
// Use BaseService's initialize method // Use BaseService's initialize method
await super.initialize({ await super.initialize({
waitForDependencies: false, // RelayHub has no dependencies waitForDependencies: false, // RelayHub has no dependencies
maxRetries: 1 maxRetries: 1,
...options
}) })
} else {
// This is a call from BaseService or other services
await super.initialize(relayUrlsOrOptions)
} }
}
private pendingRelayUrls: string[] = []
/** /**
* Service-specific initialization (called by BaseService) * Service-specific initialization (called by BaseService)
*/ */
protected async onInitialize(): Promise<void> { protected async onInitialize(): Promise<void> {
const relayUrls = this.pendingRelayUrls if (!this.relayUrls || this.relayUrls.length === 0) {
throw new Error('No relay URLs provided. Call setRelayUrls() before initialize()')
if (!relayUrls || relayUrls.length === 0) {
throw new Error('No relay URLs provided for initialization')
} }
const relayUrls = this.relayUrls
this.debug(`Initializing with URLs: ${relayUrls.join(', ')}`) this.debug(`Initializing with URLs: ${relayUrls.join(', ')}`)
// Convert URLs to relay configs // Convert URLs to relay configs
@ -201,7 +198,6 @@ export class RelayHub extends BaseService {
this.debug('Starting connection...') this.debug('Starting connection...')
await this.connect() await this.connect()
this.startHealthCheck() this.startHealthCheck()
this.isInitializedLegacy = true // Keep for backward compatibility
this.debug('Initialization complete') this.debug('Initialization complete')
} }
@ -573,7 +569,6 @@ export class RelayHub extends BaseService {
// Disconnect and cleanup // Disconnect and cleanup
this.disconnect() this.disconnect()
this.removeAllListeners() this.removeAllListeners()
this.isInitializedLegacy = false
this.debug('RelayHub disposed') this.debug('RelayHub disposed')
} }

View file

@ -68,7 +68,7 @@ export function useTicketPurchase() {
// Pay with wallet - delegate to PaymentService // Pay with wallet - delegate to PaymentService
async function payWithWallet(paymentRequest: string) { async function payWithWallet(paymentRequest: string) {
try { try {
await paymentService.payInvoiceWithUserWallet(paymentRequest, undefined, { await paymentService.payWithWallet(paymentRequest, undefined, {
showToast: false // We'll handle success notifications in the ticket purchase flow showToast: false // We'll handle success notifications in the ticket purchase flow
}) })
return true return true
@ -179,7 +179,7 @@ export function useTicketPurchase() {
// Open Lightning wallet - delegate to PaymentService // Open Lightning wallet - delegate to PaymentService
function handleOpenLightningWallet() { function handleOpenLightningWallet() {
if (paymentRequest.value) { if (paymentRequest.value) {
paymentService.openExternalLightningWallet(paymentRequest.value) paymentService.openExternalWallet(paymentRequest.value)
} }
} }

View file

@ -22,7 +22,7 @@ export function useLightningPayment() {
async function payInvoice(paymentRequest: string, orderId?: string): Promise<boolean> { async function payInvoice(paymentRequest: string, orderId?: string): Promise<boolean> {
try { try {
// Use PaymentService with custom success message // Use PaymentService with custom success message
const paymentResult = await paymentService.payInvoiceWithUserWallet( const paymentResult = await paymentService.payWithWallet(
paymentRequest, paymentRequest,
undefined, // No specific amount requirement undefined, // No specific amount requirement
{ {
@ -61,14 +61,14 @@ export function useLightningPayment() {
// Open external Lightning wallet (fallback) - delegate to PaymentService // Open external Lightning wallet (fallback) - delegate to PaymentService
function openExternalLightningWallet(paymentRequest: string) { function openExternalLightningWallet(paymentRequest: string) {
paymentService.openExternalLightningWallet(paymentRequest) paymentService.openExternalWallet(paymentRequest)
} }
// Main payment handler - tries wallet first, falls back to external // Main payment handler - tries wallet first, falls back to external
async function handlePayment(paymentRequest: string, orderId?: string): Promise<void> { async function handlePayment(paymentRequest: string, orderId?: string): Promise<void> {
try { try {
// Use PaymentService's automatic fallback handling // Use PaymentService's automatic fallback handling
const result = await paymentService.handlePaymentWithFallback( const result = await paymentService.handlePayment(
paymentRequest, paymentRequest,
undefined, // No specific amount requirement undefined, // No specific amount requirement
{ {