Skip to content

Commit

Permalink
feat: lazy parallel loading of lightning address data
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz committed Aug 5, 2024
1 parent 939b5ee commit fd9646b
Showing 1 changed file with 41 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/lightning-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,31 @@ export default class LightningAddress {
if (!this.domain || !this.username) {
return;
}
const lnurlResult = await fetch(this.lnurlpUrl());
const keysendResult = await fetch(this.keysendUrl());
const nostrResult = await fetch(this.nostrUrl());

let lnurlData: LnUrlRawData | undefined;
if (lnurlResult.ok) {
lnurlData = await lnurlResult.json();
}
let keysendData: KeySendRawData | undefined;
if (keysendResult.ok) {
keysendData = await keysendResult.json();
}
let nostrData: NostrResponse | undefined;
if (nostrResult.ok) {
nostrData = await nostrResult.json();
}
const [lnurlData, keysendData, nostrData] = await Promise.all([
this.requestLnurlData(),
this.requestKeysendData(),
this.requestNostrData(),
]);

await this.parseResponse(lnurlData, keysendData, nostrData);
}

async fetchLnurlData() {
const lnurlData = await this.requestLnurlData();
await this.parseResponse(lnurlData, undefined, undefined);
}

async fetchKeysendData() {
const keysendData = await this.requestKeysendData();
await this.parseResponse(undefined, keysendData, undefined);
}

async fetchNostrData() {
const nostrData = await this.requestNostrData();
await this.parseResponse(undefined, undefined, nostrData);
}

lnurlpUrl() {
return `https://${this.domain}/.well-known/lnurlp/${this.username}`;
}
Expand Down Expand Up @@ -249,6 +254,27 @@ export default class LightningAddress {
return response;
}

private async requestLnurlData(): Promise<LnUrlRawData | undefined> {
const lnurlResult = await fetch(this.lnurlpUrl());
if (lnurlResult.ok) {
return lnurlResult.json();
}
}

private async requestKeysendData(): Promise<KeySendRawData | undefined> {
const keysendResult = await fetch(this.keysendUrl());
if (keysendResult.ok) {
return keysendResult.json();
}
}

private async requestNostrData(): Promise<NostrResponse | undefined> {
const nostrResult = await fetch(this.nostrUrl());
if (nostrResult.ok) {
return nostrResult.json();
}
}

private async parseResponse(
lnurlpData: LnUrlRawData | undefined,
keysendData: KeySendRawData | undefined,
Expand Down

0 comments on commit fd9646b

Please sign in to comment.