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" ;
2
3
import type { DisplayState } from "./display.js" ;
3
4
import { clear , display , observe } from "./display.js" ;
4
5
import { library } from "./stdlib/index.js" ;
@@ -32,18 +33,29 @@ export type Definition = {
32
33
assets ?: Map < string , string > ;
33
34
} ;
34
35
35
- export class NotebookInstance {
36
+ export class NotebookRuntime {
37
+ readonly runtime : Runtime & { fileAttachments : typeof fileAttachments } ;
38
+ readonly main : Module ;
36
39
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 ( ) ;
41
44
}
42
45
43
46
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 ;
45
57
const variables = state . variables ;
46
- const v = this . main . variable ( observer ( state , definition ) , { shadow : { } } ) ;
58
+ const v = this . main . variable ( observer ( state , definition ) , { shadow : { } } ) ;
47
59
const vid = output ?? ( outputs . length ? `cell ${ id } ` : null ) ;
48
60
if ( inputs . includes ( "display" ) || inputs . includes ( "view" ) ) {
49
61
let displayVersion = - 1 ; // the variable._version of currently-displayed values
@@ -64,7 +76,7 @@ export class NotebookInstance {
64
76
) ;
65
77
v . _shadow . set ( "display" , vd ) ;
66
78
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 : { } } ) ;
68
80
vv . _shadow . set ( "display" , vd ) ;
69
81
vv . define ( [ "display" ] , ( display ) => ( value : unknown ) => input ( display ( value ) ) ) ;
70
82
v . _shadow . set ( "view" , vv ) ;
@@ -100,19 +112,21 @@ export class NotebookInstance {
100
112
}
101
113
}
102
114
103
- const defaultInstance = new NotebookInstance ( ) ;
115
+ const defaultNotebook = new NotebookRuntime ( ) ;
104
116
105
- export const runtime = defaultInstance . runtime ;
106
- export const main = defaultInstance . main ;
117
+ export const runtime = defaultNotebook . runtime ;
118
+ export const main = defaultNotebook . main ;
107
119
108
120
main . constructor . prototype . defines = function ( this : Module , name : string ) : boolean {
109
121
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 )
113
123
) ;
114
124
} ;
115
125
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