diff --git a/src/store/createStore.ts b/src/store/createStore.ts new file mode 100644 index 0000000..283ec88 --- /dev/null +++ b/src/store/createStore.ts @@ -0,0 +1,18 @@ +import produce, { Draft } from 'immer' +import create, { State, StateCreator } from 'zustand' + +const immerMiddleware: ImmerMiddleware = ( + config: StateCreator) => void) => void>, +): StateCreator => (set, get, api) => + config(fn => set(produce(fn) as (state: T) => T), get, api) + +/** Create a store of zustand */ +export const createStore = ( + config: ParametersHead>, +) => create(immerMiddleware(config)) + +interface ImmerMiddleware { + ( + config: StateCreator) => void) => void>, + ): StateCreator +}