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:
parent
0bced11623
commit
099c16abc9
5 changed files with 33 additions and 37 deletions
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -75,7 +75,6 @@ export class RelayHub extends BaseService {
|
|||
private relayConfigs: Map<string, RelayConfig> = new Map()
|
||||
private connectedRelays: Map<string, Relay> = new Map()
|
||||
private subscriptions: Map<string, any> = 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<void>
|
||||
async initialize(options: any): Promise<void>
|
||||
async initialize(relayUrlsOrOptions: string[] | any): Promise<void> {
|
||||
// Handle backward compatibility for relayUrls array
|
||||
if (Array.isArray(relayUrlsOrOptions)) {
|
||||
this.pendingRelayUrls = relayUrlsOrOptions
|
||||
setRelayUrls(urls: string[]): void {
|
||||
this.relayUrls = urls
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the relay hub
|
||||
*/
|
||||
async initialize(options = {}): Promise<void> {
|
||||
// Use BaseService's initialize method
|
||||
await super.initialize({
|
||||
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)
|
||||
*/
|
||||
protected async onInitialize(): Promise<void> {
|
||||
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')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ export function useLightningPayment() {
|
|||
async function payInvoice(paymentRequest: string, orderId?: string): Promise<boolean> {
|
||||
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<void> {
|
||||
try {
|
||||
// Use PaymentService's automatic fallback handling
|
||||
const result = await paymentService.handlePaymentWithFallback(
|
||||
const result = await paymentService.handlePayment(
|
||||
paymentRequest,
|
||||
undefined, // No specific amount requirement
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue