Opinions on worker/main thread communication design #3053
Unanswered
SpanishPear
asked this question in
Q&A
Replies: 1 comment
-
JavaScript (and Agent v1) uses a push-based design where messages are pushed to the receiver, I think channel is the correct way to convert this to a poll-based API.
This design is very similar to Agent v2 (#2773), which is blocked by rustwasm/gloo#251. I believe a M:N worker message handle should implement Is it possible to proceed rustwasm/gloo#251 with the current design? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
This is a long question on how to best design the communication between worker and main thread when it comes to lots of state - I've tried to lay out the probelem space well but it's late and I'm tired so please let me know if you'd like more details!
I'm trying to refactor a large yew application with a web worker to be more idiomatic and nicer deisgned...
There is a large structure that holds the state of this application that has previously lived in both the application, and the worker itself, i.e. sometimes, fields inside that struct need to be updated, and so I would send a request to the worker to perform some calculations, and send back the new "updated" struct, so that the app could have a new updated copy. I'll note also that the whole struct (or most of it) needs to be sent, as certain calculations to update 1 field, would make use of lots of the fields.
This started becoming more and more unideal the larger the struct became, and also means that for some things (i.e. a web component that renders one field of the struct, i.e. vec of numbers) - to update would have to send a message to the worker, and break the flow (i.e. most of the time the component can't send a message and wait for a response from that message, control flow goes back to the worker callback).
I've attempted one new approach:
And this does almost work - with the exception that I can't figure out how to subscribe to the channel for new updates, and re-render if (and only if) a new vec of numbers is in the channel. It also has the issue of what happens if in future when multiple types of messages are sent (a vec of images per say) and only the component that renders a vec of images should recieve the vec of images, not the numbers component - it will end up dependant on rendering order, whoever .recv().await first!
I.e. - if a button press adds to the vec of numbers, what I would hope to happen is:
another example:
But, I'm not quite sure how possible this is.
It's possible that channels are not even the right for this type of sending data back/forth a lot that determines the data for which structs are rendered, etc, and maybe a big struct at the application root is the best way :/
Let me know if a minimal example of the old method would help
EDIT:
Since the original posing of this question, I've initially gone with:
Beta Was this translation helpful? Give feedback.
All reactions