@@ -5,7 +5,12 @@ import type {
5
5
StoreApi ,
6
6
} from 'zustand' ;
7
7
import { createVanillaTemporal } from './temporal' ;
8
- import type { TemporalState , Write , ZundoOptions } from './types' ;
8
+ import type {
9
+ TemporalState ,
10
+ TemporalStateWithInternals ,
11
+ Write ,
12
+ ZundoOptions ,
13
+ } from './types' ;
9
14
10
15
type Zundo = <
11
16
TState ,
@@ -27,51 +32,61 @@ declare module 'zustand/vanilla' {
27
32
}
28
33
}
29
34
30
- const zundoImpl =
31
- < TState > (
32
- config : StateCreator < TState , [ ] , [ ] > ,
33
- {
34
- partialize = ( state : TState ) => state ,
35
- handleSet : userlandSetFactory = ( handleSetCb ) => handleSetCb ,
36
- ... restOptions
37
- } = { } as ZundoOptions < TState > ,
38
- ) : StateCreator < TState , [ ] , [ ] > =>
39
- ( set , get , _store ) => {
40
- type TState = ReturnType < typeof config > ;
41
- type StoreAddition = StoreApi < TemporalState < TState > > ;
42
-
43
- const temporalStore = createVanillaTemporal < TState > ( set , get , partialize , restOptions ) ;
44
-
45
- const store = _store as Mutate <
46
- StoreApi < TState > ,
47
- [ [ 'temporal' , StoreAddition ] ]
48
- > ;
49
- const setState = store . setState ;
50
-
51
- // TODO: should temporal be only temporalStore.getState()?
52
- // We can hide the rest of the store in the secret internals.
53
- store . temporal = temporalStore ;
35
+ const zundoImpl = < TState > (
36
+ config : StateCreator < TState , [ ] , [ ] > ,
37
+ {
38
+ partialize = ( state : TState ) => state ,
39
+ handleSet = ( handleSetCb ) => handleSetCb ,
40
+ ... restOptions
41
+ } = { } as ZundoOptions < TState > ,
42
+ ) : StateCreator < TState , [ ] , [ ] > => {
43
+ type StoreAddition = StoreApi < TemporalState < TState > > ;
44
+ type StoreWithAddition = Mutate <
45
+ StoreApi < TState > ,
46
+ [ [ 'temporal' , StoreAddition ] ]
47
+ > ;
48
+ const configWithTemporal = (
49
+ set : StoreApi < TState > [ 'setState' ] ,
50
+ get : StoreApi < TState > [ 'getState' ] ,
51
+ store : StoreWithAddition ,
52
+ ) => {
53
+ store . temporal = createVanillaTemporal < TState > (
54
+ set ,
55
+ get ,
56
+ partialize ,
57
+ restOptions ,
58
+ ) ;
54
59
55
- const curriedUserLandSet = userlandSetFactory (
56
- temporalStore . getState ( ) . __handleUserSet ,
60
+ const curriedHandleSet = handleSet (
61
+ ( store . temporal . getState ( ) as TemporalStateWithInternals < TState > )
62
+ . __handleSet ,
57
63
) ;
58
64
59
- const modifiedSetState : typeof setState = ( state , replace ) => {
65
+ const setState = store . setState ;
66
+ // Modify the setState function to call the userlandSet function
67
+ store . setState = ( state , replace ) => {
68
+ // Get most up to date state. The state from the callback might be a partial state.
69
+ // The order of the get() and set() calls is important here.
60
70
const pastState = partialize ( get ( ) ) ;
61
71
setState ( state , replace ) ;
62
- curriedUserLandSet ( pastState ) ;
72
+ curriedHandleSet ( pastState ) ;
63
73
} ;
64
- store . setState = modifiedSetState ;
65
74
66
- const modifiedSetter : typeof set = ( state , replace ) => {
67
- // Get most up to date state. Should this be the same as the state in the callback?
68
- const pastState = partialize ( get ( ) ) ;
69
- set ( state , replace ) ;
70
- curriedUserLandSet ( pastState ) ;
71
- } ;
72
-
73
- return config ( modifiedSetter , get , _store ) ;
75
+ return config (
76
+ // Modify the set function to call the userlandSet function
77
+ ( state , replace ) => {
78
+ // Get most up to date state. The state from the callback might be a partial state.
79
+ // The order of the get() and set() calls is important here.
80
+ const pastState = partialize ( get ( ) ) ;
81
+ set ( state , replace ) ;
82
+ curriedHandleSet ( pastState ) ;
83
+ } ,
84
+ get ,
85
+ store ,
86
+ ) ;
74
87
} ;
88
+ return configWithTemporal as StateCreator < TState , [ ] , [ ] > ;
89
+ } ;
75
90
76
91
export const temporal = zundoImpl as unknown as Zundo ;
77
92
export type { ZundoOptions , Zundo , TemporalState } ;
0 commit comments