mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-17 05:13:32 +00:00
1849b4d39f
* chore: migrate eslint to oxlint * chore: migrate prettier to oxfmt * chore: migrate typescript * chore: toThrow should have a expected value * chore: cast test value as Day * chore: small title fix * chore: mocks should be hoisted * chore: incorrect async useage * chore: test should be inside description * chore: test sohuld include an expeced * chore: oxfmt --------- Co-authored-by: alex-Arc <omnivox@LAPTOP-RC5SNBVV.localdomain>
33 lines
994 B
TypeScript
33 lines
994 B
TypeScript
import { RuntimeStore, runtimeStorePlaceholder } from 'ontime-types';
|
|
import isEqual from 'react-fast-compare';
|
|
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);
|
|
|
|
/**
|
|
* 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 });
|
|
}
|