From 7fb8e9b6da86698334871ff4ff4e35bef2d36f65 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 20 Jul 2021 11:23:06 -0600 Subject: [PATCH] return whole doc, better unsubscribe --- client/lib/json-patch-lib.ts | 1 + client/lib/store.ts | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/client/lib/json-patch-lib.ts b/client/lib/json-patch-lib.ts index 5ccf5b4..c88371b 100644 --- a/client/lib/json-patch-lib.ts +++ b/client/lib/json-patch-lib.ts @@ -25,6 +25,7 @@ export type Doc = { [key: string]: any } export type Operation = AddOperation | RemoveOperation | ReplaceOperation export function getValueByPointer (document: any, pointer: string): any { + if (pointer === '/') return document const pathArr = pointer.split('/') pathArr.shift() try { diff --git a/client/lib/store.ts b/client/lib/store.ts index c667fc0..180a651 100644 --- a/client/lib/store.ts +++ b/client/lib/store.ts @@ -1,6 +1,5 @@ import { DBCache, Dump, Http, Revision, Update } from './types' import { BehaviorSubject, Observable } from 'rxjs' -import { finalize } from 'rxjs/operators' import { applyOperation, getValueByPointer, Operation } from './json-patch-lib' import BTree from 'sorted-btree' @@ -34,9 +33,6 @@ export class Store { const path = `/${args.join('/')}` if (!this.watchedNodes[path]) { this.watchedNodes[path] = new BehaviorSubject(getValueByPointer(this.cache.data, path)) - this.watchedNodes[path].pipe( - finalize(() => delete this.watchedNodes[path]), - ) } return this.watchedNodes[path].asObservable() } @@ -134,14 +130,19 @@ export class Store { } private updateWatchedNodes (revisionPath: string) { + const kill = (path: string) => { + this.watchedNodes[path].complete() + delete this.watchedNodes[path] + } + Object.keys(this.watchedNodes).forEach(path => { + if (this.watchedNodes[path].observers.length === 0) return kill(path) + if (path.includes(revisionPath) || revisionPath.includes(path)) { const val = getValueByPointer(this.cache.data, path) - if (val !== undefined) { - this.watchedNodes[path].next(val) - } else { - this.watchedNodes[path].complete() - } + if (val === undefined) return kill(path) + + this.watchedNodes[path].next(val) } }) } @@ -154,4 +155,4 @@ export class Store { private isRevision (update: Update): update is Revision { return !!(update as Revision).patch } -} +} \ No newline at end of file