-
Notifications
You must be signed in to change notification settings - Fork 71
/
runtime.js
365 lines (322 loc) · 11.7 KB
/
runtime.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
import {Library, FileAttachments} from "@observablehq/stdlib";
import {RuntimeError} from "./errors.js";
import {generatorish} from "./generatorish.js";
import {Module, variable_variable, variable_invalidation, variable_visibility} from "./module.js";
import {noop} from "./noop.js";
import {Variable, TYPE_IMPLICIT, no_observer, variable_stale} from "./variable.js";
const frame = typeof requestAnimationFrame === "function" ? requestAnimationFrame
: typeof setImmediate === "function" ? setImmediate
: f => setTimeout(f, 0);
export function Runtime(builtins = new Library, global = window_global) {
const builtin = this.module();
Object.defineProperties(this, {
_dirty: {value: new Set},
_updates: {value: new Set},
_precomputes: {value: [], writable: true},
_computing: {value: null, writable: true},
_init: {value: null, writable: true},
_modules: {value: new Map},
_variables: {value: new Set},
_disposed: {value: false, writable: true},
_builtin: {value: builtin},
_global: {value: global}
});
if (builtins) for (const name in builtins) {
(new Variable(TYPE_IMPLICIT, builtin)).define(name, [], builtins[name]);
}
}
Object.defineProperties(Runtime.prototype, {
_precompute: {value: runtime_precompute, writable: true, configurable: true},
_compute: {value: runtime_compute, writable: true, configurable: true},
_computeSoon: {value: runtime_computeSoon, writable: true, configurable: true},
_computeNow: {value: runtime_computeNow, writable: true, configurable: true},
dispose: {value: runtime_dispose, writable: true, configurable: true},
module: {value: runtime_module, writable: true, configurable: true},
fileAttachments: {value: FileAttachments, writable: true, configurable: true}
});
function runtime_dispose() {
this._computing = Promise.resolve();
this._disposed = true;
this._variables.forEach(v => {
v._invalidate();
v._version = NaN;
});
}
function runtime_module(define, observer = noop) {
let module;
if (define === undefined) {
if (module = this._init) {
this._init = null;
return module;
}
return new Module(this);
}
module = this._modules.get(define);
if (module) return module;
this._init = module = new Module(this);
this._modules.set(define, module);
try {
define(this, observer);
} finally {
this._init = null;
}
return module;
}
function runtime_precompute(callback) {
this._precomputes.push(callback);
this._compute();
}
function runtime_compute() {
return this._computing || (this._computing = this._computeSoon());
}
function runtime_computeSoon() {
return new Promise(frame).then(() => this._disposed ? undefined : this._computeNow());
}
async function runtime_computeNow() {
let queue = [],
variables,
variable,
precomputes = this._precomputes;
// If there are any paused generators, resume them before computing so they
// can update (if synchronous) before computing downstream variables.
if (precomputes.length) {
this._precomputes = [];
for (const callback of precomputes) callback();
await runtime_defer(3);
}
// Compute the reachability of the transitive closure of dirty variables.
// Any newly-reachable variable must also be recomputed.
// Any no-longer-reachable variable must be terminated.
variables = new Set(this._dirty);
variables.forEach(function(variable) {
variable._inputs.forEach(variables.add, variables);
const reachable = variable_reachable(variable);
if (reachable > variable._reachable) {
this._updates.add(variable);
} else if (reachable < variable._reachable) {
variable._invalidate();
}
variable._reachable = reachable;
}, this);
// Compute the transitive closure of updating, reachable variables.
variables = new Set(this._updates);
variables.forEach(function(variable) {
if (variable._reachable) {
variable._indegree = 0;
variable._outputs.forEach(variables.add, variables);
} else {
variable._indegree = NaN;
variables.delete(variable);
}
});
this._computing = null;
this._updates.clear();
this._dirty.clear();
// Compute the indegree of updating variables.
variables.forEach(function(variable) {
variable._outputs.forEach(variable_increment);
});
do {
// Identify the root variables (those with no updating inputs).
variables.forEach(function(variable) {
if (variable._indegree === 0) {
queue.push(variable);
}
});
// Compute the variables in topological order.
while (variable = queue.pop()) {
variable_compute(variable);
variable._outputs.forEach(postqueue);
variables.delete(variable);
}
// Any remaining variables are circular, or depend on them.
variables.forEach(function(variable) {
if (variable_circular(variable)) {
variable_error(variable, new RuntimeError("circular definition"));
variable._outputs.forEach(variable_decrement);
variables.delete(variable);
}
});
} while (variables.size);
function postqueue(variable) {
if (--variable._indegree === 0) {
queue.push(variable);
}
}
}
// We want to give generators, if they’re defined synchronously, a chance to
// update before computing downstream variables. This creates a synchronous
// promise chain of the given depth that we’ll await before recomputing
// downstream variables.
function runtime_defer(depth = 0) {
let p = Promise.resolve();
for (let i = 0; i < depth; ++i) p = p.then(() => {});
return p;
}
function variable_circular(variable) {
const inputs = new Set(variable._inputs);
for (const i of inputs) {
if (i === variable) return true;
i._inputs.forEach(inputs.add, inputs);
}
return false;
}
function variable_increment(variable) {
++variable._indegree;
}
function variable_decrement(variable) {
--variable._indegree;
}
function variable_value(variable) {
return variable._promise.catch(variable._rejector);
}
function variable_invalidator(variable) {
return new Promise(function(resolve) {
variable._invalidate = resolve;
});
}
function variable_intersector(invalidation, variable) {
let node = typeof IntersectionObserver === "function" && variable._observer && variable._observer._node;
let visible = !node, resolve = noop, reject = noop, promise, observer;
if (node) {
observer = new IntersectionObserver(([entry]) => (visible = entry.isIntersecting) && (promise = null, resolve()));
observer.observe(node);
invalidation.then(() => (observer.disconnect(), observer = null, reject()));
}
return function(value) {
if (visible) return Promise.resolve(value);
if (!observer) return Promise.reject();
if (!promise) promise = new Promise((y, n) => (resolve = y, reject = n));
return promise.then(() => value);
};
}
function variable_compute(variable) {
variable._invalidate();
variable._invalidate = noop;
variable._pending();
const value0 = variable._value;
const version = ++variable._version;
// Lazily-constructed invalidation variable; only constructed if referenced as an input.
let invalidation = null;
// If the variable doesn’t have any inputs, we can optimize slightly.
const promise = variable._promise = (variable._inputs.length
? Promise.all(variable._inputs.map(variable_value)).then(define)
: new Promise(resolve => resolve(variable._definition.call(value0))))
.then(generate);
// Compute the initial value of the variable.
function define(inputs) {
if (variable._version !== version) throw variable_stale;
// Replace any reference to invalidation with the promise, lazily.
for (let i = 0, n = inputs.length; i < n; ++i) {
switch (inputs[i]) {
case variable_invalidation: {
inputs[i] = invalidation = variable_invalidator(variable);
break;
}
case variable_visibility: {
if (!invalidation) invalidation = variable_invalidator(variable);
inputs[i] = variable_intersector(invalidation, variable);
break;
}
case variable_variable: {
inputs[i] = variable;
break;
}
}
}
return variable._definition.apply(value0, inputs);
}
// If the value is a generator, then retrieve its first value, and dispose of
// the generator if the variable is invalidated. Note that the cell may
// already have been invalidated here, in which case we need to terminate the
// generator immediately!
function generate(value) {
if (variable._version !== version) throw variable_stale;
if (generatorish(value)) {
(invalidation || variable_invalidator(variable)).then(variable_return(value));
return variable_generate(variable, version, value);
}
return value;
}
promise.then((value) => {
variable._value = value;
variable._fulfilled(value);
}, (error) => {
if (error === variable_stale || variable._version !== version) return;
variable._value = undefined;
variable._rejected(error);
});
}
function variable_generate(variable, version, generator) {
const runtime = variable._module._runtime;
let currentValue; // so that yield resolves to the yielded value
// Retrieve the next value from the generator; if successful, invoke the
// specified callback. The returned promise resolves to the yielded value, or
// to undefined if the generator is done.
function compute(onfulfilled) {
return new Promise(resolve => resolve(generator.next(currentValue))).then(({done, value}) => {
return done ? undefined : Promise.resolve(value).then(onfulfilled);
});
}
// Retrieve the next value from the generator; if successful, fulfill the
// variable, compute downstream variables, and schedule the next value to be
// pulled from the generator at the start of the next animation frame. If not
// successful, reject the variable, compute downstream variables, and return.
function recompute() {
const promise = compute((value) => {
if (variable._version !== version) throw variable_stale;
currentValue = value;
postcompute(value, promise).then(() => runtime._precompute(recompute));
variable._fulfilled(value);
return value;
});
promise.catch((error) => {
if (error === variable_stale || variable._version !== version) return;
postcompute(undefined, promise);
variable._rejected(error);
});
}
// After the generator fulfills or rejects, set its current value, promise,
// and schedule any downstream variables for update.
function postcompute(value, promise) {
variable._value = value;
variable._promise = promise;
variable._outputs.forEach(runtime._updates.add, runtime._updates);
return runtime._compute();
}
// When retrieving the first value from the generator, the promise graph is
// already established, so we only need to queue the next pull.
return compute((value) => {
if (variable._version !== version) throw variable_stale;
currentValue = value;
runtime._precompute(recompute);
return value;
});
}
function variable_error(variable, error) {
variable._invalidate();
variable._invalidate = noop;
variable._pending();
++variable._version;
variable._indegree = NaN;
(variable._promise = Promise.reject(error)).catch(noop);
variable._value = undefined;
variable._rejected(error);
}
function variable_return(generator) {
return function() {
generator.return();
};
}
function variable_reachable(variable) {
if (variable._observer !== no_observer) return true; // Directly reachable.
const outputs = new Set(variable._outputs);
for (const output of outputs) {
if (output._observer !== no_observer) return true;
output._outputs.forEach(outputs.add, outputs);
}
return false;
}
function window_global(name) {
return globalThis[name];
}