Skip to content

Commit

Permalink
fix: use latest webln if available in globalThis after init
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Oct 19, 2023
1 parent 9662648 commit ff0c032
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/lightning-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class LightningAddress {

constructor(address: string, options?: LightningAddressOptions) {
this.address = address;
this.options = { proxy: DEFAULT_PROXY, webln: globalThis.webln };
this.options = { proxy: DEFAULT_PROXY };
this.options = Object.assign(this.options, options);
this.parse();
this.webln = this.options.webln;
Expand All @@ -54,6 +54,10 @@ export default class LightningAddress {
}
}

getWebLN() {
return this.webln || globalThis.webln
}

async fetch() {
if (this.options.proxy) {
return this.fetchWithProxy();
Expand Down Expand Up @@ -186,7 +190,7 @@ export default class LightningAddress {
boost,
},
{
webln: this.webln,
webln: this.getWebLN(),
},
);
}
Expand Down Expand Up @@ -233,12 +237,13 @@ export default class LightningAddress {
options: ZapOptions = {},
): Promise<SendPaymentResponse> {
const invoice = this.zapInvoice(args, options);
if (!this.webln) {
const webln = this.getWebLN()
if (!webln) {
// mainly for TS
throw new Error("WebLN not available");
}
await this.webln.enable();
const response = this.webln.sendPayment((await invoice).paymentRequest);
await webln.enable();
const response = webln.sendPayment((await invoice).paymentRequest);
return response;
}

Expand Down

0 comments on commit ff0c032

Please sign in to comment.