forked from element-hq/hydrogen-web
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Communication between main thread and sync worker #13
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
psrpinto
added
the
upstream-contribution-candidate
Supposed to go to upstream as a PR once base branch is merged
label
Mar 27, 2023
11 tasks
ashfame
approved these changes
Mar 27, 2023
psrpinto
force-pushed
the
sync-worker-02-communication
branch
from
March 28, 2023 14:49
ad4c1f9
to
68f9e94
Compare
ashfame
approved these changes
Mar 28, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
sync-worker
upstream-contribution-candidate
Supposed to go to upstream as a PR once base branch is merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR defines the protocol of communication between main thread and workers. The idea is to provide primitives that can be used by all workers, not just the sync worker. In the future, other workers could be adapted to leverage these primitives, e.g. the service worker and the olm worker, or new workers that might be implemented.
There are two types of communication:
Request
/Response
. Upon receiving aRequest
, the worker will do whatever it needs to do, and then return aResponse
. The requester is free to ignore the response, but the worker will always send one.Event
s to aBroadcastChannel
, and main thread can listen to said events.Primitives
The primitives introduced here are:
BaseWorker
: generic code that applies to all workers.SharedWorker
: leveragesBaseWorker
to implement aSharedWorker
.DedicatedWorker
could be implemented as well.WorkerProxy
: allows running code in a worker as if it would be running in the main thread. Sends aRequest
to the worker, and optionally waits forResponse
and returns result.EventBus
: allows subscribing to specific event types (more on types below).Types
The API of each worker is specified by defining types for its
Request
s,Response
s andEvent
s. For example, the sync worker would define these types:Example usage
Worker side:
Main thread side: