Skip to content

Commit c1b3be7

Browse files
committed
fix(immer): remove the direct manipulation of global state
1 parent 0a3904e commit c1b3be7

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/helper.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,9 @@ const getInitialState = async <T extends { modelName: string }>(
9898
if (config && config.isServer) {
9999
ServerState[modelName] = asyncState
100100
} else {
101-
Global.State[modelName] = {
102-
...Global.State[modelName],
103-
...asyncState
104-
}
101+
Global.State = produce(Global.State, (s) => {
102+
s[modelName] = { ...s[modelName], ...asyncState }
103+
})
105104
}
106105
}
107106
})

src/index.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference path="./index.d.ts" />
2+
import produce from 'immer'
23
import * as React from 'react'
34
import { PureComponent, useEffect, useState, useRef } from 'react'
45
import Global from './global'
@@ -35,7 +36,9 @@ function Model<M extends Models, MT extends ModelType, E>(
3536
if (isModelType(models)) {
3637
Global.uid += 1
3738
const hash = '__' + Global.uid
38-
Global.State[hash] = models.state
39+
Global.State = produce(Global.State, (s) => {
40+
s[hash] = models.state
41+
})
3942
if (models.middlewares) {
4043
Global.Middlewares[hash] = models.middlewares
4144
}
@@ -77,22 +80,30 @@ function Model<M extends Models, MT extends ModelType, E>(
7780
} as any
7881
}
7982
if (initialState && !initialState.__FROM_SERVER__) {
80-
Global.State = initialState || {}
83+
Global.State = produce(Global.State, (s) => {
84+
Object.assign(s, initialState || {})
85+
})
8186
}
8287
extContext && (Global.Context['__global'] = extContext)
8388
Object.entries(models).forEach(([name, model]) => {
8489
if (model.__ERROR__) {
8590
// Fallback State and Actions when model schema is invalid
8691
console.error(name + " model's schema is invalid")
87-
Global.State[name] = {}
92+
Global.State = produce(Global.State, (s) => {
93+
s[name] = {}
94+
})
8895
Global.Actions[name] = {}
8996
return
9097
}
9198
if (!isAPI(model)) {
9299
if (initialState && initialState.__FROM_SERVER__) {
93-
Global.State[name] = { ...model.state, ...initialState[name] }
100+
Global.State = produce(Global.State, (s) => {
101+
s[name] = { ...model.state, ...initialState[name] }
102+
})
94103
} else if (!Global.State[name]) {
95-
Global.State[name] = model.state
104+
Global.State = produce(Global.State, (s) => {
105+
s[name] = model.state
106+
})
96107
}
97108
if (model.middlewares) {
98109
Global.Middlewares[name] = model.middlewares
@@ -102,13 +113,14 @@ function Model<M extends Models, MT extends ModelType, E>(
102113
} else {
103114
// If you develop on SSR mode, hot reload will still keep the old Global reference, so initialState won't change unless you restart the dev server
104115
if (!Global.State[name] || !initialState) {
105-
Global.State[name] = Global.State[model.__id]
116+
Global.State = produce(Global.State, (s) => {
117+
s[name] = s[model.__id]
118+
})
106119
}
107120
if (initialState && initialState.__FROM_SERVER__) {
108-
Global.State[name] = {
109-
...Global.State[model.__id],
110-
...initialState[name]
111-
}
121+
Global.State = produce(Global.State, (s) => {
122+
s[name] = { ...s[model.__id], ...initialState[name] }
123+
})
112124
}
113125
Global.Actions[name] = Global.Actions[model.__id]
114126
Global.AsyncState[name] = Global.AsyncState[model.__id]

0 commit comments

Comments
 (0)