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

Skip to content

Commit 9d8671e

Browse files
committed
refactor: make class less confusing
1 parent fe8b3ef commit 9d8671e

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

site/src/hooks/useTimeSync.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ type ReactSubscriptionEntry = Readonly<
4343
// try to retrieve a value, it's easy to differentiate between a value being
4444
// undefined because that's an explicit selection value, versus it being
4545
// undefined because we forgot to set it in the cache
46-
type SelectionCacheEntry = Readonly<{
47-
value: unknown;
48-
select?: SelectCallback;
49-
}>;
46+
type SelectionCacheEntry = Readonly<{ value: unknown }>;
5047

5148
interface ReactTimeSyncApi {
5249
subscribe: (entry: ReactSubscriptionEntry) => () => void;
@@ -69,15 +66,6 @@ class ReactTimeSync implements ReactTimeSyncApi {
6966
subscribe = (entry: ReactSubscriptionEntry): (() => void) => {
7067
const { select, id, idealRefreshIntervalMs, onUpdate } = entry;
7168

72-
const updateCacheEntry = () => {
73-
const date = this.#timeSync.getTimeSnapshot();
74-
const cacheValue = select?.(date) ?? date;
75-
this.#selectionCache.set(id, {
76-
value: cacheValue,
77-
select,
78-
});
79-
};
80-
8169
// Make sure that we subscribe first, in case TimeSync is configured to
8270
// invalidate the snapshot on a new subscription. Want to remove risk of
8371
// stale data
@@ -91,13 +79,16 @@ class ReactTimeSync implements ReactTimeSyncApi {
9179
return;
9280
}
9381

94-
updateCacheEntry();
82+
this.#selectionCache.set(id, { value: newSelection });
9583
onUpdate(newDate);
9684
},
9785
};
9886
this.#timeSync.subscribe(patchedEntry);
9987

100-
updateCacheEntry();
88+
const date = this.#timeSync.getTimeSnapshot();
89+
const cacheValue = select?.(date) ?? date;
90+
this.#selectionCache.set(id, { value: cacheValue });
91+
10192
return () => this.#timeSync.unsubscribe(id);
10293
};
10394

@@ -127,13 +118,7 @@ class ReactTimeSync implements ReactTimeSyncApi {
127118

128119
const dateSnapshot = this.#timeSync.getTimeSnapshot();
129120
const newSelection = select?.(dateSnapshot) ?? dateSnapshot;
130-
131-
// Keep the old select callback, because only that version will be
132-
// memoized via useEffectEvent
133-
this.#selectionCache.set(id, {
134-
value: newSelection,
135-
select: cacheEntry.select,
136-
});
121+
this.#selectionCache.set(id, { value: newSelection });
137122
};
138123
}
139124

0 commit comments

Comments
 (0)