mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-05 00:32:03 +00:00
prevent race conditions in BitcoinConnect initialization by memoizing promise, clean up some logs
This commit is contained in:
parent
3583379468
commit
074de680df
@ -7,10 +7,15 @@ const Button = dynamic(() => import('@getalby/bitcoin-connect-react').then(mod =
|
|||||||
|
|
||||||
// Module-level state
|
// Module-level state
|
||||||
let initialized = false;
|
let initialized = false;
|
||||||
|
let initializationPromise = null;
|
||||||
let bitcoinConnectClient = null;
|
let bitcoinConnectClient = null;
|
||||||
|
|
||||||
export async function initializeBitcoinConnect() {
|
export async function initializeBitcoinConnect() {
|
||||||
if (!initialized) {
|
if (initialized) return bitcoinConnectClient; // fast path
|
||||||
|
|
||||||
|
if (initializationPromise) return initializationPromise; // someone else is already doing the work
|
||||||
|
|
||||||
|
initializationPromise = (async () => {
|
||||||
try {
|
try {
|
||||||
// Import the required modules
|
// Import the required modules
|
||||||
const bc = await import('@getalby/bitcoin-connect-react');
|
const bc = await import('@getalby/bitcoin-connect-react');
|
||||||
@ -38,18 +43,20 @@ export async function initializeBitcoinConnect() {
|
|||||||
|
|
||||||
initialized = true;
|
initialized = true;
|
||||||
console.log('Bitcoin Connect initialized successfully, client:', bitcoinConnectClient);
|
console.log('Bitcoin Connect initialized successfully, client:', bitcoinConnectClient);
|
||||||
|
return bitcoinConnectClient;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// If the error is about custom element already being defined, we can ignore it
|
// If the error is about custom element already being defined, we can ignore it
|
||||||
// as it means the component is already initialized
|
// as it means the component is already initialized
|
||||||
if (!error.message?.includes('has already been defined as a custom element')) {
|
if (!error.message?.includes('has already been defined as a custom element')) {
|
||||||
console.error('Error initializing Bitcoin Connect:', error);
|
console.error('Error initializing Bitcoin Connect:', error);
|
||||||
}
|
}
|
||||||
|
throw error; // re-throw so callers see the failure
|
||||||
|
} finally {
|
||||||
|
initializationPromise = null; // allow retry after failure
|
||||||
}
|
}
|
||||||
} else {
|
})();
|
||||||
console.log('Bitcoin Connect already initialized');
|
|
||||||
}
|
return initializationPromise;
|
||||||
|
|
||||||
return bitcoinConnectClient;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export the SDK for direct usage
|
// Export the SDK for direct usage
|
||||||
|
@ -76,7 +76,6 @@ const SubscriptionPaymentButtons = ({
|
|||||||
satoshi: amount,
|
satoshi: amount,
|
||||||
comment: `Subscription Purchase. User: ${session?.user?.id}`,
|
comment: `Subscription Purchase. User: ${session?.user?.id}`,
|
||||||
});
|
});
|
||||||
console.log("Invoice fetched successfully:", newInvoice);
|
|
||||||
return newInvoice;
|
return newInvoice;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching invoice:', error);
|
console.error('Error fetching invoice:', error);
|
||||||
@ -132,7 +131,6 @@ const SubscriptionPaymentButtons = ({
|
|||||||
|
|
||||||
// Get NWC URL
|
// Get NWC URL
|
||||||
const newNWCUrl = newNwc.getNostrWalletConnectUrl();
|
const newNWCUrl = newNwc.getNostrWalletConnectUrl();
|
||||||
console.log("NWC URL generated:", !!newNWCUrl);
|
|
||||||
|
|
||||||
if (newNWCUrl) {
|
if (newNWCUrl) {
|
||||||
const nwcProvider = new sdk.webln.NostrWebLNProvider({
|
const nwcProvider = new sdk.webln.NostrWebLNProvider({
|
||||||
@ -153,7 +151,7 @@ const SubscriptionPaymentButtons = ({
|
|||||||
|
|
||||||
console.log("Sending payment with NWC provider");
|
console.log("Sending payment with NWC provider");
|
||||||
const paymentResponse = await nwcProvider.sendPayment(invoice.paymentRequest);
|
const paymentResponse = await nwcProvider.sendPayment(invoice.paymentRequest);
|
||||||
console.log("Payment response:", paymentResponse);
|
console.log("Payment response:", paymentResponse?.preimage);
|
||||||
|
|
||||||
if (!paymentResponse || !paymentResponse?.preimage) {
|
if (!paymentResponse || !paymentResponse?.preimage) {
|
||||||
showToast('error', 'NWC', 'Payment failed');
|
showToast('error', 'NWC', 'Payment failed');
|
||||||
@ -215,7 +213,7 @@ const SubscriptionPaymentButtons = ({
|
|||||||
|
|
||||||
console.log("Sending payment with manual NWC");
|
console.log("Sending payment with manual NWC");
|
||||||
const payResponse = await nwc.sendPayment(invoice.paymentRequest);
|
const payResponse = await nwc.sendPayment(invoice.paymentRequest);
|
||||||
console.log("Payment response:", payResponse);
|
console.log("Payment response:", payResponse?.preimage);
|
||||||
|
|
||||||
if (!payResponse || !payResponse.preimage) {
|
if (!payResponse || !payResponse.preimage) {
|
||||||
showToast('error', 'NWC', 'Payment failed');
|
showToast('error', 'NWC', 'Payment failed');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user