@@ -27,54 +27,58 @@ declare module 'zustand/vanilla' {
2727 }
2828}
2929
30- type ZundoImpl = < TState > (
31- config : StateCreator < TState , [ ] , [ ] > ,
32- options : ZundoOptions < TState > ,
33- ) => StateCreator < TState , [ ] , [ ] > ;
30+ const zundoImpl =
31+ < TState > (
32+ config : StateCreator < TState , [ ] , [ ] > ,
33+ {
34+ partialize = ( state : TState ) => state ,
35+ equality,
36+ onSave,
37+ limit,
38+ handleSet : userlandSetFactory = ( handleSetCb ) => handleSetCb ,
39+ } = { } as ZundoOptions < TState > ,
40+ ) : StateCreator < TState , [ ] , [ ] > =>
41+ ( set , get , _store ) => {
42+ type TState = ReturnType < typeof config > ;
43+ type StoreAddition = StoreApi < TemporalState < TState > > ;
3444
35- const zundoImpl : ZundoImpl = ( config , baseOptions ) => ( set , get , _store ) => {
36- type TState = ReturnType < typeof config > ;
37- type StoreAddition = StoreApi < TemporalState < TState > > ;
45+ const temporalStore = createVanillaTemporal < TState > ( set , get , {
46+ partialize,
47+ equality,
48+ onSave,
49+ limit,
50+ } ) ;
3851
39- const options = {
40- partialize : ( state : TState ) => state ,
41- handleSet : ( handleSetCb : typeof set ) => handleSetCb ,
42- ...baseOptions ,
43- } ;
44- const { partialize, handleSet : userlandSetFactory } = options ;
45-
46- const temporalStore = createVanillaTemporal < TState > ( set , get , options ) ;
52+ const store = _store as Mutate <
53+ StoreApi < TState > ,
54+ [ [ 'temporal' , StoreAddition ] ]
55+ > ;
56+ const { setState } = store ;
4757
48- const store = _store as Mutate <
49- StoreApi < TState > ,
50- [ [ 'temporal' , StoreAddition ] ]
51- > ;
52- const { setState } = store ;
58+ // TODO: should temporal be only temporalStore.getState()?
59+ // We can hide the rest of the store in the secret internals.
60+ store . temporal = temporalStore ;
5361
54- // TODO: should temporal be only temporalStore.getState()?
55- // We can hide the rest of the store in the secret internals.
56- store . temporal = temporalStore ;
62+ const curriedUserLandSet = userlandSetFactory (
63+ temporalStore . getState ( ) . __internal . handleUserSet ,
64+ ) ;
5765
58- const curriedUserLandSet = userlandSetFactory (
59- temporalStore . getState ( ) . __internal . handleUserSet ,
60- ) ;
66+ const modifiedSetState : typeof setState = ( state , replace ) => {
67+ const pastState = partialize ( get ( ) ) ;
68+ setState ( state , replace ) ;
69+ curriedUserLandSet ( pastState ) ;
70+ } ;
71+ store . setState = modifiedSetState ;
6172
62- const modifiedSetState : typeof setState = ( state , replace ) => {
63- const pastState = partialize ( get ( ) ) ;
64- setState ( state , replace ) ;
65- curriedUserLandSet ( pastState ) ;
66- } ;
67- store . setState = modifiedSetState ;
73+ const modifiedSetter : typeof set = ( state , replace ) => {
74+ // Get most up to date state. Should this be the same as the state in the callback?
75+ const pastState = partialize ( get ( ) ) ;
76+ set ( state , replace ) ;
77+ curriedUserLandSet ( pastState ) ;
78+ } ;
6879
69- const modifiedSetter : typeof set = ( state , replace ) => {
70- // Get most up to date state. Should this be the same as the state in the callback?
71- const pastState = partialize ( get ( ) ) ;
72- set ( state , replace ) ;
73- curriedUserLandSet ( pastState ) ;
80+ return config ( modifiedSetter , get , _store ) ;
7481 } ;
7582
76- return config ( modifiedSetter , get , _store ) ;
77- } ;
78-
7983export const temporal = zundoImpl as unknown as Zundo ;
80- export type { ZundoOptions , Zundo , TemporalState } ;
84+ export type { ZundoOptions , Zundo , TemporalState } ;
0 commit comments