Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 314be19

Browse files
committed
replace using interpreter promise with the interpreter instance itself. Also properly wait for it to be fully initialized before using on the base widget class
1 parent 442af7b commit 314be19

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { loadedEnvironments, mode, pyodideLoaded } from '../stores';
22
import { guidGenerator, addClasses } from '../utils';
33
// Premise used to connect to the first available pyodide interpreter
4-
let pyodideReadyPromise;
4+
let runtime;
55
let environments;
66
let currentMode;
77
let Element;
88

9+
910
pyodideLoaded.subscribe(value => {
10-
pyodideReadyPromise = value;
11+
runtime = value;
1112
});
1213
loadedEnvironments.subscribe(value => {
1314
environments = value;
@@ -63,7 +64,7 @@ export class BaseEvalElement extends HTMLElement {
6364
}
6465

6566
async getSourceFromFile(s: string): Promise<string> {
66-
const pyodide = await pyodideReadyPromise;
67+
const pyodide = runtime;
6768
const response = await fetch(s);
6869
this.code = await response.text();
6970
return this.code;
@@ -101,7 +102,7 @@ export class BaseEvalElement extends HTMLElement {
101102

102103
async evaluate(): Promise<void> {
103104
console.log('evaluate');
104-
const pyodide = await pyodideReadyPromise;
105+
const pyodide = runtime;
105106
let source: string;
106107
let output;
107108
try {
@@ -154,7 +155,7 @@ export class BaseEvalElement extends HTMLElement {
154155

155156
async eval(source: string): Promise<void> {
156157
let output;
157-
const pyodide = await pyodideReadyPromise;
158+
const pyodide = runtime;
158159

159160
try {
160161
output = await pyodide.runPythonAsync(source);
@@ -193,25 +194,39 @@ function createWidget(name: string, code: string, klass: string) {
193194
// ideally we can just wait for it to load and then run. To do
194195
// so we need to replace using the promise and actually using
195196
// the interpreter after it loads completely
196-
setTimeout(() => {
197-
this.eval(this.code).then(() => {
198-
this.proxy = this.proxyClass(this);
199-
console.log('proxy', this.proxy);
200-
this.proxy.connect();
201-
this.registerWidget();
202-
});
203-
}, 2000);
197+
// setTimeout(() => {
198+
// this.eval(this.code).then(() => {
199+
// this.proxy = this.proxyClass(this);
200+
// console.log('proxy', this.proxy);
201+
// this.proxy.connect();
202+
// this.registerWidget();
203+
// });
204+
// }, 2000);
205+
pyodideLoaded.subscribe(value => {
206+
console.log("RUNTIME READY", value)
207+
if ("runPythonAsync" in value){
208+
runtime = value;
209+
setTimeout(() => {
210+
this.eval(this.code).then(() => {
211+
this.proxy = this.proxyClass(this);
212+
console.log('proxy', this.proxy);
213+
this.proxy.connect();
214+
this.registerWidget();
215+
});
216+
}, 1000);
217+
}
218+
});
204219
}
205220

206221
async registerWidget() {
207-
const pyodide = await pyodideReadyPromise;
222+
const pyodide = runtime;
208223
console.log('new widget registered:', this.name);
209224
pyodide.globals.set(this.id, this.proxy);
210225
}
211226

212227
async eval(source: string): Promise<void> {
213228
let output;
214-
const pyodide = await pyodideReadyPromise;
229+
const pyodide = runtime;
215230
try {
216231
output = await pyodide.runPythonAsync(source);
217232
this.proxyClass = pyodide.globals.get(this.klass);
@@ -306,14 +321,14 @@ export class PyWidget extends HTMLElement {
306321
}
307322

308323
async getSourceFromFile(s: string): Promise<string> {
309-
const pyodide = await pyodideReadyPromise;
324+
const pyodide = runtime;
310325
const response = await fetch(s);
311326
return await response.text();
312327
}
313328

314329
async eval(source: string): Promise<void> {
315330
let output;
316-
const pyodide = await pyodideReadyPromise;
331+
const pyodide = runtime;
317332
try {
318333
output = await pyodide.runPythonAsync(source);
319334
if (output !== undefined) {

0 commit comments

Comments
 (0)