pull/12118/merge
Emir Halıcı 2024-03-16 09:33:25 +08:00 zatwierdzone przez GitHub
commit 50bd4c0712
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
3 zmienionych plików z 18 dodań i 2 usunięć

Wyświetl plik

@ -88,6 +88,20 @@ MicroPython code execution will suspend the browser so be sure to atomize usage
within this environment. Unfortunately interrupts have not been implemented for the
browser.
Running with WebWorkers on browsers
-----------------
To enable WebWorker support and receive messages outside of DOM, set `Module.webWorkerEventCallback` to a function that'll receive the events.
```
importScripts('micropython.js');
// ...
Module.webWorkerEventCallback = (event) => {
console.log(`Received an event from MicroPython!`);
};
```
WebWorkers on the web gives more flexibility to developers when consuming the WASM module. Example cases where this is useful are: not blocking main thread, creating and disposing multiple instances on a single session, using multiple instances for different purposes separate from each other...
Testing
-------

Wyświetl plik

@ -27,7 +27,9 @@
mergeInto(LibraryManager.library, {
mp_js_write: function(ptr, len) {
const buffer = HEAPU8.subarray(ptr, ptr + len)
if (ENVIRONMENT_IS_NODE) {
if (ENVIRONMENT_IS_WORKER && typeof Module.webWorkerEventCallback === 'function') {
Module.webWorkerEventCallback({detail: buffer});
} else if (ENVIRONMENT_IS_NODE) {
process.stdout.write(buffer);
} else {
const printEvent = new CustomEvent('micropython-print', { detail: buffer });

Wyświetl plik

@ -35,7 +35,7 @@ var mainProgram = function()
MP_JS_EPOCH = Date.now();
if (typeof window === 'undefined' && require.main === module) {
if (ENVIRONMENT_IS_NODE && require.main === module) {
var fs = require('fs');
var heap_size = 128 * 1024;
var contents = '';