From 6ef04d25ad35ef1c52112ba50937ebc1b0434312 Mon Sep 17 00:00:00 2001 From: hilezir Date: Fri, 29 Jan 2021 19:12:47 +0800 Subject: [PATCH] chore(store): createStore, instead of redux in futures --- src/store/createStore.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/store/createStore.ts 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 +}