mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 18:27:29 -05:00
[next] xworker.sync pollution example (#1659)
This commit is contained in:
committed by
GitHub
parent
4cc9647dc6
commit
da3b43abdd
File diff suppressed because one or more lines are too long
@@ -2,6 +2,7 @@ import "@ungap/with-resolvers";
|
|||||||
import { $ } from "basic-devtools";
|
import { $ } from "basic-devtools";
|
||||||
import { define, XWorker } from "polyscript";
|
import { define, XWorker } from "polyscript";
|
||||||
import { htmlDecode } from "./utils.js";
|
import { htmlDecode } from "./utils.js";
|
||||||
|
import sync from "./sync.js";
|
||||||
|
|
||||||
// this is imported as string (via rollup)
|
// this is imported as string (via rollup)
|
||||||
import display from "./display.py";
|
import display from "./display.py";
|
||||||
@@ -179,6 +180,9 @@ define("py", {
|
|||||||
env: "py-script",
|
env: "py-script",
|
||||||
interpreter: "pyodide",
|
interpreter: "pyodide",
|
||||||
...workerHooks,
|
...workerHooks,
|
||||||
|
onWorkerReady(_, xworker) {
|
||||||
|
assign(xworker.sync, sync);
|
||||||
|
},
|
||||||
onBeforeRun(pyodide, element) {
|
onBeforeRun(pyodide, element) {
|
||||||
currentElement = element;
|
currentElement = element;
|
||||||
bootstrapNodeAndPlugins(pyodide, element, before, "onBeforeRun");
|
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
|
// 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
|
// and as `pyodide` is the only default interpreter that can deal with
|
||||||
// all the features we need to deliver pyscript out there.
|
// 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,
|
...options,
|
||||||
type: "pyodide",
|
type: "pyodide",
|
||||||
});
|
});
|
||||||
|
assign(xworker.sync, sync);
|
||||||
|
return xworker;
|
||||||
}
|
}
|
||||||
|
|||||||
5
pyscript.core/src/sync.js
Normal file
5
pyscript.core/src/sync.js
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export default {
|
||||||
|
sleep(seconds) {
|
||||||
|
return new Promise(($) => setTimeout($, seconds * 1000));
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,5 +1,9 @@
|
|||||||
from pyscript import display
|
from pyscript import display, sync
|
||||||
|
|
||||||
import a
|
import a
|
||||||
|
|
||||||
display("Hello World", target="test", append=True)
|
display("Hello World", target="test", append=True)
|
||||||
|
|
||||||
|
print("sleeping")
|
||||||
|
sync.sleep(1)
|
||||||
|
print("awake")
|
||||||
|
|||||||
1
pyscript.core/types/core.d.ts
vendored
1
pyscript.core/types/core.d.ts
vendored
@@ -22,4 +22,5 @@ export namespace hooks {
|
|||||||
let codeAfterRunWorkerAsync: Set<string>;
|
let codeAfterRunWorkerAsync: Set<string>;
|
||||||
}
|
}
|
||||||
declare let config: any;
|
declare let config: any;
|
||||||
|
import sync from "./sync.js";
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
4
pyscript.core/types/sync.d.ts
vendored
Normal file
4
pyscript.core/types/sync.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
declare namespace _default {
|
||||||
|
function sleep(seconds: any): Promise<any>;
|
||||||
|
}
|
||||||
|
export default _default;
|
||||||
Reference in New Issue
Block a user