Skip to content
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

Implement async using workers #46

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

AntonPieper
Copy link

@AntonPieper AntonPieper commented Feb 21, 2024

I toyed around with Web Workers, Atomics, SharedArrayBuffers and OffscreenCanvas and this actually makes most async tasks possible without using asyncify! This does not require any dependencies or code transformations, it is just the browser's web APIs.

Changes

  • Adds a raylib-wrapper.js file that is the new main entry point
  • raylib.js is run on a worker (not called raylib-worker.js to make git happy)
  • All necessary state is synchronized using SharedArrayBuffers
  • sleeping is done on the Worker using Atomics#wait
  • adds a server.py script due to security requirements for SharedArrayBuffer

Closes #45

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed for GitHub pages, due to modern requirements for SharedArrayBuffer (after Spectre) and is a bit tacky by using a service worker that reloads the page with the needed headers set.

Ideally, GitHub pages would just set this header (like the the CORS headers), but that's not the case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying all the examples, I instead opted for backwards compatibility and added another example that does not use the raylib_js_set_entry function (straight from the official examples).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this would be called raylib.js and the other file would be called raylib-worker.js (instead of raylib.js), but that would confuse git

Copy link
Author

@AntonPieper AntonPieper Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because python -m http.server has no CLI options, this just starts http.server with the additional headers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to enable-threads.js, python3 -m http.server should work too, however this is more clean

@AntonPieper
Copy link
Author

Relates to #22

@Azer0s
Copy link

Azer0s commented Feb 21, 2024

Oh damn, I was working on the same thing, you beat me to it!
However, I would fully remove the backwards compatibility.

- automatically creates properties on both sides
- types are synchronized for IDE users
- because we don't await start anyway, I removed the initialized reply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Async with Workers, SharedArrayBuffer and Atomics
2 participants