From 7dbc9b42b7a07c6e198ce319caf7f567878286a4 Mon Sep 17 00:00:00 2001 From: EYHN Date: Mon, 17 Mar 2025 09:09:27 +0000 Subject: [PATCH] feat(nbstore): add debug log for incorrect writing (#10697) --- packages/common/nbstore/src/frontend/doc.ts | 26 ++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/common/nbstore/src/frontend/doc.ts b/packages/common/nbstore/src/frontend/doc.ts index 065b64c4e8..f9ffa6a55a 100644 --- a/packages/common/nbstore/src/frontend/doc.ts +++ b/packages/common/nbstore/src/frontend/doc.ts @@ -13,7 +13,9 @@ import { applyUpdate, type Doc as YDoc, encodeStateAsUpdate, + Map as YMap, mergeUpdates, + type Transaction as YTransaction, } from 'yjs'; import type { DocRecord, DocStorage } from '../storage'; @@ -403,13 +405,18 @@ export class DocFrontend { this.statusUpdatedSubject$.next(job.docId); } + private isApplyingUpdate = false; + applyUpdate(docId: string, update: Uint8Array) { const doc = this.status.docs.get(docId); if (doc && !isEmptyUpdate(update)) { try { + this.isApplyingUpdate = true; applyUpdate(doc, update, NBSTORE_ORIGIN); } catch (err) { console.error('failed to apply update yjs doc', err); + } finally { + this.isApplyingUpdate = false; } } } @@ -417,11 +424,28 @@ export class DocFrontend { private readonly handleDocUpdate = ( update: Uint8Array, origin: any, - doc: YDoc + doc: YDoc, + transaction: YTransaction ) => { if (origin === NBSTORE_ORIGIN) { return; } + if (this.isApplyingUpdate && BUILD_CONFIG.debug) { + let changedList = ''; + for (const [changed, keys] of transaction.changed) { + for (const key of keys) { + if (changed instanceof YMap && key) { + changedList += `${key} => ${changed.get(key)}\n`; + } + } + } + console.warn(`⚠️ When nbstore applies a remote update, some code triggers a local change to the doc. +This will causes the document's 'edited by' to become the current user, even if the user has not actually modified the document. +This is usually caused by a coding error and needs to be fixed by the developer. +Changed: +${changedList} +`); + } if (!this.status.docs.has(doc.guid)) { return; }