1
1
/// <reference path="./index.d.ts" />
2
+ import produce from 'immer'
2
3
import * as React from 'react'
3
4
import { PureComponent , useEffect , useState , useRef } from 'react'
4
5
import Global from './global'
@@ -35,7 +36,9 @@ function Model<M extends Models, MT extends ModelType, E>(
35
36
if ( isModelType ( models ) ) {
36
37
Global . uid += 1
37
38
const hash = '__' + Global . uid
38
- Global . State [ hash ] = models . state
39
+ Global . State = produce ( Global . State , ( s ) => {
40
+ s [ hash ] = models . state
41
+ } )
39
42
if ( models . middlewares ) {
40
43
Global . Middlewares [ hash ] = models . middlewares
41
44
}
@@ -77,22 +80,30 @@ function Model<M extends Models, MT extends ModelType, E>(
77
80
} as any
78
81
}
79
82
if ( initialState && ! initialState . __FROM_SERVER__ ) {
80
- Global . State = initialState || { }
83
+ Global . State = produce ( Global . State , ( s ) => {
84
+ Object . assign ( s , initialState || { } )
85
+ } )
81
86
}
82
87
extContext && ( Global . Context [ '__global' ] = extContext )
83
88
Object . entries ( models ) . forEach ( ( [ name , model ] ) => {
84
89
if ( model . __ERROR__ ) {
85
90
// Fallback State and Actions when model schema is invalid
86
91
console . error ( name + " model's schema is invalid" )
87
- Global . State [ name ] = { }
92
+ Global . State = produce ( Global . State , ( s ) => {
93
+ s [ name ] = { }
94
+ } )
88
95
Global . Actions [ name ] = { }
89
96
return
90
97
}
91
98
if ( ! isAPI ( model ) ) {
92
99
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
+ } )
94
103
} else if ( ! Global . State [ name ] ) {
95
- Global . State [ name ] = model . state
104
+ Global . State = produce ( Global . State , ( s ) => {
105
+ s [ name ] = model . state
106
+ } )
96
107
}
97
108
if ( model . middlewares ) {
98
109
Global . Middlewares [ name ] = model . middlewares
@@ -102,13 +113,14 @@ function Model<M extends Models, MT extends ModelType, E>(
102
113
} else {
103
114
// 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
104
115
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
+ } )
106
119
}
107
120
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
+ } )
112
124
}
113
125
Global . Actions [ name ] = Global . Actions [ model . __id ]
114
126
Global . AsyncState [ name ] = Global . AsyncState [ model . __id ]
0 commit comments