From c54a1f037c499ae930bb449152b79aaea18132ca Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Tue, 15 Nov 2022 17:06:41 -0700 Subject: [PATCH] only update nodes once per patch (#55) --- client/lib/patch-db.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/client/lib/patch-db.ts b/client/lib/patch-db.ts index 62ccb22..8cdc983 100644 --- a/client/lib/patch-db.ts +++ b/client/lib/patch-db.ts @@ -173,22 +173,19 @@ export class PatchDB { applyOperation(cache, op) }) // update watched nodes - revision.patch.forEach(op => { - this.updateWatchedNodes(op.path, cache.data) + Object.entries(this.watchedNodes).forEach(([watchedPath, { pathArr }]) => { + const match = revision.patch.find(({ path }) => { + const arr = arrayFromPath(path) + return startsWith(pathArr, arr) || startsWith(arr, pathArr) + }) + if (match) this.updateWatchedNode(watchedPath, cache.data) }) } private handleDump(dump: Dump, cache: DBCache): void { cache.data = { ...dump.value } - this.updateWatchedNodes('', cache.data) - } - - private updateWatchedNodes(revisionPath: string, data: T): void { - const r = arrayFromPath(revisionPath) - Object.entries(this.watchedNodes).forEach(([path, { pathArr }]) => { - if (startsWith(pathArr, r) || startsWith(r, pathArr)) { - this.updateWatchedNode(path, data) - } + Object.keys(this.watchedNodes).forEach(watchedPath => { + this.updateWatchedNode(watchedPath, cache.data) }) }