File size: 744 Bytes
349fb8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const pythonWorker = new Worker("python-worker.js");

let _onPythonInitialized = null;
let pythonInitialized = new Promise((onSuccess) => _onPythonInitialized = onSuccess);
let _onReceivedCallback = null;

pythonWorker.onmessage = (event) => {
    if (event.data == "initialized") {
        _onPythonInitialized();
    } else {
        _onReceivedCallback(event.data);
    }
};

documentUrl = document.URL;

// initialize worker
pythonWorker.postMessage({ documentUrl, micropipIncludePre, pythonModuleName });

async function jsConnect(receiveCallback) {
    _onReceivedCallback = receiveCallback;
    await pythonInitialized;
    console.log("Python engine initialized!");
}

async function jsSend(data) {
    pythonWorker.postMessage(data);
}