Skip to content

Commit 7808861

Browse files
authored
common: Fix construction of write-tracking key (#2312)
1 parent 18de56b commit 7808861

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

packages/runtime-common/realm.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -490,19 +490,23 @@ export class Realm {
490490
: (await this.#adapter.exists(path))
491491
? 'updated'
492492
: 'added';
493-
let messageHash = `${type}-${JSON.stringify({ [type]: path })}`;
493+
let recentWritesKey = this.constructRecentWritesKey(type, path);
494494
this.#recentWrites.set(
495-
messageHash,
495+
recentWritesKey,
496496
setTimeout(() => {
497-
this.#recentWrites.delete(messageHash);
497+
this.#recentWrites.delete(recentWritesKey);
498498
}, 500) as unknown as number, // don't use NodeJS Timeout type
499499
);
500500
}
501501

502+
private constructRecentWritesKey(operation: string, path: string) {
503+
return `${operation}-${JSON.stringify({ [operation]: path })}`;
504+
}
505+
502506
private getTrackedWrite(
503507
data: UpdateRealmEventContent,
504508
): { isTracked: boolean; url: URL } | undefined {
505-
let file: string | undefined;
509+
let file: string;
506510
let type: string | undefined;
507511
if ('updated' in data) {
508512
file = data.updated;
@@ -516,14 +520,14 @@ export class Realm {
516520
} else {
517521
return;
518522
}
519-
let messageHash = `${type}-${JSON.stringify(data)}`;
523+
let recentWritesKey = this.constructRecentWritesKey(type, file);
520524
let url = this.paths.fileURL(file);
521-
let timeout = this.#recentWrites.get(messageHash);
525+
let timeout = this.#recentWrites.get(recentWritesKey);
522526
if (timeout) {
523527
// This is a best attempt to eliminate an echo here since it's unclear whether this update is one
524528
// that we wrote or one that was created outside of us
525529
clearTimeout(timeout);
526-
this.#recentWrites.delete(messageHash);
530+
this.#recentWrites.delete(recentWritesKey);
527531
return { isTracked: true, url };
528532
}
529533
return { isTracked: false, url };

0 commit comments

Comments
 (0)