-
Notifications
You must be signed in to change notification settings - Fork 0
/
pure-frame.js
514 lines (449 loc) · 13.2 KB
/
pure-frame.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
const React = require('react');
const { useState, useEffect } = React;
const { fromJS, is, isMap, isList, Map } = require('immutable');
const { v4: uuid } = require('uuid');
/* # Common Utilities */
Object.fromEntries = Object.fromEntries || (pairs => pairs.reduce((o, [key, value]) => {
o[key] = value;
return o;
}, {}));
const isA = (o, type) => typeof(o) === type;
const isUndefined = o => isA(o, 'undefined');
const isString = o => isA(o, 'string');
const isFunction = o => isA(o, 'function');
const isArray = o => Array.isArray(o);
const isObject = o => !isArray(o) && isA(o, 'object');
const toJS = o => (isMap(o) || isList(o))? o.toJS(): o;
/* # Inner Data */
/**
* Cache the latest value of state, extractors and transformers.
*/
const snapshots = {
state: fromJS({}) // Preset Global State
};
/**
* Definitions of extractors and transformers.
*/
const formulas = {};
/**
* Relations between extractors and transformers.
*/
const dependencies = {
upstreams: {},
downstreams: {}
};
/**
* Actions and Reducers.
*/
const reducers = {};
/**
* Data Sources and Fetchers.
*/
const fetchers = {};
/**
* Effects and Performers.
*/
const performers = {};
/**
* Views.
*/
const views = {};
/* # Compute Management */
let computeIfAbsent = null; // pre-declare
/**
* Force compute the formula of `id`.
* The upstream formulas will be computed
* if and only if it has not cached.
*/
const compute = id => {
const upstreams = dependencies.upstreams[id] || [];
const params = upstreams.map(computeIfAbsent);
const formula = formulas[id];
return isFunction(formula)? formula(...params): null;
};
/**
* Compute the formula of `id` if and only if it has not cached.
* The result will be cached.
*/
computeIfAbsent = id => {
if (!(id in snapshots)) {
snapshots[id] = fromJS(compute(id));
}
return snapshots[id];
};
/**
* Define a formula.
* - id: the unique formula id.
* - upstreams: list of dependent formulas.
* - fn: operator.
*/
const defineFormula = (id, upstreams, fn) => {
formulas[id] = fn;
dependencies.upstreams[id] = upstreams;
upstreams.forEach(upstream => {
if (!(upstream in dependencies.downstreams)) {
dependencies.downstreams[upstream] = [];
}
dependencies.downstreams[upstream].push(id);
});
};
/**
* Unregister formula.
*/
const deleteFormula = id => {
delete formulas[id];
dependencies.upstreams[id].forEach(upstream => {
const downstreams = dependencies.downstreams[upstream];
const index = downstreams.indexOf(upstream);
if (index > -1) {
downstreams.splice(index, 1);
}
});
delete dependencies.upstreams[id];
};
/* # Snapshots */
/**
* reset the value of `id`.
* When the value changed, downstream formulas of `id` will be re-computed.
*/
const reset = (id, value) => {
const origin = snapshots[id];
const newValue = fromJS(value);
if (!is(origin, newValue)) {
snapshots[id] = newValue;
const downstreams = dependencies.downstreams[id] || [];
downstreams.filter(downstream => downstream in formulas)
.forEach(downstream => reset(downstream, compute(downstream)));
}
};
/**
* Get the value of `id`.
* Formulas will be computed if need.
*/
const deref = computeIfAbsent;
/* # Action Dispatch */
/**
* Dispatch `action` synchronized.
*/
const dispatchSync = action => {
const id = isList(action)? action.get(0): action[0];
if (!(id in reducers)) {
console.warn(`No reducer for action ${action}`);
return;
}
const chain = reducers[id];
let context = fromJS({ snapshots: {}, effects: {}, action });
chain.reduce((context, reducer) => reducer(context), context);
};
/**
* Dispatch `action` asynchronized after `ms` millseconds.
*/
const dispatchLater = (action, ms) => setTimeout(() => dispatchSync(action), ms);
/**
* Dispatch `action` asynchronized.
*/
const dispatch = action => dispatchLater(action, 0);
/**
* Generic Dispatch.
*/
const dispatchGeneric = (action, mode = 'async', ms = 0) => {
switch (mode) {
case 'sync':
return dispatchSync(action);
case 'later':
return dispatchLater(action, ms);
default:
return dispatch(action);
}
};
/* # Effect Performers */
/**
* Define effect performer.
*/
const definePerformer = (id, performer) => performers[id] = performer;
/**
* Preset effect performers.
*/
definePerformer('dispatch', dispatch);
definePerformer('dispatch-later', dispatchLater);
definePerformer('dispatch-sync', dispatchSync);
/* # Interceptors */
/**
* Preset interceptors.
*/
const standardInterceptors = [{
/**
* Inject state into snapshots of context.
*/
id: 'inject-state',
before: context => context.setIn(['snapshots', 'state'], deref('state'))
}, {
/**
* Perform effects:
* 1. Reset state if provided.
* 2. Perform ordered effects in fx.
*/
id: 'do-fx',
after: context => {
const state = context.getIn(['effects', 'state']);
if (isMap(state)) {
reset('state', state);
}
const fx = context.getIn(['effects', 'fx']);
if (isList(fx)) {
fx.filter(([effect]) => effect in performers)
.forEach(([effect, ...params]) => performers[effect](...params));
}
return context;
}
}];
/* # Data Sources and Fetchers */
/**
* Define data source and fetcher.
*/
const defineFetcher = (id, fetcher) => fetchers[id] = fetcher;
/**
* Execute data fetch.
*/
const dataFetch = (snapshots, id, params) =>
fetchers[id](snapshots, ...params.map(param => fromJS(param)));
/**
* Wrap dataSource fetcher to interceptor.
*/
const fetcher = (id, ...params) => ({
id,
before: context => {
const snapshot = dataFetch(context.get('snapshots'), id, params);
return context.setIn(['snapshots', id], fromJS(snapshot));
}
});
/**
* Fetch data synchronized.
* It's useful for initialize state.
* Returns JS type data.
*/
const fetch = (id, ...params) => {
const data = dataFetch(fromJS({
state: snapshots.state
}), id, params);
return toJS(data);
};
/* # Action Reducers */
/**
* Wrap action reducer to interceptor.
*/
const wrapReducerToInterceptor = (id, fn) => ({
id,
before: context => {
const snapshots = context.get('snapshots');
const action = context.get('action');
const effects = fn(snapshots, action);
return context.mergeDeep(fromJS({ effects }));
}
});
/**
* Define reducer.
* - id: action id.
* - interceptors: optional interceptors between standard interceptors and reducer.
* - reducer: reducer.
*/
const defineReducer = (id, interceptors, reducer) => {
if (isUndefined(reducer)) {
reducer = interceptors;
interceptors = [];
}
const chain = [...standardInterceptors,
...interceptors,
wrapReducerToInterceptor(id, reducer)];
reducers[id] = [];
chain.filter(interceptor => isFunction(interceptor.before))
.map(interceptor => interceptor.before)
.forEach(before => reducers[id].push(before));
chain.reverse()
.filter(interceptor => isFunction(interceptor.after))
.map(interceptor => interceptor.after)
.forEach(after => reducers[id].push(after));
};
/**
* Wrap state reducer to reducer.
*/
const wrapStateReducerToReducer = reducer => (coffects, action) => fromJS({ state: reducer(coffects.get('state'), action) });
/**
* Define state reducer.
* - id: action id.
* - reducer: state reducer.
*/
const defineStateReducer = (id, reducer) => defineReducer(id, [], wrapStateReducerToReducer(reducer));
/* # Query */
/**
* Define state extractor: extract the data from `state`.
* - id: formula id.
* - ...path: the data path in state.
*/
const defineExtractor = (id, ...path) => defineFormula(id, ['state'], state => state.getIn(path));
/**
* Define transformer: deriving data of extractor or transformer.
* - id: formula id.
* - upstreams: id list of dependent formulas.
* - fn: operator.
*/
const defineTransformer = defineFormula;
/* # View */
/**
* Generates dispatchers with action declares.
*/
const dispatchersOf = actions => Object.fromEntries(
Object.entries(actions)
.map(([prop, action]) =>
[prop, isString(action)? { id: action }: action])
.map(([prop, { id, mode = 'async', ms = 0 }]) =>
[prop, (...params) => dispatchGeneric([id, ...params], mode, ms)]));
/**
* Define view: connect formulas and action with pure function view.
* - id: optional view id.
* - options:
* - injects: map of watching formulas.
* key is formula id.
* value is prop name of component, the value of formula.
* - actions: map of dispatching actions.
* key is prop name of component, the function to dispatch action.
* value is string action id, or object {
* id: string, // action id.
* mode: 'async' | 'sync' | 'later', // 'async' default.
* ms: number // 'later' mode only, 0 default.
* }.
* - component: pure function component.
*/
const defineView = (id, options, component) => {
if (!isString(id) && isUndefined(component)) {
component = options;
options = id;
id = undefined;
}
const { inits = [], constants = {}, injects = {}, actions = {} } = options;
const formulaIds = Object.keys(injects);
const injectionNames = Object.values(injects);
const dispatchers = dispatchersOf(actions);
const view = props => {
const states = formulaIds.map(deref).map(useState);
const getters = states.map(state => state[0]).map(toJS);
const injections = injectionNames.map((name, index) => [
isArray(name) ? name : [name],
getters[index]
]).reduce((o, [path, getter]) => o.setIn(path, getter), Map()).toJS()
useEffect(() => {
const setters = states.map(state => state[1]);
const formulaId = isUndefined(id)? uuid(): id;
defineFormula(formulaId, formulaIds, (...params) => {
setters.forEach((setter, index) => {
setter(params[index]);
});
});
if (inits) {
inits.forEach(action => dispatch(action));
}
return () => deleteFormula(formulaId);
}, []);
return React.createElement(component, {
...constants,
...props,
...injections,
...dispatchers
}, props.children);
};
if (isString(id)) {
views[id] = view;
}
return view;
};
/**
* Get view by id.
*/
const viewOf = id => views[id];
/**
* The root of pure frame.
* - state: default value of state.
*/
const PureFrameRoot = ({ state = {}, children }) => {
reset('state', fromJS(state));
return React.createElement(React.Fragment, null, children);
};
/* # Events and Listeners */
/**
* Wrap action dispatcher to event listener.
*/
const wrapDispatcherToListener = dispatcher => {
if (isString(dispatcher)) {
return event => ({ action: [dispatcher, event] });
} else if (isObject(dispatcher)) {
return event => ({
action: [dispatcher.id, event],
mode: dispatcher.mode,
ms: dispatcher.ms
});
} else if (isFunction(dispatcher)) {
return event => {
const action = dispatcher(event);
if (isArray(action)) {
return { action };
} else if (isObject(action)) {
return action;
} else {
throw `Invalid action: ${action}`;
}
};
} else {
throw `Invalid dispatcher: ${dispatcher}`;
}
};
/**
* Define event listener: to dispatch action when event fired.
* - target: object can receive events and may have listeners for them.
* - type: a case-sensitive string representing the event type to listen for.
* - dispatcher: string or function.
* - string: action id. the event object will be used as params.
* - function: to cast event to action and params.
* - options: options or useCapture, default `false`.
*/
const defineListener = (target, type, dispatcher, options = false) => {
const listener = wrapDispatcherToListener(dispatcher);
return target.addEventListener(type, event => {
const { action, mode, ms } = listener(event);
dispatchGeneric(action, mode, ms);
}, options);
};
/**
* Wrap setInterval & setTimeout as clock events.
*/
const clock = {
addEventListener: (type, listener, options) => {
const ms = isObject(options)? options.ms: options;
const fn = () => listener(Date.now());
switch (type) {
case 'interval':
return setInterval(fn, ms);
case 'timeout':
return setTimeout(fn, ms);
default:
throw `Unknown clock event type: ${type}`;
}
}
};
module.exports = {
clock,
dispatchSync,
dispatchLater,
dispatch,
defineFetcher,
fetcher,
fetch,
definePerformer,
defineReducer,
defineStateReducer,
defineExtractor,
defineTransformer,
defineView,
viewOf,
defineListener,
PureFrameRoot
};