Skip to content

Commit a45d475

Browse files
committed
Add runtime.dispose.
1 parent 95e96fe commit a45d475

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ If *define* is specified, it is a function which defines the new module’s [var
100100

101101
If an *observer* factory function is specified, it is called for each named variable in the returned module, being passed the variable’s name. The [standard inspector](#inspector) is available as a ready-made observer: it displays DOM elements “as-is” and renders interactive displays for other arbitrary values such as numbers and objects.
102102

103+
<a href="#runtime_dispose" name="runtime_dispose">#</a> <i>runtime</i>.<b>dispose</b>() [<>](https://github.com/observablehq/runtime/blob/master/src/runtime.js "Source")
104+
105+
Disposes this runtime, invalidating all active variables and disabling future computation.
106+
103107
### Modules
104108

105109
A module is a namespace for [variables](#variables); within a module, variables should typically have unique names. [Imports](#variable_import) allow variables to be referenced across modules.

src/runtime.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export default function Runtime(builtins = new Library, global = window_global)
1818
_updates: {value: new Set},
1919
_computing: {value: null, writable: true},
2020
_modules: {value: new Map},
21+
_variables: {value: new Set},
22+
_disposed: {value: false, writable: true},
2123
_builtin: {value: builtin},
2224
_global: {value: global}
2325
});
@@ -34,9 +36,19 @@ Object.defineProperties(Runtime.prototype, {
3436
_compute: {value: runtime_compute, writable: true, configurable: true},
3537
_computeSoon: {value: runtime_computeSoon, writable: true, configurable: true},
3638
_computeNow: {value: runtime_computeNow, writable: true, configurable: true},
39+
dispose: {value: runtime_dispose, writable: true, configurable: true},
3740
module: {value: runtime_module, writable: true, configurable: true}
3841
});
3942

43+
function runtime_dispose() {
44+
this._computing = Promise.resolve();
45+
this._disposed = true;
46+
this._variables.forEach(v => {
47+
v._invalidate();
48+
v._version = NaN;
49+
});
50+
}
51+
4052
function runtime_module(define, observer = noop) {
4153
if (define === undefined) return new Module(this);
4254
let module = this._modules.get(define);
@@ -54,7 +66,7 @@ function runtime_computeSoon() {
5466
return new Promise(function(resolve) {
5567
frame(function() {
5668
resolve();
57-
runtime._computeNow();
69+
runtime._disposed || runtime._computeNow();
5870
});
5971
});
6072
}

src/variable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ function variable_defineImpl(name, inputs, definition) {
9797
this._definition = definition;
9898
this._value = undefined;
9999

100+
// Is this an active variable (that may require disposal)?
101+
if (definition === noop) runtime._variables.delete(this);
102+
else runtime._variables.add(this);
103+
100104
// Did the variable’s name change? Time to patch references!
101105
if (name == this._name && scope.get(name) === this) {
102106
this._outputs.forEach(runtime._updates.add, runtime._updates);

test/dispose.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<script type=module>
3+
4+
import {Runtime, Inspector} from "/dist/runtime.js";
5+
6+
const runtime = new Runtime();
7+
const main = runtime.module();
8+
9+
main.variable(true).define(["invalidation"], async invalidation => {
10+
await invalidation;
11+
console.log("invalidation");
12+
});
13+
14+
main.variable(true).define([], function*() {
15+
try { while (true) yield; }
16+
finally { console.log("return"); }
17+
});
18+
19+
setTimeout(() => runtime.dispose(), 1000);
20+
21+
</script>

0 commit comments

Comments
 (0)