Skip to content

Commit

Permalink
Merge pull request #150 from matjaz/lazy-load-data
Browse files Browse the repository at this point in the history
feat: lazy parallel loading of lightning address data
  • Loading branch information
rolznz authored Oct 20, 2024
2 parents bd94a16 + ac37951 commit 3eaa6a3
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions src/lightning-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,45 @@ export default class LightningAddress {
);
const json = await result.json();

await this.parseResponse(json.lnurlp, json.keysend, json.nostr);
await this.parseLnUrlPayResponse(json.lnurlp);
this.parseKeysendResponse(json.keysend);
this.parseNostrResponse(json.nostr);
}

async fetchWithoutProxy() {
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;
await Promise.all([
this.fetchLnurlData(),
this.fetchKeysendData(),
this.fetchNostrData(),
]);
}

async fetchLnurlData() {
const lnurlResult = await fetch(this.lnurlpUrl());
if (lnurlResult.ok) {
lnurlData = await lnurlResult.json();
const lnurlData = await lnurlResult.json();
await this.parseLnUrlPayResponse(lnurlData);
}
let keysendData: KeySendRawData | undefined;
}

async fetchKeysendData() {
const keysendResult = await fetch(this.keysendUrl());
if (keysendResult.ok) {
keysendData = await keysendResult.json();
const keysendData = await keysendResult.json();
this.parseKeysendResponse(keysendData);
}
let nostrData: NostrResponse | undefined;
}

async fetchNostrData() {
const nostrResult = await fetch(this.nostrUrl());
if (nostrResult.ok) {
nostrData = await nostrResult.json();
const nostrData = await nostrResult.json();
this.parseNostrResponse(nostrData);
}

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

lnurlpUrl() {
Expand Down Expand Up @@ -249,17 +263,19 @@ export default class LightningAddress {
return response;
}

private async parseResponse(
lnurlpData: LnUrlRawData | undefined,
keysendData: KeySendRawData | undefined,
nostrData: NostrResponse | undefined,
) {
private async parseLnUrlPayResponse(lnurlpData: LnUrlRawData | undefined) {
if (lnurlpData) {
this.lnurlpData = await parseLnUrlPayResponse(lnurlpData);
}
}

private parseKeysendResponse(keysendData: KeySendRawData | undefined) {
if (keysendData) {
this.keysendData = parseKeysendResponse(keysendData);
}
}

private parseNostrResponse(nostrData: NostrResponse | undefined) {
if (nostrData) {
[this.nostrData, this.nostrPubkey, this.nostrRelays] = parseNostrResponse(
nostrData,
Expand Down

0 comments on commit 3eaa6a3

Please sign in to comment.