Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ports/webassembly: Add support for browser WebWorkers #12118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

emirhalici
Copy link

@emirhalici emirhalici commented Jul 29, 2023

This PR adds MicroPython WebAssembly support to WebWorkers on browsers. Changes are similar and adhere to existing code style. A small section is added to README.md file about the new support.

WASM port works great so far but it is always tied to main thread on browsers. This blocks us from loading/disposing multiple instances of the module or put the work outside of main thread to not block it. It looks like WASM side has no problems with running on a WebWorker, being loaded to a worker, and disposal when worker is disposed. This opens a door for having clean instances of the module whenever needed in a WebWorker. Getting it up and running on WebWorker's without modifying source code meant emulating global window and document objects, adding addEventListener and removeEventListener functionality to each webWorker file. Having this change merged removes all this hack and boilterplate code and allows a very simple single API for receiving messages from WASM module.

As WebWorkers don't have direct access to DOM, using a callback that has been previously set to the Module works. This means WASM module can have a dedicated communication line to push buffers to webWorker. When webWorkerEventCallback is not set on Module, webWorker environment is ignored.

'myWebWorkerFile.js':

importScripts('micropython.js');
// ...
Module.webWorkerEventCallback = (event) => {
  console.log(`Received an event from MicroPython!`);
  const buffer = event.detail;
};

Please let me know if I need to take further action, I'd like to contribute this change to the repository. Thanks

When Module is provided webWorkerEventCallback and environment
is webworker, callback is used instead to push buffer messages.

Signed-off-by: Emir Halıcı <[email protected]>
@emirhalici
Copy link
Author

@dpgeorge Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants