diff --git a/README.md b/README.md index 3356414..49b9c9d 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,11 @@ module1.variable().import("foo", "bar", module0); #### *variable*.delete() -[Source](https://github.com/observablehq/runtime/blob/main/src/variable.js) · Deletes this variable’s current definition and name, if any. Any variable in this module that references this variable as an input will subsequently throw a ReferenceError. If exactly one other variable defined this variable’s previous name, such that that variable throws a ReferenceError due to its duplicate definition, that variable’s original definition is restored. +[Source](https://github.com/observablehq/runtime/blob/main/src/variable.js) · Deletes this variable’s current definition and name, if any. Any variable that references this variable as an input will subsequently throw a ReferenceError, unless the variable is subsequently redefined. If exactly one other variable defined this variable’s previous name, such that that variable throws a ReferenceError due to its duplicate definition, that variable’s original definition is restored. + +#### *variable*.dispose() + +[Source](https://github.com/observablehq/runtime/blob/main/src/variable.js) · Invalidates this variable’s current value and prevents future computation and observation. The variable should no longer be used after being disposed. You may also want to delete the variable to invalidate references to the now-disposed variable. ### Observers diff --git a/src/runtime.js b/src/runtime.js index 63f0215..354bc55 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -39,10 +39,7 @@ Object.defineProperties(Runtime.prototype, { function runtime_dispose() { this._computing = Promise.resolve(); this._disposed = true; - this._variables.forEach(v => { - v._invalidate(); - v._version = NaN; - }); + this._variables.forEach(v => v.dispose()); } function runtime_module(define, observer = noop) { diff --git a/src/variable.js b/src/variable.js index 16bdb9b..e2b20ac 100644 --- a/src/variable.js +++ b/src/variable.js @@ -41,6 +41,7 @@ Object.defineProperties(Variable.prototype, { _resolve: {value: variable_resolve, writable: true, configurable: true}, define: {value: variable_define, writable: true, configurable: true}, delete: {value: variable_delete, writable: true, configurable: true}, + dispose: {value: variable_dispose, writable: true, configurable: true}, import: {value: variable_import, writable: true, configurable: true} }); @@ -202,6 +203,11 @@ function variable_import(remote, name, module) { return variable_defineImpl.call(this, String(name), [module._resolve(String(remote))], identity); } +function variable_dispose() { + this._invalidate(); + this._version = NaN; +} + function variable_delete() { return variable_defineImpl.call(this, null, [], noop); }