-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhydrate.ts
More file actions
30 lines (23 loc) · 753 Bytes
/
hydrate.ts
File metadata and controls
30 lines (23 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @author Kuitos
* @homepage https://github.com/kuitos/
* @since 2018-06-26 15:43
*/
import { ObservableMap, runInAction } from 'mobx';
import { Constructor } from './meta';
export default function hydrate<T>(instance: T, Host: Constructor<T>, ...args: any[]) {
if (!(instance instanceof Host)) {
const real: any = new Host(...args);
// awake the reactive system of the model
Object.keys(instance).forEach((key: string) => {
if (real[key] instanceof ObservableMap) {
const { name, enhancer } = real[key];
runInAction(() => real[key] = new ObservableMap((instance as any)[key], enhancer, name));
} else {
runInAction(() => real[key] = (instance as any)[key]);
}
});
return real as T;
}
return instance;
}