Skip to content

Commit

Permalink
fix: don't delete readonly field 'bag'
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Jun 11, 2023
1 parent 6c5df9a commit 5837f5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/watch-fs/FileSystemMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class FileSystemMonitor {

constructor() {
// eslint-disable-next-line no-console, @typescript-eslint/no-empty-function
this.debugLog = this.isDebug ? console.log : () => {};
this.debugLog = this.isDebug ? (...args: any[]) => console.log('[watch-fs]', ...args) : () => {};
this.watchPathBase = path.resolve(
($tw.boot.wikiInfo?.config as ITiddlyWikiInfoJSONWithExtraConfig | undefined)?.watchFolder || $tw.boot.wikiTiddlersPath || './tiddlers',
);
Expand All @@ -88,9 +88,9 @@ class FileSystemMonitor {
'**/.git',
],
atomic: true,
useFsEvents: false, // fsevents is not bundled with 3rds.js
// usePolling: true, // CHOKIDAR_USEPOLLING=1
});
this.setupListeners();
}

// Helpers to maintain our cached index for file path and tiddler title
Expand Down
25 changes: 15 additions & 10 deletions src/watch-fs/deep-equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ function isTiddlerField(x: unknown): x is ITiddlerFields {
return typeof x === 'object' && x !== null;
}

/**
* having fields like bag, revision doesn't mean two tiddler not equal, so we are not counting them
*/
const fieldsToCount = ['bag', 'revision'];

export function deepEqual(x: unknown, y: unknown): boolean {
if (x === y) {
return true;
Expand Down Expand Up @@ -43,11 +48,12 @@ export function deepEqual(x: unknown, y: unknown): boolean {
return true;
}
if (isTiddlerField(x) && isTiddlerField(y)) {
deleteRuntimeFieldsFromTiddler(x);
deleteRuntimeFieldsFromTiddler(y);
if (Object.keys(x).length !== Object.keys(y).length) return false;
const xNotCount = countRuntimeFieldsFromTiddler(x);
const yNotCount = countRuntimeFieldsFromTiddler(y);
if ((Object.keys(x).length - xNotCount) !== (Object.keys(y).length - yNotCount)) return false;

for (const [property, xValue] of Object.entries(x)) {
if (fieldsToCount.includes(property)) continue;
if (!deepEqual(xValue, (y as Record<string, unknown>)?.[property])) return false;
}

Expand Down Expand Up @@ -82,17 +88,16 @@ function timeStampEqual(x: unknown, y: unknown) {
return false;
}

const fieldsToDelete = ['bag', 'revision'];
/**
* Delete things like "bag" and "revision" that doesn't save to file
* Count things like "bag" and "revision" that doesn't save to file
* @param {tiddler.fields} tiddlerFields
*/
function deleteRuntimeFieldsFromTiddler(tiddlerFields: ITiddlerFields) {
for (const fieldName of fieldsToDelete) {
function countRuntimeFieldsFromTiddler(tiddlerFields: ITiddlerFields) {
let counter = 0;
for (const fieldName of fieldsToCount) {
if (fieldName in tiddlerFields) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error Index signature in type 'ITiddlerFields' only permits reading.ts(2542)
delete tiddlerFields[fieldName];
counter += 1;
}
}
return counter;
}

0 comments on commit 5837f5f

Please sign in to comment.