import isEqual from 'react-fast-compare'; import { RuntimeStore, runtimeStorePlaceholder } from 'ontime-types'; import { createWithEqualityFn, useStoreWithEqualityFn } from 'zustand/traditional'; const deepCompare = (a: T, b: T) => isEqual(a, b); export const runtimeStore = createWithEqualityFn( () => ({ ...runtimeStorePlaceholder, }), deepCompare, ); export const useRuntimeStore = (selector: (state: RuntimeStore) => T) => useStoreWithEqualityFn(runtimeStore, selector, deepCompare); let batchStore: Partial = {}; export function addToBatchUpdates(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(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) { const state = runtimeStore.getState(); runtimeStore.setState({ ...state, ...patch }); }