Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ffe2af

Browse files
committed
fix: remove race condition in caching logic
1 parent d7c70e2 commit 0ffe2af

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

site/src/hooks/useTimeSync.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ class ReactTimeSync implements ReactTimeSyncApi {
6868
subscribe = (entry: ReactSubscriptionEntry): (() => void) => {
6969
const { select, id, targetRefreshInterval, onUpdate } = entry;
7070

71-
const patchedEntry: SubscriptionEntry = {
71+
const fullEntry: SubscriptionEntry = {
7272
id,
7373
targetRefreshInterval,
7474
onUpdate: (newDate) => {
75-
const prevSelection = this.getSelectionSnapshot(id);
75+
const prevSelection = this.#selectionCache.get(id)?.value;
7676
const newSelection = select?.(newDate) ?? newDate;
7777
if (areValuesDeepEqual(prevSelection, newSelection)) {
7878
return;
@@ -83,7 +83,7 @@ class ReactTimeSync implements ReactTimeSyncApi {
8383
},
8484
};
8585

86-
this.#timeSync.subscribe(patchedEntry);
86+
this.#timeSync.subscribe(fullEntry);
8787
return () => this.#timeSync.unsubscribe(id);
8888
};
8989

@@ -114,13 +114,13 @@ class ReactTimeSync implements ReactTimeSyncApi {
114114
const dateSnapshot = this.#timeSync.getTimeSnapshot();
115115
const newSelection = select?.(dateSnapshot) ?? dateSnapshot;
116116

117-
const prevSelection = this.#selectionCache.get(id);
118-
if (prevSelection === undefined) {
117+
const cacheEntry = this.#selectionCache.get(id);
118+
if (cacheEntry === undefined) {
119119
this.#selectionCache.set(id, { value: newSelection });
120120
return;
121121
}
122122

123-
if (!areValuesDeepEqual(newSelection, prevSelection.value)) {
123+
if (!areValuesDeepEqual(newSelection, cacheEntry.value)) {
124124
this.#selectionCache.set(id, { value: newSelection });
125125
}
126126
};

0 commit comments

Comments
 (0)