-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo.ts
268 lines (254 loc) · 8.08 KB
/
do.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import * as React from 'react';
import * as memize from 'memize';
import createCache from './cache';
import { clearUndef, isPlain, Root, shallowEqual, select } from './utils';
class Runner {
selectors;
map;
getState;
setState;
cache = createCache();
mounted = false;
unmount;
capture: null | { pushed: any; callbacks: any } = null;
active: null | {
props: any;
state: { watchers: any; pushed: any; callbacks: any };
} = null;
constructor(selectors, map, getState, setState) {
this.selectors = selectors;
this.map = map;
this.getState = getState;
this.setState = setState;
}
createWatcher(selectors, map, extra?) {
const memoizedMap = memize(
args => {
this.capture = { pushed: {}, callbacks: [] };
const value = map.apply(null, args);
if (typeof value === 'function') value();
else if (value) Object.assign(this.capture.pushed, value);
const result = this.capture;
this.capture = null;
return result;
},
{ maxSize: 50 },
);
return {
getArgs: () =>
selectors
.map(s => select(s, this.active!.props, this.active!.state.pushed))
.concat(extra || []),
run: (args, commit) => {
if (commit) {
const value = map.apply(null, args.concat(true));
if (typeof value === 'function') return value;
if (value) {
Object.assign(this.active!.state.pushed, this.cache(value, true));
}
} else {
const { pushed, callbacks } = memoizedMap(args);
Object.assign(this.active!.state.pushed, this.cache(pushed, true));
this.active!.state.callbacks.push(...callbacks);
}
},
};
}
runWatcher(watcher, commit) {
const lastArgs = watcher[commit ? 'commitArgs' : 'renderArgs'];
const args = (commit && watcher.renderArgs) || watcher.getArgs();
if (!lastArgs || args.some((a, i) => a !== lastArgs[i])) {
if (commit && watcher.stop) watcher.stop();
const stop = watcher.run(args, commit);
if (!commit) {
if (stop) stop();
return { ...watcher, renderArgs: args };
}
return { ...watcher, commitArgs: args, renderArgs: args, stop };
}
return watcher;
}
runLayer(commit) {
const prev = { ...this.active!.state.pushed };
this.active!.state.watchers = this.active!.state.watchers.map(w =>
this.runWatcher(w, commit),
);
if (!shallowEqual(this.active!.state.pushed, prev)) this.runLayer(commit);
}
run(props, state, func?, commit?) {
this.active = {
props,
state: {
watchers: state.watchers || [],
pushed: { ...(state.pushed || {}) },
callbacks: [...(state.callbacks || [])],
},
};
if (func) func();
this.runLayer(commit);
const result = this.active.state;
this.active = null;
Object.keys(result).forEach(k => {
if (state[k] && shallowEqual(result[k], state[k])) result[k] = state[k];
});
if (shallowEqual(result, state)) return null;
result.callbacks = result.callbacks.filter(c => !c.done);
return result;
}
push = (update, callback?) => {
if (this.capture) {
Object.assign(this.capture.pushed, update || {});
if (callback) this.capture.callbacks.push({ call: callback });
} else if (this.active) {
Object.assign(this.active.state.pushed, this.cache(update || {}, true));
if (callback) this.active.state.callbacks.push({ call: callback });
} else if (this.mounted) {
this.setState((props, state) =>
this.run(props, state, () => {
Object.assign(
this.active!.state.pushed,
this.cache(update || {}, true),
);
if (callback) this.active!.state.callbacks.push({ call: callback });
}),
);
}
};
load(props, state?) {
if (state) this.mounted = true;
return this.run(
props,
{ pushed: state && state.pushed },
() => {
let loaded = false;
const watch: any = (sels, m, extra?) => {
this.active!.state.watchers.push(
this.runWatcher(this.createWatcher(sels, m, extra), !!state),
);
};
if (this.selectors.length) {
watch(this.selectors, this.map, this.push);
} else {
const value = this.map(
(...sels) => {
const m = sels.pop();
if (!sels.length) {
if (this.capture) {
throw new Error('Get called in render');
}
if (this.active) {
return m ? this.active.state.pushed : this.active.props;
}
return this.getState(m);
}
if (loaded) throw new Error('Watch called after load');
watch(sels, m);
},
this.push,
!!state,
);
if (typeof value === 'function') {
if (state) this.unmount = value;
else value();
} else if (value) {
Object.assign(this.active!.state.pushed, this.cache(value, true));
}
}
loaded = true;
},
!!state,
);
}
}
export default (...selectors) => (C: any = Root) => {
const map = selectors.pop();
if ((!selectors.length && map.length < 2) || map.length <= selectors.length) {
let mapResult = selectors.length ? {} : null;
const maps: any[] = selectors.length ? [[selectors, map]] : [];
const globalMaps = {};
return class DoPure extends React.Component {
state = { maps: {}, cache: createCache(), pushed: null as {} | null };
static getDerivedStateFromProps(props, state) {
if (!mapResult) {
mapResult =
map((...sels) => {
const m = sels.pop();
if (!sels.length) throw new Error('Get called in pure do');
maps.push([sels, m]);
}) || {};
}
const next = { maps: {}, pushed: {} } as any;
maps.forEach(([sels, m], i) => {
const args = sels.map(s => select(s, props));
if (args.every(isPlain)) {
globalMaps[i] = globalMaps[i] || memize(m, { maxSize: 50 });
Object.assign(next.pushed, globalMaps[i].apply(null, args) || {});
} else {
next.maps[i] = state.maps[i] || memize(m, { maxSize: 10 });
Object.assign(next.pushed, next.maps[i].apply(null, args) || {});
}
});
Object.assign(next.pushed, mapResult);
next.pushed = state.cache(next.pushed);
return next;
}
render() {
return React.createElement(
C,
clearUndef({ ...this.props, ...this.state.pushed }),
);
}
};
}
return class Do extends React.Component<any, any> {
constructor(props) {
super(props);
const runner = new Runner(
selectors,
map,
v => (v ? this.state.state.pushed : this.props),
func => {
this.setState(({ state }) => {
const next = func(this.props, state);
return next ? { state: next } : null;
});
},
);
this.state = { runner, state: runner.load(this.props) };
}
static getDerivedStateFromProps(props, { runner, state }) {
const next = runner.run(props, state);
return next ? { state: next } : null;
}
componentDidMount() {
const { runner, state } = this.state;
this.setState({ state: runner.load(this.props, state) });
}
componentDidUpdate() {
const { runner, state } = this.state;
const next = runner.run(
this.props,
state,
() => {
state.callbacks.forEach(c => {
c.call();
c.done = true;
});
},
true,
);
if (next) this.setState({ state: next });
}
componentWillUnmount() {
const { runner, state } = this.state;
state.watchers.forEach(w => w.stop && w.stop());
if (runner.unmount) runner.unmount();
}
render() {
return React.createElement(
C,
clearUndef({ ...this.props, ...this.state.state.pushed }),
);
}
};
};