Skip to content

Commit 20911d6

Browse files
Allow instantiation of multiple notebooks inside a single scope (#49)
* Allow instantiation of multiple notebooks inside a single scope Signed-off-by: Gordon Smith <[email protected]> * rename to NotebookRuntime; expose builtins * sort imports * minimize diff --------- Signed-off-by: Gordon Smith <[email protected]> Co-authored-by: Gordon Smith <[email protected]>
1 parent eb30fda commit 20911d6

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/runtime/define.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type {Variable, VariableDefinition} from "@observablehq/runtime";
1+
import type {Module, Variable, VariableDefinition} from "@observablehq/runtime";
22
import type {DisplayState} from "./display.js";
33
import {clear, display, observe} from "./display.js";
4-
import {main} from "./index.js";
54
import {input} from "./stdlib/generators/index.js";
65
import {Mutator} from "./stdlib/mutable.js";
76

@@ -31,7 +30,7 @@ export type Definition = {
3130
assets?: Map<string, string>;
3231
};
3332

34-
export function define(state: DefineState, definition: Definition, observer = observe): void {
33+
export function define(main: Module, state: DefineState, definition: Definition, observer = observe): void {
3534
const {id, body, inputs = [], outputs = [], output, autodisplay, autoview, automutable} = definition;
3635
const variables = state.variables;
3736
const v = main.variable(observer(state, definition), {shadow: {}});

src/runtime/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,40 @@
11
import type {Module} from "@observablehq/runtime";
22
import {Runtime} from "@observablehq/runtime";
3+
import type {DefineState, Definition} from "./define.js";
4+
import {define as _define} from "./define.js";
5+
import type {observe} from "./display.js";
36
import {fileAttachments} from "./stdlib/fileAttachment.js";
47
import {library} from "./stdlib/index.js";
58

6-
export * from "./define.js";
9+
export type {DefineState, Definition} from "./define.js";
710
export * from "./display.js";
811
export * from "./inspect.js";
912
export * from "./stdlib/index.js";
10-
1113
export type * from "./stdlib/databaseClient.js";
12-
export type * from "./stdlib/fileAttachment.js";
1314
export {DatabaseClient} from "./stdlib/databaseClient.js";
15+
export type * from "./stdlib/fileAttachment.js";
1416
export {FileAttachment, registerFile} from "./stdlib/fileAttachment.js";
1517

16-
export const runtime = Object.assign(new Runtime({...library, __ojs_runtime: () => runtime}), {fileAttachments});
17-
export const main = (runtime as typeof runtime & {main: Module}).main = runtime.module();
18+
export class NotebookRuntime {
19+
readonly runtime: Runtime & {fileAttachments: typeof fileAttachments};
20+
readonly main: Module;
21+
22+
constructor(builtins = library) {
23+
const runtime = new Runtime({...builtins, __ojs_runtime: () => runtime});
24+
this.runtime = Object.assign(runtime, {fileAttachments});
25+
this.main = runtime.module();
26+
}
27+
28+
define(state: DefineState, definition: Definition, observer?: typeof observe): void {
29+
_define(this.main, state, definition, observer);
30+
}
31+
}
32+
33+
const defaultNotebook = new NotebookRuntime();
34+
35+
export const runtime = defaultNotebook.runtime;
36+
export const main = defaultNotebook.main;
37+
export const define = defaultNotebook.define.bind(defaultNotebook);
1838

1939
main.constructor.prototype.defines = function (this: Module, name: string): boolean {
2040
return (

0 commit comments

Comments
 (0)