8000 Fix extensions regression bug (#489) · TutorExilius/pyscript@f60dd6a · GitHub
[go: up one dir, main page]

Skip to content

Commit f60dd6a

Browse files
authored
Fix extensions regression bug (pyscript#489)
* add output div visibility rule and make sure CustomWidget is being registered * add new runAfterRuntimeInitialized method that classes can use to run code only after runtime actually initialized * replace setTimout with proper event to wait for runtime to be completed * replace setTimout on pyinpytbox with proper event to wait for runtime to be completed
1 parent d6a88d4 commit f60dd6a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

pyscriptjs/src/components/base.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class BaseEvalElement extends HTMLElement {
184184
this.errorElement.children[this.errorElement.children.length - 1].setAttribute('error', '');
185185
this.errorElement.hidden = false;
186186
this.errorElement.style.display = 'block';
187+
this.errorElement.style.visibility = 'visible';
187188
}
188189
} // end evaluate
189190

@@ -200,6 +201,16 @@ export class BaseEvalElement extends HTMLElement {
200201
console.log(err);
201202
}
202203
} // end eval
204+
205+
runAfterRuntimeInitialized(callback: () => Promise<void>){
206+
pyodideLoaded.subscribe(value => {
207+
if ('runPythonAsync' in value) {
208+
setTimeout(async () => {
209+
await callback();
210+
}, 100);
211+
}
212+
});
213+
}
203214
}
204215

205216
function createWidget(name: string, code: string, klass: string) {
@@ -270,6 +281,7 @@ function createWidget(name: string, code: string, klass: string) {
270281
}
271282
}
272283
}
284+
const xPyWidget = customElements.define(name, CustomWidget);
273285
}
274286

275287
export class PyWidget extends HTMLElement {

pyscriptjs/src/components/pybutton.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ export class PyButton extends BaseEvalElement {
6565

6666
// now that we appended and the element is attached, lets connect with the event handlers
6767
// defined for this widget
68-
setTimeout(async () => {
68+
this.runAfterRuntimeInitialized(async () => {
6969
await this.eval(this.code);
7070
await this.eval(registrationCode);
7171
console.log('registered handlers');
72-
}, 4000);
72+
});
7373

7474
console.log('py-button connected');
7575
}

pyscriptjs/src/components/pyinputbox.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export class PyInputBox extends BaseEvalElement {
4343

4444
// TODO: For now we delay execution to allow pyodide to load but in the future this
4545
// should really wait for it to load..
46-
setTimeout(async () => {
46+
this.runAfterRuntimeInitialized(async () => {
4747
await this.eval(this.code);
4848
await this.eval(registrationCode);
4949
console.log('registered handlers');
50-
}, 4000);
50+
});
5151
}
5252
}

0 commit comments

Comments
 (0)
0