-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-sync-controller.ts
More file actions
92 lines (90 loc) · 2.94 KB
/
Copy pathconfig-sync-controller.ts
File metadata and controls
92 lines (90 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { createConfigSyncMutationRuntime } from "./config-sync/runtime.js";
import { createConfigRuleSyncRequester } from "./config-sync/rule-sync.js";
import { createConfigSpeedSyncScheduler } from "./config-sync/speed-sync.js";
import { createConfigSyncStateManager } from "./config-sync/state.js";
import type {
BrowserClearTimeout,
BrowserSetTimeout,
ConfigSyncController,
ConfigSyncViewState,
MutationRunner,
PostControlFunction,
SimulationMutations,
} from "./types/controller.js";
import type { SimulationSnapshot } from "./types/domain.js";
import type { AppState } from "./types/state.js";
export function createConfigSyncController({
state: _state,
mutationRunner,
simulationMutations = null,
postControl,
onError,
onSyncStateChanged = () => {},
applySimulationState,
refreshState,
setTimeoutFn = (callback, delay) => window.setTimeout(callback, delay),
clearTimeoutFn = (timerId) => window.clearTimeout(timerId),
speedDebounceMs = 200,
}: {
state: AppState;
mutationRunner: MutationRunner;
simulationMutations?: SimulationMutations | null;
postControl: PostControlFunction;
onError: (error: unknown) => void;
onSyncStateChanged?: (viewState: ConfigSyncViewState) => void;
applySimulationState: (
simulationState: SimulationSnapshot,
options?: { source?: string },
) => void;
refreshState: () => Promise<void>;
setTimeoutFn?: BrowserSetTimeout;
clearTimeoutFn?: BrowserClearTimeout;
speedDebounceMs?: number;
}): ConfigSyncController {
const stateManager = createConfigSyncStateManager(onSyncStateChanged);
const runtime = createConfigSyncMutationRuntime({
mutationRunner,
simulationMutations,
onError,
applySimulationState,
refreshState,
});
const speedSync = createConfigSpeedSyncScheduler({
stateManager,
runtime,
postControl,
setTimeoutFn,
clearTimeoutFn,
speedDebounceMs,
});
const ruleSync = createConfigRuleSyncRequester({
stateManager,
runtime,
postControl,
});
return {
dispose() {
speedSync.dispose();
},
getDisplaySpeed(fallbackSpeed: number) {
return stateManager.getDisplaySpeed(fallbackSpeed);
},
getViewState(): ConfigSyncViewState {
return stateManager.getViewState();
},
reconcile(simulationState: SimulationSnapshot) {
stateManager.reconcile(simulationState, {
clearScheduledSpeedSync: speedSync.clearScheduledSpeedSync,
});
},
requestRuleSync(nextRuleName, options) {
ruleSync.requestRuleSync(nextRuleName, options);
},
scheduleSpeedSync(nextSpeed) {
speedSync.scheduleSpeedSync(nextSpeed);
},
shouldAdoptBackendRule() {
return stateManager.shouldAdoptBackendRule();
},
};
}