Skip to content

Commit

Permalink
Allow passing in specials via runtime.module(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
visnup committed Oct 15, 2019
1 parent eb641cf commit c24a4e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/module.js
Expand Up @@ -6,13 +6,14 @@ import rethrow from "./rethrow";
import {variable_invalidation, variable_visibility} from "./runtime";
import Variable, {TYPE_DUPLICATE, TYPE_IMPLICIT, TYPE_NORMAL, no_observer} from "./variable";

export default function Module(runtime) {
export default function Module(runtime, special = {}) {
Object.defineProperties(this, {
_runtime: {value: runtime},
_scope: {value: new Map},
_special: {value: new Map([
["invalidation", variable_invalidation],
["visibility", variable_visibility]
["visibility", variable_visibility],
...Object.entries(special)
])},
_source: {value: null, writable: true}
});
Expand Down
4 changes: 2 additions & 2 deletions src/runtime.js
Expand Up @@ -50,7 +50,7 @@ function runtime_dispose() {
});
}

function runtime_module(define, observer = noop) {
function runtime_module(define, observer = noop, special = {}) {
let module;
if (define === undefined) {
if (module = this._init) {
Expand All @@ -61,7 +61,7 @@ function runtime_module(define, observer = noop) {
}
module = this._modules.get(define);
if (module) return module;
this._init = module = new Module(this);
this._init = module = new Module(this, special);
this._modules.set(define, module);
try {
define(this, observer);
Expand Down
6 changes: 3 additions & 3 deletions test/module/special-test.js
@@ -1,11 +1,11 @@
import { Runtime } from "../../src";
import tape from "../tape";
import valueof from "../variable/valueof";
import noop from "../../src/noop";

tape("module._special is defines an module local variable", async test => {
tape("module._special defines a module local variable", async test => {
const runtime = new Runtime();
const module = runtime.module();
module._special.set("foo", () => 42);
const module = runtime.module(noop, undefined, {foo: () => 42});
const bar = module.variable(true).define("bar", ["foo"], foo => foo + 1);
await new Promise(setImmediate);
test.deepEqual(await valueof(bar), {value: 43});
Expand Down

0 comments on commit c24a4e1

Please sign in to comment.