Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lazy parallel loading of lightning address data #150

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this one async and the others aren't? did you miss something in your refactoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I haven't. Good eye. First one uses utils await parseLnUrlPayResponse() because sha256 is async. Others doesn't have async code.

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() {
matjaz marked this conversation as resolved.
Show resolved Hide resolved
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
Loading