File tree Expand file tree Collapse file tree 1 file changed +10
-17
lines changed Expand file tree Collapse file tree 1 file changed +10
-17
lines changed Original file line number Diff line number Diff line change
1
+ import createNextState , { Draft } from 'immer'
1
2
import { EntityState } from './models'
2
3
3
4
export enum DidMutate {
@@ -13,24 +14,16 @@ export function createStateOperator<V, R>(
13
14
mutator : ( arg : any , state : any ) => DidMutate
14
15
) : any {
15
16
return function operation < S extends EntityState < V > > ( arg : R , state : any ) : S {
16
- const clonedEntityState : EntityState < V > = {
17
- ids : [ ...state . ids ] ,
18
- entities : { ...state . entities }
19
- }
17
+ // @ts -ignore createNextState() produces an Immutable<Draft<S>> rather
18
+ // than an Immutable<S>, and TypeScript cannot find out how to reconcile
19
+ // these two types.
20
+ return createNextState ( state , ( draft : Draft < EntityState < V > > ) => {
21
+ const { ids : originalIds } = draft
22
+ const didMutate = mutator ( arg , draft )
20
23
21
- const didMutate = mutator ( arg , clonedEntityState )
22
-
23
- if ( didMutate === DidMutate . Both ) {
24
- return Object . assign ( { } , state , clonedEntityState )
25
- }
26
-
27
- if ( didMutate === DidMutate . EntitiesOnly ) {
28
- return {
29
- ...state ,
30
- entities : clonedEntityState . entities
24
+ if ( didMutate === DidMutate . EntitiesOnly ) {
25
+ draft . ids = originalIds
31
26
}
32
- }
33
-
34
- return state
27
+ } )
35
28
}
36
29
}
You can’t perform that action at this time.
0 commit comments