8000 SharedWorker Atomics by WebReflection · Pull Request #7 · WebReflection/sabayon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

SharedWorker Atomics #7

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 6 commits into
base: main
Choose a base branch
from
Open

SharedWorker Atomics #7

wants to merge 6 commits into from

Conversation

WebReflection
Copy link
Owner
@WebReflection WebReflection commented Dec 4, 2024

This MR would like to introduce Atomics for the SharedWorker use case, considering the folowing caveats:

  • same way a main thread should never be blocked, a SharedWorker should likely never be blocked too. This means that only Atomics.notify and Atomics.waitAsync are currently implemented and I believe that's the right thing to do
  • with SharedWorker, there is one end-point and potentially multiple ports to satisfy. The current logic allows each port to send a SAB and wait asynchronously its notification but of course, if it's a Worker itself connecting to a SharedWorker, it could wait synchronously, it's only the SharedWorker that won't allow blocking wait in it.
  • ports don't communicate with each other ... a SAB shared from one port won't ever affect other SAB as it is already for the current standard implementation
  • because sync notification is not possible, SharedWorker won't ever orchestrate the ServiceWorker dance
  • because of the orchestration needed AOT to make it work, each port is automatically started once a SharedWorker is created (arguably the most awkward API I've seen to date).

That's it, once this is tested and merged, it opens doors to coincident project as there could be things, at that point, that were previously not possible, from either workers or the main thread.

@WebReflection WebReflection force-pushed the shared-worker branch 3 times, most recently from 1cb1378 to f942c7e Compare December 4, 2024 16:35
@WebReflection
Copy link
Owner Author
WebReflection commented Dec 4, 2024

... and "who am I kidding" ... I've just realized my polyfill works perfectly fine but native implementation wouldn't, accordingly with this SO answer, which also makes somehow sense ... back to the whiteboard then, SharedWorker in here should be patched no matter what, it's not a SAB affair, it never was 🤦

@khanhicetea
Copy link

Wait, don't tell me your plan is use this polyfill for shared worker sql.js lib ? 😃 So curious!

@WebReflection
Copy link
Owner Author

@khanhicetea the thing is, once this works out of the box the whole API can be forwarded asynchronously without me needing to maintain or update it, including statements and whatnot, it should "just work" ... except the each will probably need some orchestration anyway but that'd be it.

Currently though, I am not sure if this can ever possibly work but I am trying ... good thing it's a PR I can close and forget about if I'll fail.

@WebReflection WebReflection marked this pull request as draft December 5, 2024 11:51
@WebReflection
Copy link
Owner Author
WebReflection commented Dec 5, 2024

OK, latest works as expected with or without SharedArrayBuffer around, which is cool and everything but before making this library bigger than it needs to be, I'd like to temporarily publish this with a different name and see how well it plays, or breaks, with existent libraries based on this approach ... hence it'd be draft for a while, or maybe forever, who knows.

@WebReflection
Copy link
Owner Author

Published as sabayon-shared-worker until I am happy about all it does: https://github.com/WebReflection/sabayon?tab=readme-ov-file#a-sharedworker-module-is-on-its-way-

@WebReflection
Copy link
Owner Author

damn it ... with latest "lazy all the things" approach I really think this should go in ...

@WebReflection WebReflection marked this pull request as ready for review December 5, 2024 17:23
@WebReflection
Copy link
Owner Author

@khanhicetea btw, sql.js in SharedWorker is already a thing ... here I am trying to see if I can go beyond that library but SharedWorkers are a weird beast ... just the fact you need same-origin policy no matter what makes these less portable for libraries' authors.

@khanhicetea
Copy link

just the fact you need same-origin policy no matter what makes these less portable for libraries' authors.

So you mean the shared-worker script must be hosted in same-origin to work , right ?

Or the headers : Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp ? (these headers is a reason I don't use any SAB - as I understand it's safe for end-user but for developing a web app, it totally BIG GATE KEEPER from many features )

😄 My browser API knowledge level is a step far behind yours :( So sometimes I really try to understand what you do, but end with guessing about basic concepts what I currently know.

@WebReflection
Copy link
Owner Author

@khanhicetea

these headers is a reason I don't use any SAB

this project is the reason you shouldn't care because it enables SAB even when those headers are not defined, falling back with either mini-coi or XHR when sync SAB from workers is desired.

So you mean the shared-worker script must be hosted in same-origin to work , right ?

AFAIK yes, and those headers might not be enough ... those are also not necessary for SharedWorker if such file comes already from the same origin ... having a worker file in your assets is common enough, no reason to panic for this use case ... but as that's async only and SAB cannot possibly work (it's patched in here) you really could use sabayon as SharedWorker and call it a day ... actually this might be a good reason already to convince me this PR should land officially.

@WebReflection
Copy link
Owner Author
WebReflection commented Dec 6, 2024

Somehow a change of direction ... as SharedWorker can't literallly deal with SAB, I wonder if a simple postMessage orchestrated dance to eventually bring the same, as it's async anyway, would be enough, without me making this module less interesting and more bloated ... still working on it, but there's this mind-stopper to me that keeps telling me: this is not the right place to do so, it'll be just overhead for very little gain!

@nickchomey
Copy link

Please forgive me if I'm not being helpful here, but I wonder if the Broadcast Channel API would be helpful or perhaps even more appropriate here? Particularly since this is for using SharedWorkers, which are typically used across many tabs, so Broadcast Channel would allow for easy communication across all of them. I can imagine that it has particular limitations or extra overhead though, so perhaps its not appropriate for what youre trying to do

@WebReflection
Copy link
Owner Author

I’m not sure I’m following … but within the sharedworker you can grab ports and do whatever you like, this module is to enable Atomics and SAB and broadcasted atomic operation makes no sense to me because you need to create a SAB and that cannot be broadcasted … as others wouldn’t have the SAB in their code, or promises waiting for its notification… but again, you can add broadcast yourself if needed

@nickchomey
Copy link

Again, apologies - evidently my suggestion was not helpful. Please carry on!

@WebReflection
Copy link
Owner Author

as update, we're here: DallasHoff/sqlocal#39 (comment)

my polyfill works over something that shouldn't and I don't think that's a way forward ... rather a trap for the future of this module or any other module based on this.

Let's see how it goes in there though ... we're sharing the same goal, I am confident something will happen sooner than later.

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.

3 participants
0