1
1
import * as jsyaml from 'js-yaml' ;
2
2
import { BaseEvalElement } from './base' ;
3
- import { initializers , loadedEnvironments , mode , postInitializers , pyodideLoaded , scriptsQueue , globalLoader , appConfig , Initializer } from '../stores' ;
3
+ import {
4
+ initializers ,
5
+ loadedEnvironments ,
6
+ mode ,
7
+ postInitializers ,
8
+ pyodideLoaded ,
9
+ scriptsQueue ,
10
+ globalLoader ,
11
+ appConfig ,
12
+ Initializer ,
13
+ } from '../stores' ;
4
14
import { loadInterpreter } from '../interpreter' ;
5
15
import type { PyScript } from './pyscript' ;
6
16
7
-
8
17
const DEFAULT_RUNTIME = {
9
- src : " https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js" ,
10
- name : " pyodide-default" ,
11
- lang : " python"
12
- }
18
+ src : ' https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js' ,
19
+ name : ' pyodide-default' ,
20
+ lang : ' python' ,
21
+ } ;
13
22
14
23
export type Runtime = {
15
24
src : string ;
@@ -28,107 +37,103 @@ let appConfig_: AppConfig = {
28
37
autoclose_loader : true ,
29
38
} ;
30
39
31
- appConfig . subscribe ( ( value :AppConfig ) => {
32
- if ( value ) {
40
+ appConfig . subscribe ( ( value : AppConfig ) => {
41
+ if ( value ) {
33
42
appConfig_ = value ;
34
43
}
35
- console . log ( " config set!" )
44
+ console . log ( ' config set!' ) ;
36
45
} ) ;
37
46
38
47
let initializers_ : Initializer [ ] ;
39
- initializers . subscribe ( ( value :Initializer [ ] ) => {
48
+ initializers . subscribe ( ( value : Initializer [ ] ) => {
40
49
initializers_ = value ;
41
- console . log ( " initializers set" )
50
+ console . log ( ' initializers set' ) ;
42
51
} ) ;
43
52
44
53
let postInitializers_ : Initializer [ ] ;
45
- postInitializers . subscribe ( ( value :Initializer [ ] ) => {
54
+ postInitializers . subscribe ( ( value : Initializer [ ] ) => {
46
55
postInitializers_ = value ;
47
- console . log ( " post initializers set" )
56
+ console . log ( ' post initializers set' ) ;
48
57
} ) ;
49
58
50
59
let scriptsQueue_ : PyScript [ ] ;
51
- scriptsQueue . subscribe ( ( value : PyScript [ ] ) => {
60
+ scriptsQueue . subscribe ( ( value : PyScript [ ] ) => {
52
61
scriptsQueue_ = value ;
53
- console . log ( " post initializers set" )
62
+ console . log ( ' post initializers set' ) ;
54
63
} ) ;
55
64
56
65
let mode_ : string ;
57
- mode . subscribe ( ( value :string ) => {
66
+ mode . subscribe ( ( value : string ) => {
58
67
mode_ = value ;
59
- console . log ( " post initializers set" )
68
+ console . log ( ' post initializers set' ) ;
60
69
} ) ;
61
70
62
-
63
71
let pyodideReadyPromise ;
64
72
let loader ;
65
73
66
-
67
74
globalLoader . subscribe ( value => {
68
75
loader = value ;
69
76
} ) ;
<
E377
/tr>70
77
71
-
72
- export class PyodideRuntime extends Object {
78
+ export class PyodideRuntime extends Object {
73
79
src : string ;
74
80
75
- constructor ( url :string ) {
81
+ constructor ( url : string ) {
76
82
super ( ) ;
77
83
this . src = url ;
78
84
}
79
85
80
- async initialize ( ) {
81
- loader . log ( "Loading runtime..." )
82
- pyodideReadyPromise = loadInterpreter ( this . src ) ;
83
- const pyodide = await pyodideReadyPromise ;
84
- const newEnv = {
85
- id : 'a' ,
86
- promise : pyodideReadyPromise ,
87
- runtime : pyodide ,
88
- state : 'loading' ,
89
- } ;
90
- pyodideLoaded . set ( pyodide ) ;
91
-
92
- // Inject the loader into the runtime namespace
93
- pyodide . globals . set ( "pyscript_loader" , loader ) ;
94
-
95
- loader . log ( "Runtime created..." )
96
- loadedEnvironments . update ( ( value : any ) : any => {
97
- value [ newEnv [ 'id' ] ] = newEnv ;
98
- } ) ;
99
-
100
- // now we call all initializers before we actually executed all page scripts
101
- loader . log ( "Initializing components..." )
102
- for ( const initializer of initializers_ ) {
103
- await initializer ( ) ;
104
- }
105
-
106
- // now we can actually execute the page scripts if we are in play mode
2851
td>107
- loader . log ( "Initializing scripts..." )
108
- if ( mode_ == 'play' ) {
109
- for ( const script of scriptsQueue_ ) {
110
- script . evaluate ( ) ;
86
+ async initialize ( ) {
87
+ loader . log ( 'Loading runtime...' ) ;
88
+ pyodideReadyPromise = loadInterpreter ( this . src ) ;
89
+ const pyodide = await pyodideReadyPromise ;
90
+ const newEnv = {
91
+ id : 'a' ,
92
+ promise : pyodideReadyPromise ,
93
+ runtime : pyodide ,
94
+ state : 'loading' ,
95
+ } ;
96
+ pyodideLoaded . set ( pyodide ) ;
97
+
98
+ // Inject the loader into the runtime namespace
99
+ pyodide . globals . set ( 'pyscript_loader' , loader ) ;
100
+
101
+ loader . log ( 'Runtime created...' ) ;
102
+ loadedEnvironments . update ( ( value : any ) : any => {
103
+ value [ newEnv [ 'id' ] ] = newEnv ;
104
+ } ) ;
105
+
106
+ // now we call all initializers before we actually executed all page scripts
107
+ loader . log ( 'Initializing components...' ) ;
108
+ for ( const initializer of initializers_ ) {
109
+ await initializer ( ) ;
111
110
}
112
- scriptsQueue . set ( [ ] ) ;
113
- }
114
111
115
- // now we call all post initializers AFTER we actually executed all page scripts
116
- loader . log ( "Running post initializers..." ) ;
112
+ // now we can actually execute the page scripts if we are in play mode
113
+ loader . log ( 'Initializing scripts...' ) ;
114
+ if ( mode_ == 'play' ) {
115
+ for ( const script of scriptsQueue_ ) {
116
+ script . evaluate ( ) ;
117
+ }
118
+ scriptsQueue . set ( [ ] ) ;
119
+ }
117
120
118
- if ( appConfig_ && appConfig_ . autoclose_loader ) {
119
- loader . close ( ) ;
120
- console . log ( "------ loader closed ------" ) ;
121
- }
121
+ // now we call all post initializers AFTER we actually executed all page scripts
122
+ loader . log ( 'Running post initializers...' ) ;
122
123
123
- setTimeout ( ( ) => {
124
- for ( const initializer of postInitializers_ ) {
125
- initializer ( ) ;
124
+ if ( appConfig_ && appConfig_ . autoclose_loader ) {
125
+ loader . close ( ) ;
126
+ console . log ( '------ loader closed ------' ) ;
126
127
}
127
- } , 3000 ) ;
128
+
129
+ setTimeout ( ( ) => {
130
+ for ( const initializer of postInitializers_ ) {
131
+ initializer ( ) ;
132
+ }
133
+ } , 3000 ) ;
128
134
}
129
135
}
130
136
131
-
132
137
export class PyConfig extends BaseEvalElement {
133
138
shadow : ShadowRoot ;
134
139
wrapper : HTMLElement ;
@@ -149,23 +154,23 @@ export class PyConfig extends BaseEvalElement {
149
154
this . innerHTML = '' ;
150
155
151
156
const loadedValues = jsyaml . load ( this . code ) ;
152
- if ( loadedValues === undefined ) {
157
+ if ( loadedValues === undefined ) {
153
158
this . values = {
154
159
autoclose_loader : true ,
155
160
} ;
156
- } else {
161
+ } else {
157
162
this . values = Object . assign ( { } , ...loadedValues ) ;
158
163
}
159
- if ( this . values . runtimes === undefined ) {
164
+ if ( this . values . runtimes === undefined ) {
160
165
this . values . runtimes = [ DEFAULT_RUNTIME ] ;
161
166
}
162
167
appConfig . set ( this . values ) ;
163
- console . log ( " config set" , this . values ) ;
168
+ console . log ( ' config set' , this . values ) ;
164
169
165
170
this . loadRuntimes ( ) ;
166
171
}
167
172
168
- log ( msg : string ) {
173
+ log ( msg : string ) {
169
174
const newLog = document . createElement ( 'p' ) ;
170
175
newLog . innerText = msg ;
171
176
this . details . appendChild ( newLog ) ;
@@ -175,10 +180,10 @@ export class PyConfig extends BaseEvalElement {
175
180
this . remove ( ) ;
176
181
}
177
182
178
- loadRuntimes ( ) {
179
- console . log ( " Initializing runtimes..." )
183
+ loadRuntimes ( ) {
184
+ console . log ( ' Initializing runtimes...' ) ;
180
185
for ( const runtime of this . values . runtimes ) {
181
- const script = document . createElement ( " script" ) ; // create a script DOM node
186
+ const script = document . createElement ( ' script' ) ; // create a script DOM node
182
187
const runtimeSpec = new PyodideRuntime ( runtime . src ) ;
183
188
script . src = runtime . src ; // set its src to the provided URL
184
189
script . addEventListener ( 'load' , ( ) => {
0 commit comments