[next] xworker.sync pollution example (#1659)

This commit is contained in:
Andrea Giammarchi
2023-08-31 15:37:17 +02:00
committed by GitHub
parent 4cc9647dc6
commit da3b43abdd
6 changed files with 24 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@@ -2,6 +2,7 @@ import "@ungap/with-resolvers";
import { $ } from "basic-devtools";
import { define, XWorker } from "polyscript";
import { htmlDecode } from "./utils.js";
import sync from "./sync.js";
// this is imported as string (via rollup)
import display from "./display.py";
@@ -179,6 +180,9 @@ define("py", {
env: "py-script",
interpreter: "pyodide",
...workerHooks,
onWorkerReady(_, xworker) {
assign(xworker.sync, sync);
},
onBeforeRun(pyodide, element) {
currentElement = element;
bootstrapNodeAndPlugins(pyodide, element, before, "onBeforeRun");
@@ -269,8 +273,10 @@ export function PyWorker(file, options) {
// as the interpreter to use in the worker, as all hooks assume that
// and as `pyodide` is the only default interpreter that can deal with
// all the features we need to deliver pyscript out there.
return XWorker.call(new Hook(null, workerHooks), file, {
const xworker = XWorker.call(new Hook(null, workerHooks), file, {
...options,
type: "pyodide",
});
assign(xworker.sync, sync);
return xworker;
}

View File

@@ -0,0 +1,5 @@
export default {
sleep(seconds) {
return new Promise(($) => setTimeout($, seconds * 1000));
},
};

View File

@@ -1,5 +1,9 @@
from pyscript import display
from pyscript import display, sync
import a
display("Hello World", target="test", append=True)
print("sleeping")
sync.sleep(1)
print("awake")

View File

@@ -22,4 +22,5 @@ export namespace hooks {
let codeAfterRunWorkerAsync: Set<string>;
}
declare let config: any;
import sync from "./sync.js";
export {};

4
pyscript.core/types/sync.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
declare namespace _default {
function sleep(seconds: any): Promise<any>;
}
export default _default;