From 56b44b598ad42e56c9497001151c8d07bc742a27 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Mon, 11 Sep 2023 17:55:19 +0300 Subject: [PATCH] save recursion with subscriptions, fix logout --- src/js/nostr/Session.ts | 1 + src/js/state/Node.ts | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/js/nostr/Session.ts b/src/js/nostr/Session.ts index 78566511..79151cf8 100644 --- a/src/js/nostr/Session.ts +++ b/src/js/nostr/Session.ts @@ -43,6 +43,7 @@ const Session = { } IndexedDB.clear(); localStorage.clear(); + location.reload(); }, unsubscribe() { // wat dis diff --git a/src/js/state/Node.ts b/src/js/state/Node.ts index d9d26994..699cfc06 100644 --- a/src/js/state/Node.ts +++ b/src/js/state/Node.ts @@ -12,6 +12,11 @@ type NodeProps = { parent?: Node | null; }; +type Subscription = { + callback: Callback; + recursion: number; +}; + export const DIR_VALUE = '__DIR__'; /** @@ -24,8 +29,8 @@ export default class Node { parent: Node | null; children = new Map(); // should subscriptions also include the desired level of recursion? - on_subscriptions = new Map(); - map_subscriptions = new Map(); + on_subscriptions = new Map(); + map_subscriptions = new Map(); adapters: Adapter[]; private counter = 0; @@ -61,7 +66,7 @@ export default class Node { value, }; const promises = this.adapters.map((adapter) => adapter.set(this.id, nodeValue)); - this.on_subscriptions.forEach((callback) => { + this.on_subscriptions.forEach(({ callback }) => { callback(value, this.id, updatedAt, () => {}); }); await Promise.all(promises); @@ -95,10 +100,15 @@ export default class Node { if (!this.parent.children.has(childName)) { this.parent.children.set(childName, this); } - for (const [id, callback] of this.parent.map_subscriptions) { - callback(value, this.id, updatedAt, () => { - this.parent?.map_subscriptions.delete(id); - }); + for (const [id, { callback, recursion }] of this.parent.map_subscriptions) { + if (value !== DIR_VALUE || recursion === 0) { + callback(value, this.id, updatedAt, () => { + this.parent?.map_subscriptions.delete(id); + }); + } else if (recursion > 0) { + // TODO fix + //this.open(callback, recursion - 1); + } } } } @@ -156,7 +166,7 @@ export default class Node { } }; - this.on_subscriptions.set(uniqueId, localCallback); + this.on_subscriptions.set(uniqueId, { callback: localCallback, recursion }); const adapterUnsubscribes = this.adapters.map((adapter) => adapter.get(this.id, localCallback)); @@ -175,7 +185,7 @@ export default class Node { */ map(callback: Callback, recursion = 0): Unsubscribe { const id = this.counter++; - this.map_subscriptions.set(id, callback); + this.map_subscriptions.set(id, { callback, recursion }); const latestMap = new Map(); let adapterSubs: Unsubscribe[] = [];