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

Skip to content

Commit 60b2220

Browse files
committed
fix: move resync option back to base class
1 parent 62e5f8e commit 60b2220

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

site/src/hooks/useTimeSync.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* @todo Things that still need to be done before this can be called done:
33
*
44
* 1. Fill out all incomplete methods
5-
* 2. Make sure the class respects the resyncOnNewSubscription option
6-
* 3. Add tests
7-
* 4. See if there's a way to make sure that if you provide a type parameter to
5+
* 2. Add tests
6+
* 3. See if there's a way to make sure that if you provide a type parameter to
87
* the hook, you must also provide a select function
98
*/
109
import {
@@ -40,21 +39,6 @@ type ReactTimeSyncSubscriptionEntry = Readonly<
4039
}
4140
>;
4241

43-
type ReactTimeSyncInitOptions = Readonly<
44-
TimeSyncInitOptions & {
45-
/**
46-
* Configures whether adding a new subscription will immediately create
47-
* a new time snapshot and use it to update all other subscriptions.
48-
*/
49-
resyncOnNewSubscription: boolean;
50-
}
51-
>;
52-
53-
const defaultReactTimeSyncOptions: ReactTimeSyncInitOptions = {
54-
...defaultOptions,
55-
resyncOnNewSubscription: true,
56-
};
57-
5842
interface ReactTimeSyncApi {
5943
subscribe: (entry: ReactTimeSyncSubscriptionEntry) => () => void;
6044
getSelectionSnapshot: <T = unknown>(id: string) => T;
@@ -65,9 +49,9 @@ class ReactTimeSync implements ReactTimeSyncApi {
6549
readonly #resyncOnNewSubscription: boolean;
6650
readonly #selectionCache: Map<string, unknown>;
6751

68-
constructor(options: Partial<ReactTimeSyncInitOptions>) {
52+
constructor(options: Partial<TimeSyncInitOptions>) {
6953
const {
70-
resyncOnNewSubscription = defaultReactTimeSyncOptions.resyncOnNewSubscription,
54+
resyncOnNewSubscription = defaultOptions.resyncOnNewSubscription,
7155
initialDatetime = defaultOptions.initialDatetime,
7256
createNewDatetime = defaultOptions.createNewDatetime,
7357
setInterval = defaultOptions.setInterval,
@@ -101,7 +85,7 @@ const timeSyncContext = createContext<ReactTimeSync | null>(null);
10185

10286
type TimeSyncProviderProps = Readonly<
10387
PropsWithChildren<{
104-
options?: Partial<ReactTimeSyncInitOptions>;
88+
options?: Partial<TimeSyncInitOptions>;
10589
}>
10690
>;
10791

@@ -122,7 +106,7 @@ export const TimeSyncProvider: FC<TimeSyncProviderProps> = ({
122106
// be treated like a pseudo-ref value, where its values can only be used in
123107
// very specific, React-approved ways
124108
const [readonlySync] = useState(
125-
() => new ReactTimeSync(options ?? defaultReactTimeSyncOptions),
109+
() => new ReactTimeSync(options ?? defaultOptions),
126110
);
127111

128112
return (

site/src/utils/TimeSync.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* @todo What's left to do here:
33
* 1. Fill out all incomplete methods
4-
* 2. Add tests
4+
* 2. Make sure the class respects the resyncOnNewSubscription option
5+
* 3. Add tests
56
*/
67
export const IDEAL_REFRESH_ONE_SECOND = 1_000;
78
export const IDEAL_REFRESH_ONE_MINUTE = 60 * 1_000;
@@ -35,10 +36,17 @@ export type TimeSyncInitOptions = Readonly<{
3536
* new interval to increase/decrease its update speed.)
3637
*/
3738
clearInterval: ClearInterval;
39+
40+
/**
41+
* Configures whether adding a new subscription will immediately create
42+
* a new time snapshot and use it to update all other subscriptions.
43+
*/
44+
resyncOnNewSubscription: boolean;
3845
}>;
3946

4047
export const defaultOptions: TimeSyncInitOptions = {
4148
initialDatetime: new Date(),
49+
resyncOnNewSubscription: true,
4250
createNewDatetime: () => new Date(),
4351
setInterval: window.setInterval,
4452
clearInterval: window.clearInterval,
@@ -83,6 +91,7 @@ interface TimeSyncApi {
8391
* composing your components to minimize the costs of re-renders.
8492
*/
8593
export class TimeSync implements TimeSyncApi {
94+
readonly #resyncOnNewSubscription: boolean;
8695
readonly #createNewDatetime: (prev: Date) => Date;
8796
readonly #setInterval: SetInterval;
8897
readonly #clearInterval: ClearInterval;
@@ -94,6 +103,7 @@ export class TimeSync implements TimeSyncApi {
94103
constructor(options: Partial<TimeSyncInitOptions>) {
95104
const {
96105
initialDatetime = defaultOptions.initialDatetime,
106+
resyncOnNewSubscription = defaultOptions.resyncOnNewSubscription,
97107
createNewDatetime = defaultOptions.createNewDatetime,
98108
setInterval = defaultOptions.setInterval,
99109
clearInterval = defaultOptions.clearInterval,
@@ -102,6 +112,7 @@ export class TimeSync implements TimeSyncApi {
102112
this.#setInterval = setInterval;
103113
this.#clearInterval = clearInterval;
104114
this.#createNewDatetime = createNewDatetime;
115+
this.#resyncOnNewSubscription = resyncOnNewSubscription;
105116

106117
this.#latestDateSnapshot = initialDatetime;
107118
this.#subscriptions = [];

0 commit comments

Comments
 (0)