@@ -26,33 +26,9 @@ import { ErrorCode } from "./exceptions.js";
26
26
import { robustFetch as fetch , getText } from "./fetch.js" ;
27
27
import { hooks , main , worker , codeFor , createFunction } from "./hooks.js" ;
28
28
29
- // allows lazy element features on code evaluation
30
- let currentElement ;
31
-
32
29
// generic helper to disambiguate between custom element and script
33
30
const isScript = ( { tagName } ) => tagName === "SCRIPT" ;
34
31
35
- let shouldRegister = true ;
36
- const registerModule = ( { XWorker : $XWorker , interpreter, io } ) => {
37
- // automatically use the pyscript stderr (when/if defined)
38
- // this defaults to console.error
39
- function PyWorker ( ...args ) {
40
- const worker = $XWorker ( ...args ) ;
41
- worker . onerror = ( { error } ) => io . stderr ( error ) ;
42
- return worker ;
43
- }
44
-
45
- // enrich the Python env with some JS utility for main
46
- interpreter . registerJsModule ( "_pyscript" , {
47
- PyWorker,
48
- get target ( ) {
49
- return isScript ( currentElement )
50
- ? currentElement . target . id
51
- : currentElement . id ;
52
- } ,
53
- } ) ;
54
- } ;
55
-
56
32
// avoid multiple initialization of the same library
57
33
const [
58
34
{
@@ -118,6 +94,36 @@ for (const [TYPE, interpreter] of TYPES) {
118
94
return code ;
119
95
} ;
120
96
97
+ // register once any interpreter
98
+ let alreadyRegistered = false ;
99
+
100
+ // allows lazy element features on code evaluation
101
+ let currentElement ;
102
+
103
+ const registerModule = ( { XWorker, interpreter, io } ) => {
104
+ // avoid multiple registration of the same interpreter
105
+ if ( alreadyRegistered ) return ;
106
+ alreadyRegistered = true ;
107
+
108
+ // automatically use the pyscript stderr (when/if defined)
109
+ // this defaults to console.error
110
+ function PyWorker ( ...args ) {
111
+ const worker = XWorker ( ...args ) ;
112
+ worker . onerror = ( { error } ) => io . stderr ( error ) ;
113
+ return worker ;
114
+ }
115
+
116
+ // enrich the Python env with some JS utility for main
117
+ interpreter . registerJsModule ( "_pyscript" , {
118
+ PyWorker,
119
+ get target ( ) {
120
+ return isScript ( currentElement )
121
+ ? currentElement . target . id
122
+ : currentElement . id ;
123
+ } ,
124
+ } ) ;
125
+ } ;
126
+
121
127
// define the module as both `<script type="py">` and `<py-script>`
122
128
// but only if the config didn't throw an error
123
129
if ( ! error ) {
@@ -133,10 +139,7 @@ for (const [TYPE, interpreter] of TYPES) {
133
139
main : {
134
140
...codeFor ( main ) ,
135
141
async onReady ( wrap , element ) {
136
- if ( shouldRegister ) {
137
- shouldRegister = false ;
138
- registerModule ( wrap ) ;
139
- }
142
+ registerModule ( wrap ) ;
140
143
141
144
// allows plugins to do whatever they want with the element
142
145
// before regular stuff happens in here
@@ -320,7 +323,7 @@ for (const [TYPE, interpreter] of TYPES) {
320
323
function PyWorker ( file , options ) {
321
324
const hooks = hooked . get ( "py" ) ;
322
325
// this propagates pyscript worker hooks without needing a pyscript
323
- // bootstrap + it passes arguments and enforces `pyodide`
326
+ // bootstrap + it passes arguments and it defaults to `pyodide`
324
327
// as the interpreter to use in the worker, as all hooks assume that
325
328
// and as `pyodide` is the only default interpreter that can deal with
326
329
// all the features we need to deliver pyscript out there.
0 commit comments