Skip to content

Commit 40d39e0

Browse files
committed
rename to NotebookRuntime; expose builtins
1 parent 5de72fd commit 40d39e0

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/runtime/define.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Runtime, type Module, type Variable, type VariableDefinition} from "@observablehq/runtime";
1+
import type {Module, Variable, VariableDefinition} from "@observablehq/runtime";
2+
import {Runtime} from "@observablehq/runtime";
23
import type {DisplayState} from "./display.js";
34
import {clear, display, observe} from "./display.js";
45
import {library} from "./stdlib/index.js";
@@ -32,18 +33,29 @@ export type Definition = {
3233
assets?: Map<string, string>;
3334
};
3435

35-
export class NotebookInstance {
36+
export class NotebookRuntime {
37+
readonly runtime: Runtime & {fileAttachments: typeof fileAttachments};
38+
readonly main: Module;
3639

37-
readonly runtime = Object.assign(new Runtime({ ...library, __ojs_runtime: () => this.runtime }), { fileAttachments });
38-
readonly main = (this.runtime as typeof this.runtime & { main: Module }).main = this.runtime.module();
39-
40-
constructor() {
40+
constructor(builtins = library) {
41+
const runtime = new Runtime({...builtins, __ojs_runtime: () => runtime});
42+
this.runtime = Object.assign(runtime, {fileAttachments});
43+
this.main = runtime.module();
4144
}
4245

4346
define(state: DefineState, definition: Definition, observer = observe): void {
44-
const { id, body, inputs = [], outputs = [], output, autodisplay, autoview, automutable } = definition;
47+
const {
48+
id,
49+
body,
50+
inputs = [],
51+
outputs = [],
52+
output,
53+
autodisplay,
54+
autoview,
55+
automutable
56+
} = definition;
4557
const variables = state.variables;
46-
const v = this.main.variable(observer(state, definition), { shadow: {} });
58+
const v = this.main.variable(observer(state, definition), {shadow: {}});
4759
const vid = output ?? (outputs.length ? `cell ${id}` : null);
4860
if (inputs.includes("display") || inputs.includes("view")) {
4961
let displayVersion = -1; // the variable._version of currently-displayed values
@@ -64,7 +76,7 @@ export class NotebookInstance {
6476
);
6577
v._shadow.set("display", vd);
6678
if (inputs.includes("view")) {
67-
const vv = new (v.constructor as typeof Variable)(2, v._module, null, { shadow: {} });
79+
const vv = new (v.constructor as typeof Variable)(2, v._module, null, {shadow: {}});
6880
vv._shadow.set("display", vd);
6981
vv.define(["display"], (display) => (value: unknown) => input(display(value)));
7082
v._shadow.set("view", vv);
@@ -100,19 +112,21 @@ export class NotebookInstance {
100112
}
101113
}
102114

103-
const defaultInstance = new NotebookInstance();
115+
const defaultNotebook = new NotebookRuntime();
104116

105-
export const runtime = defaultInstance.runtime;
106-
export const main = defaultInstance.main;
117+
export const runtime = defaultNotebook.runtime;
118+
export const main = defaultNotebook.main;
107119

108120
main.constructor.prototype.defines = function (this: Module, name: string): boolean {
109121
return (
110-
this._scope.has(name) ||
111-
this._builtins.has(name) ||
112-
this._runtime._builtin._scope.has(name)
122+
this._scope.has(name) || this._builtins.has(name) || this._runtime._builtin._scope.has(name)
113123
);
114124
};
115125

116-
export function define(state: DefineState, definition: Definition, observer = observe): void {
117-
defaultInstance.define(state, definition, observer);
118-
}
126+
export function define(
127+
state: DefineState,
128+
definition: Definition,
129+
observer?: typeof observe
130+
): void {
131+
defaultNotebook.define(state, definition, observer);
132+
}

0 commit comments

Comments
 (0)