mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 01:43:43 +00:00
d34280c03d
* batch updates in client * rearange updating logic * utilety type * guard undefinde in affectsLoaded * fix: pause state not saved to restore point
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import isEqual from 'react-fast-compare';
|
|
import { RuntimeStore, runtimeStorePlaceholder } from 'ontime-types';
|
|
import { createWithEqualityFn, useStoreWithEqualityFn } from 'zustand/traditional';
|
|
|
|
const deepCompare = <T>(a: T, b: T) => isEqual(a, b);
|
|
|
|
export const runtimeStore = createWithEqualityFn<RuntimeStore>(
|
|
() => ({
|
|
...runtimeStorePlaceholder,
|
|
}),
|
|
deepCompare,
|
|
);
|
|
|
|
export const useRuntimeStore = <T>(selector: (state: RuntimeStore) => T) =>
|
|
useStoreWithEqualityFn(runtimeStore, selector, deepCompare);
|
|
|
|
let batchStore: Partial<RuntimeStore> = {};
|
|
|
|
export function addToBatchUpdates<K extends keyof RuntimeStore>(key: K, value: RuntimeStore[K]) {
|
|
batchStore[key] = value;
|
|
}
|
|
|
|
export function flushBatchUpdates() {
|
|
const state = runtimeStore.getState();
|
|
runtimeStore.setState({ ...state, ...batchStore });
|
|
batchStore = {};
|
|
}
|
|
|
|
/**
|
|
* Allows patching a property of the runtime store
|
|
*/
|
|
export function patchRuntimeProperty<K extends keyof RuntimeStore>(key: K, value: RuntimeStore[K]) {
|
|
const state = runtimeStore.getState();
|
|
state[key] = value;
|
|
runtimeStore.setState({ ...state });
|
|
}
|
|
|
|
/**
|
|
* Allows patching the entire runtime store
|
|
*/
|
|
export function patchRuntime(patch: Partial<RuntimeStore>) {
|
|
const state = runtimeStore.getState();
|
|
runtimeStore.setState({ ...state, ...patch });
|
|
}
|