-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
267 lines (234 loc) · 6.58 KB
/
core.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
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import 'babel-polyfill';
import {mkProps} from './props'
////////////
// CONCUR //
////////////
// Concur
// React async components
// async function* () {
// ...
// let p = new Promise((resolve) => {
// yield [jsx that resolves p]
// };
// let ret = await p
// ...
// return (ret + 1);
// }
export class MappedGen {
constructor(genFunc, mapView, mapRes) {
this.gen = genFunc();
this.mapViewArr = mapView? [mapView] : [];
this.mapResArr = mapRes? [mapRes] : [];
// if(!this.gen.next) {
// this.res = this.mapViewArr.reduce((v,f) => f(v), this.gen)
// }
}
mapView(f) {
this.mapViewArr.push(f);
}
mapRes(f) {
this.mapResArr.push(f);
}
async next(v) {
// if(this.res) {
// return {value: this.res, done: false};
// }
const val = await this.gen.next()
const mapper = val.done? this.mapResArr : this.mapViewArr;
val.value = mapper.reduce((v,f) => f(v), val.value)
return val;
}
[Symbol.iterator]() {
return this
}
}
// Shorthand
export function w(gen, mapView, mapRes) {
return new MappedGen(gen, mapView, mapRes);
}
// HACKY. There must be a better way.
// An internal promise that never resolves.
// Can be used for never ending async functions.
const __internal_never_ending_promise__ = new Promise(function() {});
// Display only view
// AJ: TODO: Why can't I wrap this in MappedGen?
export const displayView = (view) => (async function*() {
yield view;
return (await __internal_never_ending_promise__);
})();
export const emptyView = () => displayView([]);
// Maps a function over all values yielded by an Async Generator
export const mapView = (f) => (gen) => {
if(gen instanceof MappedGen) {
return gen.mapView(f);
}
if(isPrimitiveReact(gen)) {
return f(gen);
}
return w(async function* () {
let val = await gen.next()
while(!val.done) {
yield(f(val.value))
val = await gen.next()
}
return val.value;
});
}
// Maps a function over the return value of an Async Generator
export const mapRes = (f) => async function* (gen) {
return f(yield* gen);
};
// Internal utility
function isPrimitiveChild(x) {
return typeof x === 'string' || typeof x === 'number';
}
export function isPrimitiveReact(x) {
return isPrimitiveChild(x) || React.isValidElement(x);
}
export const orr = (children) => {
// Special case for some non-array children
if(isPrimitiveReact(children)) {
return children;
}
// Type checking the view
if(!Array.isArray(children)) {
return emptyView();
}
// Are all children primitives? Use a fragment.
if(children.map(isPrimitiveReact).reduce((x,y) => x && y, true)) {
return React.createElement(React.Fragment, null, children);
}
// If it's a mix of primitive and non-primitive,
// convert primitive children to trivial generators
// so that we can handle all children generically later
children = children.map(x => {
if(isPrimitiveReact(x)) {
return displayView(x);
}
return x;
});
// If only 0 or 1 child, then no need to merge
if(!children.length) {
return emptyView();
}
if(children.length === 1) {
return children[0];
}
return (async function* () {
// Need to get the initial views from all the children
let nextChildViewPromises = children.map(view => view.next());
let nextChildViews = await Promise.all(nextChildViewPromises)
let dones = nextChildViews.filter(x => x.done);
if(dones.length) {
return dones[0].value;
}
nextChildViewPromises = children.map((view, idx) => view.next().then(v => [idx,v]));
// Step loop
while(true) {
yield nextChildViews.reduce((x,y) => x.concat(y.value), []);
let [idx, v] = await Promise.race(nextChildViewPromises);
if(v.done) {
return v.value;
}
nextChildViews[idx] = v;
nextChildViewPromises[idx] = children[idx].next().then(v => [idx,v])
}
})();
}
const dischargeView = (handleView, view) => {
if(view && view.next && typeof view.next === 'function') {
return view.next().then(newView => {
if(newView.done) {
return newView.value;
}
handleView(newView.value);
return dischargeView(handleView, view);
});
}
return __internal_never_ending_promise__;
}
///////////
// REACT //
///////////
const reactCreateElement = (t, p) => (c) => React.createElement.apply(React, [t, p].concat(c));
export const el = function(componentOrTag, properties, ...children) {
let simpleChildren = false;
if(children.length) {
// Sometimes you can get arbitrarily nested children
children = Array.concat.apply([], children.map(child => {
if(Array.isArray(child)) {
return child;
} else {
return [child];
}
}));
simpleChildren = children.every(isPrimitiveReact);
} else {
simpleChildren = true;
}
const [p,props,simpleProps] = mkProps(properties);
if(simpleChildren && simpleProps) {
return reactCreateElement(componentOrTag, props)(children);
}
const child = orr(children);
const ret = orr([p,mapView(reactCreateElement(componentOrTag, props))(child)]);
return ret;
};
export class Concur extends Component {
constructor(props) {
super(props);
this.widget = props.children();
this.state = {view: <div>LOADING...</div>};
}
componentDidMount() {
const dv = dischargeView(v => this.setState({view: v}), this.widget);
dv.then(v => { console.log("App exited with value " + v) });
}
render() {
return this.state.view;
}
}
export const renderWidget = (w) => (root) => {
let elem = w;
if(!isPrimitiveReact(w)) {
elem = <Concur>{function() {return w;}}</Concur>
}
ReactDOM.render(elem, document.getElementById(root));
}
export const withResolver = async function*(axn) {
yield [];
let resolveFn;
const p = new Promise(resolve => { resolveFn = resolve });
axn(resolveFn);
return p;
};
//////////////////
// CONTROL FLOW //
//////////////////
export const forever = async function*(w) {
// Avoid stack overflow
while(true) {
yield* w();
}
};
// Useful array constructor
export const range = (start, stop) => Array.from({ length: stop - start }, (_, i) => start + i);
// Monad fix for JS
// mfix :: ((() -> a) -> (() -> a)) -> () -> a
export const mfix = (f) => {
return () => {
let ready = false;
let result = f(() => {
if (!ready) throw new Error(message);
return result;
})();
ready = true;
return result;
};
};
// DELME - PRIMITIVE TIMEOUT VIEW
export const Timeout = (ms) => {
return withResolver(r => setTimeout(r, ms));
}