11import isPlainObject from 'is-plain-obj'
2+ import { IS_PROXY , VALUE } from 'proxy-state-tree'
23
34import { Derived } from './derived'
45import { RehydrateClasses } from './internalTypes'
@@ -12,6 +13,7 @@ export const EXECUTION = Symbol('execution')
1213export const MODE_DEFAULT = Symbol ( 'MODE_DEFAULT' )
1314export const MODE_TEST = Symbol ( 'MODE_TEST' )
1415export const MODE_SSR = Symbol ( 'MODE_SSR' )
16+ export const IS_JSON = Symbol ( 'IS_JSON' )
1517
1618export class MockedEventEmitter {
1719 emit ( ) { }
@@ -21,6 +23,13 @@ export class MockedEventEmitter {
2123 addListener ( ) { }
2224}
2325
26+ export const json = ( obj : any ) => {
27+ const result = deepCopy ( obj && obj [ IS_PROXY ] ? obj [ VALUE ] : obj )
28+ result [ IS_JSON ] = obj
29+ return result
30+ }
31+
32+
2433export function isPromise ( maybePromise : any ) {
2534 return (
2635 maybePromise instanceof Promise ||
@@ -164,15 +173,16 @@ export function rehydrateState<T extends IState>(target: T, source: IState, clas
164173 Object . keys ( source ) . forEach ( ( key ) => {
165174 const value = source [ key ]
166175 const classInstance = classes [ key ]
167- if ( typeof value === 'object' && ! Array . isArray ( value ) && value !== null ) {
168- if ( ! target [ key ] ) target [ key ] = { }
169- rehydrateState ( target [ key ] as IState , source [ key ] as IState , classes [ key ] )
170- } else if ( classInstance && Array . isArray ( source [ key ] ) ) {
176+
177+ if ( typeof classInstance === 'function' && Array . isArray ( source [ key ] ) ) {
171178 target [ key ] = ( source [ key ] as any [ ] ) . map ( value => classInstance ( value ) )
172- } else if ( classInstance ) {
179+ } else if ( typeof classInstance === 'function' ) {
173180 target [ key ] = classInstance ( source [ key ] )
181+ } else if ( typeof value === 'object' && ! Array . isArray ( value ) && value !== null ) {
182+ if ( ! target [ key ] ) target [ key ] = { }
183+ rehydrateState ( target [ key ] as IState , source [ key ] as IState , classes [ key ] )
174184 } else {
175185 target [ key ] = source [ key ]
176186 }
177187 } )
178- }
188+ }
0 commit comments