fix persistent subscription not calling EOSE

This commit is contained in:
hzrd149 2024-08-18 13:10:51 -05:00
parent de60ae91fb
commit 835ae675fc
8 changed files with 55 additions and 32 deletions

View File

@ -67,7 +67,7 @@
"match-sorter": "^6.3.1",
"nanoid": "^5.0.4",
"ngeohash": "^0.6.3",
"nostr-idb": "^2.1.5",
"nostr-idb": "^2.1.6",
"nostr-tools": "^2.7.1",
"nostr-wasm": "^0.1.0",
"prettier": "^3.2.5",

View File

@ -90,7 +90,7 @@ export default class BatchEventLoader {
this.start();
}
update() {
async update() {
// copy everything from next to pending
for (const [key, defer] of this.next) this.pending.set(key, defer);
this.next.clear();
@ -99,9 +99,14 @@ export default class BatchEventLoader {
if (this.pending.size > 0) {
this.log(`Updating filters ${this.pending.size} events`);
this.subscription.filters = [{ ids: Array.from(this.pending.keys()) }];
this.subscription.update();
this.process.active = true;
try {
this.process.active = true;
this.subscription.filters = [{ ids: Array.from(this.pending.keys()) }];
await this.subscription.update();
} catch (error) {
if (error instanceof Error) this.log(`Failed to update subscription`, error.message);
this.process.active = false;
}
} else {
this.log("Closing");
this.subscription.close();

View File

@ -111,7 +111,7 @@ export default class BatchIdentifierLoader {
if (this.next.size > 0) this.requestUpdate();
}
update() {
async update() {
// copy everything from next to pending
for (const [identifier, defer] of this.next) this.pending.set(identifier, defer);
this.next.clear();
@ -127,11 +127,16 @@ export default class BatchIdentifierLoader {
dTags.push(identifier);
}
this.subscription.filters = [];
if (dTags.length > 0) this.subscription.filters.push({ "#d": dTags, kinds: this.kinds });
try {
this.process.active = true;
this.subscription.filters = [];
if (dTags.length > 0) this.subscription.filters.push({ "#d": dTags, kinds: this.kinds });
this.subscription.update();
this.process.active = true;
await this.subscription.update();
} catch (error) {
if (error instanceof Error) this.log(`Failed to update subscription`, error.message);
this.process.active = false;
}
} else {
this.log("Closing");
this.subscription.close();

View File

@ -102,7 +102,7 @@ export default class BatchKindPubkeyLoader {
if (this.next.size > 0) this.start();
}
update() {
async update() {
// copy everything from next to pending
for (const [key, defer] of this.next) this.pending.set(key, defer);
this.next.clear();
@ -132,9 +132,14 @@ export default class BatchKindPubkeyLoader {
.join(", "),
);
this.subscription.filters = Array.from(Object.values(filters));
this.subscription.update();
this.process.active = true;
try {
this.process.active = true;
this.subscription.filters = Array.from(Object.values(filters));
await this.subscription.update();
} catch (error) {
if (error instanceof Error) this.log(`Subscription failed to update`, error.message);
this.process.active = false;
}
} else {
this.log("Closing");
this.subscription.close();

View File

@ -109,7 +109,7 @@ export default class BatchRelationLoader {
if (this.next.size > 0) this.requestUpdate();
}
update() {
async update() {
// copy everything from next to pending
for (const [uid, defer] of this.next) this.pending.set(uid, defer);
this.next.clear();
@ -128,12 +128,17 @@ export default class BatchRelationLoader {
else ids.push(uid);
}
this.subscription.filters = [];
if (ids.length > 0) this.subscription.filters.push({ "#e": ids, kinds: this.kinds });
if (ids.length > 0) this.subscription.filters.push({ "#a": cords, kinds: this.kinds });
try {
this.process.active = true;
this.subscription.filters = [];
if (ids.length > 0) this.subscription.filters.push({ "#e": ids, kinds: this.kinds });
if (ids.length > 0) this.subscription.filters.push({ "#a": cords, kinds: this.kinds });
this.subscription.update();
this.process.active = true;
await this.subscription.update();
} catch (error) {
if (error instanceof Error) this.log(`Failed to update subscription`, error.message);
this.process.active = false;
}
} else {
this.log("Closing");
this.subscription.close();

View File

@ -103,7 +103,9 @@ export default class MultiSubscription {
if (subscription) {
subscription.filters = this.filters;
subscription.update();
subscription.update().catch((err) => {
// eat error
});
}
}
}
@ -123,7 +125,9 @@ export default class MultiSubscription {
(!isFilterEqual(this.cacheSubscription.filters, this.filters) || this.cacheSubscription.closed)
) {
this.cacheSubscription.filters = this.filters;
this.cacheSubscription.update();
this.cacheSubscription.update().catch((err) => {
// eat error
});
}
} else if (this.cacheSubscription?.closed === false) {
this.cacheSubscription.close();

View File

@ -37,13 +37,14 @@ export default class PersistentSubscription {
processManager.registerProcess(this.process);
}
/** attempts to update the subscription */
async update() {
if (!this.filters || this.filters.length === 0) return this;
if (!this.filters || this.filters.length === 0) throw new Error("Missing filters");
if (!(await relayPoolService.waitForOpen(this.relay))) return;
if (!(await relayPoolService.waitForOpen(this.relay))) throw new Error("Failed to connect to relay");
// check if its possible to subscribe to this relay
if (!relayPoolService.canSubscribe(this.relay)) return;
if (!relayPoolService.canSubscribe(this.relay)) throw new Error("Cant subscribe to relay");
this.closed = false;
this.process.active = true;
@ -70,9 +71,7 @@ export default class PersistentSubscription {
// NOTE: reset the eosed flag since nostr-tools dose not
this.subscription.eosed = false;
this.subscription.fire();
}
return this;
} else throw new Error("Subscription filters have not changed");
}
close() {
if (this.closed) return this;

View File

@ -6238,10 +6238,10 @@ normalize-package-data@^2.5.0:
semver "2 || 3 || 4 || 5"
validate-npm-package-license "^3.0.1"
nostr-idb@^2.1.5:
version "2.1.5"
resolved "https://registry.yarnpkg.com/nostr-idb/-/nostr-idb-2.1.5.tgz#672b0a7689b6fd17fbf5cd9e466cabd388c436e1"
integrity sha512-4ZD8qT5Yud3OF7pEbQ7JzRIG+nRJlaskYD8BMNN0TDzAt7WTZQMRKV85UhocX3MXB6EuYZkxsN0VyI1qItWtLA==
nostr-idb@^2.1.6:
version "2.1.6"
resolved "https://registry.yarnpkg.com/nostr-idb/-/nostr-idb-2.1.6.tgz#d62dcbc9c332b7e1be17a7aa7ae4091b39276183"
integrity sha512-1iWqeL1+bVWVmkYLHB6vDt36VxPCbdIzoHqUBEp+8ElXXgmLYIwq9V9y+n1BYHZTV6E95/sOs02PRb3eRMJ/jw==
dependencies:
debug "^4.3.6"
idb "^8.0.0"