1
1
/* eslint-disable no-console */
2
2
/* eslint-disable func-names */
3
- import { produce , produceWithPatches , Patch } from 'immer ' ;
3
+ import { create , Patch } from 'mutative ' ;
4
4
import { ReactantAction , Service } from '../interfaces' ;
5
5
import {
6
6
storeKey ,
7
7
actionIdentifier ,
8
8
enablePatchesKey ,
9
+ enableAutoFreezeKey ,
9
10
identifierKey ,
10
11
enableInspectorKey ,
11
12
} from '../constants' ;
@@ -61,38 +62,45 @@ const action = (
61
62
} ' decorated by '@action' must be bound to the current class instance.`
62
63
) ;
63
64
}
64
- let time : number ;
65
- if ( __DEV__ ) {
66
- time = Date . now ( ) ;
67
- }
68
65
if ( typeof stagedState === 'undefined' ) {
69
66
try {
70
67
const lastState = this [ storeKey ] ?. getState ( ) ;
71
68
let state : Record < string , unknown > ;
72
69
let patches : Patch [ ] | undefined ;
73
70
let inversePatches : Patch [ ] | undefined ;
74
71
if ( this [ enablePatchesKey ] ) {
75
- [ state , patches , inversePatches ] = produceWithPatches <
76
- Record < string , unknown >
77
- > ( lastState , ( draftState ) => {
78
- stagedState = draftState ;
79
- const result = fn . apply ( this , args ) ;
80
- if ( __DEV__ && result !== undefined ) {
81
- throw new Error (
82
- `The return value of the method '${ key } ' is not allowed.`
83
- ) ;
72
+ [ state , patches , inversePatches ] = create (
73
+ lastState ,
74
+ ( draftState ) => {
75
+ stagedState = draftState ;
76
+ const result = fn . apply ( this , args ) ;
77
+ if ( __DEV__ && result !== undefined ) {
78
+ throw new Error (
79
+ `The return value of the method '${ key } ' is not allowed.`
80
+ ) ;
81
+ }
82
+ } ,
83
+ {
84
+ enablePatches : true ,
85
+ enableAutoFreeze : this [ enableAutoFreezeKey ] ,
84
86
}
85
- } ) ;
87
+ ) ;
86
88
} else {
87
- state = produce < Record < string , unknown > > ( lastState , ( draftState ) => {
88
- stagedState = draftState ;
89
- const result = fn . apply ( this , args ) ;
90
- if ( __DEV__ && result !== undefined ) {
91
- throw new Error (
92
- `The return value of the method '${ key } ' is not allowed.`
93
- ) ;
89
+ state = create (
90
+ lastState ,
91
+ ( draftState ) => {
92
+ stagedState = draftState ;
93
+ const result = fn . apply ( this , args ) ;
94
+ if ( __DEV__ && result !== undefined ) {
95
+ throw new Error (
96
+ `The return value of the method '${ key } ' is not allowed.`
97
+ ) ;
98
+ }
99
+ } ,
100
+ {
101
+ enableAutoFreeze : this [ enableAutoFreezeKey ] ,
94
102
}
95
- } ) ;
103
+ ) ;
96
104
}
97
105
stagedState = undefined ;
98
106
if ( __DEV__ ) {
@@ -104,13 +112,6 @@ const action = (
104
112
`There are no state updates to method '${ methodName } '`
105
113
) ;
106
114
}
107
- // performance checking
108
- const executionTime = Date . now ( ) - time ! ;
109
- if ( executionTime > 100 )
110
- console . warn (
111
- `The execution time of method '${ methodName } ' is ${ executionTime } ms, it's recommended to use 'dispatch()' API.`
112
- ) ;
113
- // performance detail: https://immerjs.github.io/immer/docs/performance
114
115
}
115
116
this [ storeKey ] ! . dispatch < ReactantAction > ( {
116
117
type : this [ identifierKey ] ! ,
0 commit comments