feat(nbstore): add debug log for incorrect writing (#10697)

This commit is contained in:
EYHN
2025-03-17 09:09:27 +00:00
parent aa70759f44
commit 7dbc9b42b7
+25 -1
View File
@@ -13,7 +13,9 @@ import {
applyUpdate, applyUpdate,
type Doc as YDoc, type Doc as YDoc,
encodeStateAsUpdate, encodeStateAsUpdate,
Map as YMap,
mergeUpdates, mergeUpdates,
type Transaction as YTransaction,
} from 'yjs'; } from 'yjs';
import type { DocRecord, DocStorage } from '../storage'; import type { DocRecord, DocStorage } from '../storage';
@@ -403,13 +405,18 @@ export class DocFrontend {
this.statusUpdatedSubject$.next(job.docId); this.statusUpdatedSubject$.next(job.docId);
} }
private isApplyingUpdate = false;
applyUpdate(docId: string, update: Uint8Array) { applyUpdate(docId: string, update: Uint8Array) {
const doc = this.status.docs.get(docId); const doc = this.status.docs.get(docId);
if (doc && !isEmptyUpdate(update)) { if (doc && !isEmptyUpdate(update)) {
try { try {
this.isApplyingUpdate = true;
applyUpdate(doc, update, NBSTORE_ORIGIN); applyUpdate(doc, update, NBSTORE_ORIGIN);
} catch (err) { } catch (err) {
console.error('failed to apply update yjs doc', err); console.error('failed to apply update yjs doc', err);
} finally {
this.isApplyingUpdate = false;
} }
} }
} }
@@ -417,11 +424,28 @@ export class DocFrontend {
private readonly handleDocUpdate = ( private readonly handleDocUpdate = (
update: Uint8Array, update: Uint8Array,
origin: any, origin: any,
doc: YDoc doc: YDoc,
transaction: YTransaction
) => { ) => {
if (origin === NBSTORE_ORIGIN) { if (origin === NBSTORE_ORIGIN) {
return; 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)) { if (!this.status.docs.has(doc.guid)) {
return; return;
} }