11/* eslint-disable no-console */
22/* eslint-disable func-names */
3- import { produce , produceWithPatches , Patch } from 'immer ' ;
3+ import { create , Patch } from 'mutative ' ;
44import { ReactantAction , Service } from '../interfaces' ;
55import {
66 storeKey ,
77 actionIdentifier ,
88 enablePatchesKey ,
9+ enableAutoFreezeKey ,
910 identifierKey ,
1011 enableInspectorKey ,
1112} from '../constants' ;
@@ -61,38 +62,45 @@ const action = (
6162 } ' decorated by '@action' must be bound to the current class instance.`
6263 ) ;
6364 }
64- let time : number ;
65- if ( __DEV__ ) {
66- time = Date . now ( ) ;
67- }
6865 if ( typeof stagedState === 'undefined' ) {
6966 try {
7067 const lastState = this [ storeKey ] ?. getState ( ) ;
7168 let state : Record < string , unknown > ;
7269 let patches : Patch [ ] | undefined ;
7370 let inversePatches : Patch [ ] | undefined ;
7471 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 ] ,
8486 }
85- } ) ;
87+ ) ;
8688 } 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 ] ,
94102 }
95- } ) ;
103+ ) ;
96104 }
97105 stagedState = undefined ;
98106 if ( __DEV__ ) {
@@ -104,13 +112,6 @@ const action = (
104112 `There are no state updates to method '${ methodName } '`
105113 ) ;
106114 }
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
114115 }
115116 this [ storeKey ] ! . dispatch < ReactantAction > ( {
116117 type : this [ identifierKey ] ! ,
0 commit comments