-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpyodide.js
More file actions
49 lines (40 loc) · 1.17 KB
/
pyodide.js
File metadata and controls
49 lines (40 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let pyodideWorker = new Worker("/webworker.js?" + '?' + Math.random());
// if (crossOriginIsolated) {
// console.log('cross origin isolated')
// interruptBuffer = new Uint8Array(new SharedArrayBuffer(1));
// pyodideWorker.postMessage({ cmd: "setInterruptBuffer", interruptBuffer });
// }
function interruptExecution() {
// interruptBuffer[0] = 2;
}
let outputCallBack = (data) => console.log(data.content)
function setOutputCallBack(c) {
outputCallBack = c
}
const callbacks = {};
pyodideWorker.onmessage = (event) => {
if (event.data.finish) {
const { id, ...data } = event.data;
const onSuccess = callbacks[id];
delete callbacks[id];
onSuccess(data);
}
outputCallBack(event.data)
};
const asyncRun = (() => {
let id = 0; // identify a Promise
return (script, context) => {
// the id could be generated more carefully
id = (id + 1) % Number.MAX_SAFE_INTEGER;
return new Promise((onSuccess) => {
callbacks[id] = onSuccess;
// interruptBuffer[0] = 0;
pyodideWorker.postMessage({
...context,
python: script,
id,
});
});
};
})();
export { asyncRun, interruptExecution, setOutputCallBack };