Skip to content

Commit 4ec9191

Browse files
committed
refactor(strict-mode): refactor strict mode with mutative
1 parent 10c4129 commit 4ec9191

File tree

9 files changed

+41
-17
lines changed

9 files changed

+41
-17
lines changed

packages/reactant-model/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
],
2222
"license": "MIT",
2323
"peerDependencies": {
24-
"mutative": "^0.4.1",
24+
"mutative": "^0.4.2",
2525
"reactant-module": "*"
2626
},
2727
"devDependencies": {
28-
"mutative": "^0.4.1",
28+
"mutative": "^0.4.2",
2929
"reactant-module": "^0.71.0"
3030
}
3131
}

packages/reactant-model/src/model.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { create, Patch, Immutable } from 'mutative';
1+
import { create, Immutable } from 'mutative';
22
import {
33
storeKey,
44
Service,
@@ -8,6 +8,8 @@ import {
88
enableAutoFreezeKey,
99
identifierKey,
1010
nameKey,
11+
strictKey,
12+
Patch,
1113
} from 'reactant-module';
1214

1315
interface Scheme<S, A> {
@@ -32,8 +34,8 @@ export const model = <
3234
Object.assign(scheme.actions, {
3335
[key]: (...args: any[]) => {
3436
let state: Immutable<S> | S | undefined;
35-
let patches: Patch<true>[] | undefined;
36-
let inversePatches: Patch<true>[] | undefined;
37+
let patches: Patch[] | undefined;
38+
let inversePatches: Patch[] | undefined;
3739
if (module[enablePatchesKey]) {
3840
[state, patches, inversePatches] = create(
3941
module[stateKey] as S,
@@ -42,6 +44,7 @@ export const model = <
4244
},
4345
{
4446
enablePatches: true,
47+
strict: module[strictKey],
4548
enableAutoFreeze: module[enableAutoFreezeKey],
4649
}
4750
);
@@ -52,6 +55,7 @@ export const model = <
5255
fn(...args)(draftState);
5356
},
5457
{
58+
strict: module[strictKey],
5559
enableAutoFreeze: module[enableAutoFreezeKey],
5660
}
5761
);

packages/reactant-module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"license": "MIT",
2828
"dependencies": {
29-
"mutative": "^0.4.1",
29+
"mutative": "^0.4.2",
3030
"reactant-di": "^0.71.0",
3131
"redux": "^4.1.0"
3232
},

packages/reactant-module/src/constants/reduxKeys.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export const stateKey: unique symbol = Symbol('state');
66
export const defaultStateKey: unique symbol = Symbol('defaultState');
77
export const enablePatchesKey: unique symbol = Symbol('enablePatches');
88
export const enableAutoFreezeKey: unique symbol = Symbol('enableAutoFreeze');
9+
export const strictKey: unique symbol = Symbol('strict');
910
export const enableInspectorKey: unique symbol = Symbol('enableInspector');
1011
export const actionIdentifier = 'REACTANT_ACTION' as const;

packages/reactant-module/src/core/createStore.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
subscriptionsKey,
3838
enableInspectorKey,
3939
dynamicModulesKey,
40+
strictKey,
4041
} from '../constants';
4142
import { getStagedState } from '../decorators';
4243
import type {
@@ -91,6 +92,7 @@ export function createStore<T = any>({
9192
const enableReduxDevTools = devOptions.reduxDevTools ?? __DEV__;
9293
const enablePatches = devOptions.enablePatches ?? false;
9394
const enableInspector = devOptions.enableInspector ?? false;
95+
const strict = devOptions.strict ?? false;
9496

9597
dynamicModules.forEach((module, key) => {
9698
try {
@@ -314,6 +316,12 @@ export function createStore<T = any>({
314316
load(...args);
315317
},
316318
},
319+
// use strict mode for mutative
320+
[strictKey]: {
321+
enumerable: false,
322+
configurable: false,
323+
value: strict,
324+
},
317325
// enableAutoFreeze options for mutative
318326
[enableAutoFreezeKey]: {
319327
enumerable: false,
@@ -326,6 +334,7 @@ export function createStore<T = any>({
326334
configurable: false,
327335
value: enablePatches,
328336
},
337+
329338
// enableInspector options for state changing check before dispatching
330339
[enableInspectorKey]: {
331340
enumerable: false,

packages/reactant-module/src/decorators/action.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* eslint-disable no-console */
22
/* eslint-disable func-names */
3-
import { create, Patch } from 'mutative';
4-
import { ReactantAction, Service } from '../interfaces';
3+
import { create } from 'mutative';
4+
import { Patch, ReactantAction, Service } from '../interfaces';
55
import {
66
storeKey,
77
actionIdentifier,
88
enablePatchesKey,
99
enableAutoFreezeKey,
1010
identifierKey,
1111
enableInspectorKey,
12+
strictKey,
1213
} from '../constants';
1314

1415
let stagedState: Record<string, unknown> | undefined;
@@ -82,6 +83,7 @@ const action = (
8283
},
8384
{
8485
enablePatches: true,
86+
strict: this[strictKey],
8587
enableAutoFreeze: this[enableAutoFreezeKey],
8688
}
8789
);
@@ -98,6 +100,7 @@ const action = (
98100
}
99101
},
100102
{
103+
strict: this[strictKey],
101104
enableAutoFreeze: this[enableAutoFreezeKey],
102105
}
103106
);

packages/reactant-module/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { apply as applyPatches } from 'mutative';
1+
export { apply as applyPatches, current, original, unsafe } from 'mutative';
2+
23
export {
34
inject,
45
optional,

packages/reactant-module/src/interfaces.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
Unsubscribe,
1010
ReducersMapObject,
1111
} from 'redux';
12-
import type { Patch } from 'mutative';
12+
import type { Patch as IPatch, Patches as IPatches } from 'mutative';
1313
import type { EnhancerOptions } from 'redux-devtools-extension';
1414
import type {
1515
Container,
@@ -32,12 +32,17 @@ import {
3232
initStateKey,
3333
unsubscriptionsKey,
3434
enableInspectorKey,
35+
strictKey,
3536
} from './constants';
3637
import { PluginModule } from './core';
3738

39+
export type Patch = IPatch<true>;
40+
41+
export type Patches = IPatches<true>;
42+
3843
export interface DevOptions {
3944
/**
40-
* Enable react strict mode.
45+
* Enable strict mode for React and Mutative.
4146
*/
4247
strict?: boolean;
4348
/**
@@ -78,6 +83,7 @@ export interface Service<T extends Record<string, any> = Record<string, any>> {
7883
readonly [loaderKey]?: Loader;
7984
readonly [enablePatchesKey]?: boolean;
8085
readonly [enableAutoFreezeKey]?: boolean;
86+
readonly [strictKey]?: boolean;
8187
readonly [enableInspectorKey]?: boolean;
8288
readonly [subscriptionsKey]?: Subscriptions;
8389
readonly [unsubscriptionsKey]?: Set<Unsubscribe>;
@@ -103,8 +109,8 @@ export interface ReactantAction<T = any> extends Action<string | symbol> {
103109
state: Record<string, T>;
104110
params: any[];
105111
_reactant: typeof actionIdentifier;
106-
_patches?: Patch<true>[];
107-
_inversePatches?: Patch<true>[];
112+
_patches?: Patch[];
113+
_inversePatches?: Patch[];
108114
}
109115

110116
export type StateMapObject<T extends Record<string, Function>> = {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9076,10 +9076,10 @@ multimatch@^5.0.0:
90769076
arrify "^2.0.1"
90779077
minimatch "^3.0.4"
90789078

9079-
mutative@^0.4.1:
9080-
version "0.4.1"
9081-
resolved "https://registry.yarnpkg.com/mutative/-/mutative-0.4.1.tgz#7e71eb01ec24e5a8b031079588d5455fa16786e7"
9082-
integrity sha512-V3z99IFyMfmGoEFoYWXgyEzPis3nsqu8SceQFgR2dKm+Rzn8lQBle/VjPF9sEh1xABxgJvCicQ82q0iwdj4E+A==
9079+
mutative@^0.4.2:
9080+
version "0.4.2"
9081+
resolved "https://registry.yarnpkg.com/mutative/-/mutative-0.4.2.tgz#dacdae80c32f09b380979cfc534c4274f5a46079"
9082+
integrity sha512-zAWi/R6mShx7Dr6SnT61Z2Wf2TKVbAVGPzoS8vbr/fK5X0e7hD4pglpLSCWNtA0jh3RoS0QsdjJCywbQpwef5g==
90839083

90849084
mute-stream@0.0.7:
90859085
version "0.0.7"

0 commit comments

Comments
 (0)