Skip to content
Myr

redux.ts

This file

Path

src/middleware/redux.ts

50 lines · 1.3 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than f85898f point at it.

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 1–50why, one step back
1import type { StateCreator, StoreMutatorIdentifier } from '../vanilla.ts'
2import type { NamedSet } from './devtools.ts'34type Write<T, U> = Omit<T, keyof U> & U56type Action = { type: string }78type StoreRedux<A> = {9 dispatch: (a: A) => A10 dispatchFromDevtools: true11}1213type ReduxState<A> = {14 dispatch: StoreRedux<A>['dispatch']15}1617type WithRedux<S, A> = Write<S, StoreRedux<A>>1819type Redux = <20 T,21 A extends Action,22 Cms extends [StoreMutatorIdentifier, unknown][] = [],23>(24 reducer: (state: T, action: A) => T,25 initialState: T,26) => StateCreator<Write<T, ReduxState<A>>, Cms, [['zustand/redux', A]]>2728declare module '../vanilla' {29 interface StoreMutators<S, A> {30 'zustand/redux': WithRedux<S, A>31 }32}3334type ReduxImpl = <T, A extends Action>(35 reducer: (state: T, action: A) => T,36 initialState: T,37) => StateCreator<T & ReduxState<A>, [], []>3839const reduxImpl: ReduxImpl = (reducer, initial) => (set, _get, api) => {40 type S = typeof initial41 type A = Parameters<typeof reducer>[1]42 ;(api as any).dispatch = (action: A) => {43 ;(set as NamedSet<S>)((state: S) => reducer(state, action), false, action)44 return action45 }46 ;(api as any).dispatchFromDevtools = true4748 return { dispatch: (...args) => (api as any).dispatch(...args), ...initial }49}50export const redux = reduxImpl as unknown as Redux