-
Notifications
You must be signed in to change notification settings - Fork 14
/
useModel.ts
142 lines (119 loc) · 3.49 KB
/
useModel.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import mountModel from './mountModel';
import { combineSubscribe } from './subscribe';
import { Context, Model, UseModel } from '@/types';
import { getComputedDepModels, isModel } from '@/utils/misc';
function createUseModel(context: Context): UseModel {
function useModel(...args: any) {
const flattenedArgs = Array.isArray(args[0])
? [...args[0], ...args.slice(1)]
: args;
flattenedArgs.forEach(model => {
if (isModel(model)) {
mountModel(context, model);
}
});
const { getState, getActions, actualModels, subscribe } = parseModelParams(
context,
flattenedArgs,
);
const computedArr = actualModels.map(m => {
const {
modelDesc: { computed },
} = context.apis.getModel(m);
return computed;
});
const computedDepModels = getComputedDepModels(computedArr);
computedDepModels.forEach(model => {
if (isModel(model)) {
mountModel(context, model);
}
});
let [state, actions] = [getState(), getActions()];
({ state, actions } = context.pluginCore.invokePipeline(
'useModel',
{
state,
actions,
},
{
models: actualModels,
mountedModels: actualModels.map(model => context.apis.getModel(model)),
},
));
return [state, actions, subscribe];
}
return useModel as UseModel;
}
const parseModelParams = (context: Context, _models: any) => {
const models = Array.isArray(_models) ? _models : [_models];
const actualModels = [];
const selectors = [];
for (const model of models) {
if (isModel(model)) {
actualModels.push(model);
} else {
selectors.push(model);
}
}
const [stateSelector, actionSelector] = selectors;
if (actualModels.length > 1) {
actualModels.forEach(m => {
if (
Object.prototype.toString.call(context.apis.getModel(m).state) !==
'[object Object]'
) {
throw new Error(
`You cant use multiple model one of which's state is primitive data`,
);
}
});
}
const getStateWithComputed = (model: Model) => {
const {
state,
modelDesc: { computed },
} = context.apis.getModel(model);
let computedState: any;
if (computed) {
computedState = Object.keys(computed).reduce((curState, computedKey) => {
curState[computedKey] = state[computedKey];
return curState;
}, {});
// state reference always changes when model has computed properties
return { ...state, ...computedState };
}
return state;
};
const finalStateSelector = (...models: any[]) => {
if (stateSelector) {
return stateSelector(
...actualModels.map(model => getStateWithComputed(model)),
);
}
if (models.length === 1) {
return getStateWithComputed(models[0]);
}
return models.reduce(
(res, model) => ({ ...res, ...getStateWithComputed(model) }),
{},
);
};
const finalActionSelector =
actionSelector ||
((...actions: any[]) =>
actions.reduce((res, action) => Object.assign(res, action), {}));
return {
getState: () => finalStateSelector(...actualModels),
getActions: () =>
finalActionSelector(
...actualModels.map(model => context.apis.getModel(model)!.actions),
),
subscribe: (handler: () => void) =>
combineSubscribe(
context,
...actualModels.map(model => context.apis.getModelSubscribe(model)),
)(handler),
actualModels,
};
};
export { createUseModel };