Skip to content

Commit

Permalink
chore(store): createStore, instead of redux in futures
Browse files Browse the repository at this point in the history
  • Loading branch information
aqzhyi committed Jan 29, 2021
1 parent 9be527a commit 6ef04d2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/store/createStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import produce, { Draft } from 'immer'
import create, { State, StateCreator } from 'zustand'

const immerMiddleware: ImmerMiddleware = <T extends State>(
config: StateCreator<T, (fn: (draft: Draft<T>) => void) => void>,
): StateCreator<T> => (set, get, api) =>
config(fn => set(produce(fn) as (state: T) => T), get, api)

/** Create a store of zustand */
export const createStore = <StoreStateAndAction extends State>(
config: ParametersHead<ImmerMiddleware<StoreStateAndAction>>,
) => create<StoreStateAndAction>(immerMiddleware(config))

interface ImmerMiddleware<T extends State = any> {
(
config: StateCreator<T, (fn: (draft: Draft<T>) => void) => void>,
): StateCreator<T>
}

0 comments on commit 6ef04d2

Please sign in to comment.