diff --git a/src/runtime/define.ts b/src/runtime/define.ts index d1a6cbe..a49f170 100644 --- a/src/runtime/define.ts +++ b/src/runtime/define.ts @@ -1,7 +1,6 @@ -import type {Variable, VariableDefinition} from "@observablehq/runtime"; +import type {Module, Variable, VariableDefinition} from "@observablehq/runtime"; import type {DisplayState} from "./display.js"; import {clear, display, observe} from "./display.js"; -import {main} from "./index.js"; import {input} from "./stdlib/generators/index.js"; import {Mutator} from "./stdlib/mutable.js"; @@ -31,7 +30,7 @@ export type Definition = { assets?: Map; }; -export function define(state: DefineState, definition: Definition, observer = observe): void { +export function define(main: Module, state: DefineState, definition: Definition, observer = observe): void { const {id, body, inputs = [], outputs = [], output, autodisplay, autoview, automutable} = definition; const variables = state.variables; const v = main.variable(observer(state, definition), {shadow: {}}); diff --git a/src/runtime/index.ts b/src/runtime/index.ts index d1594ac..d3d77a4 100644 --- a/src/runtime/index.ts +++ b/src/runtime/index.ts @@ -1,20 +1,40 @@ import type {Module} from "@observablehq/runtime"; import {Runtime} from "@observablehq/runtime"; +import type {DefineState, Definition} from "./define.js"; +import {define as _define} from "./define.js"; +import type {observe} from "./display.js"; import {fileAttachments} from "./stdlib/fileAttachment.js"; import {library} from "./stdlib/index.js"; -export * from "./define.js"; +export type {DefineState, Definition} from "./define.js"; export * from "./display.js"; export * from "./inspect.js"; export * from "./stdlib/index.js"; - export type * from "./stdlib/databaseClient.js"; -export type * from "./stdlib/fileAttachment.js"; export {DatabaseClient} from "./stdlib/databaseClient.js"; +export type * from "./stdlib/fileAttachment.js"; export {FileAttachment, registerFile} from "./stdlib/fileAttachment.js"; -export const runtime = Object.assign(new Runtime({...library, __ojs_runtime: () => runtime}), {fileAttachments}); -export const main = (runtime as typeof runtime & {main: Module}).main = runtime.module(); +export class NotebookRuntime { + readonly runtime: Runtime & {fileAttachments: typeof fileAttachments}; + readonly main: Module; + + constructor(builtins = library) { + const runtime = new Runtime({...builtins, __ojs_runtime: () => runtime}); + this.runtime = Object.assign(runtime, {fileAttachments}); + this.main = runtime.module(); + } + + define(state: DefineState, definition: Definition, observer?: typeof observe): void { + _define(this.main, state, definition, observer); + } +} + +const defaultNotebook = new NotebookRuntime(); + +export const runtime = defaultNotebook.runtime; +export const main = defaultNotebook.main; +export const define = defaultNotebook.define.bind(defaultNotebook); main.constructor.prototype.defines = function (this: Module, name: string): boolean { return (