diff --git a/package.json b/package.json index e1ff7401d..5df8632ec 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/classes/batch-event-loader.ts b/src/classes/batch-event-loader.ts index 5ca457d10..69fdf834a 100644 --- a/src/classes/batch-event-loader.ts +++ b/src/classes/batch-event-loader.ts @@ -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(); diff --git a/src/classes/batch-identifier-loader.ts b/src/classes/batch-identifier-loader.ts index fa26fdaec..75945d51e 100644 --- a/src/classes/batch-identifier-loader.ts +++ b/src/classes/batch-identifier-loader.ts @@ -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(); diff --git a/src/classes/batch-kind-pubkey-loader.ts b/src/classes/batch-kind-pubkey-loader.ts index 626ba72b3..54e90cc5c 100644 --- a/src/classes/batch-kind-pubkey-loader.ts +++ b/src/classes/batch-kind-pubkey-loader.ts @@ -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(); diff --git a/src/classes/batch-relation-loader.ts b/src/classes/batch-relation-loader.ts index f8a95261e..90cd1052e 100644 --- a/src/classes/batch-relation-loader.ts +++ b/src/classes/batch-relation-loader.ts @@ -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(); diff --git a/src/classes/multi-subscription.ts b/src/classes/multi-subscription.ts index fa39cff6d..f9468491e 100644 --- a/src/classes/multi-subscription.ts +++ b/src/classes/multi-subscription.ts @@ -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(); diff --git a/src/classes/persistent-subscription.ts b/src/classes/persistent-subscription.ts index ea84f3e95..8b5913cc9 100644 --- a/src/classes/persistent-subscription.ts +++ b/src/classes/persistent-subscription.ts @@ -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; diff --git a/yarn.lock b/yarn.lock index 6757e3b76..c1438f1a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"