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

Communication between main thread and sync worker #13

Merged
merged 18 commits into from
Mar 28, 2023

Conversation

psrpinto
Copy link
Member

@psrpinto psrpinto commented Mar 21, 2023

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:

  1. Asking a worker to run a command: implemented in the form of a Request/Response. Upon receiving a Request, the worker will do whatever it needs to do, and then return a Response. The requester is free to ignore the response, but the worker will always send one.
  2. Receiving events from a worker: worker publishes Events to a BroadcastChannel, and main thread can listen to said events.

Primitives

The primitives introduced here are:

  1. Worker side:
    • BaseWorker: generic code that applies to all workers.
    • SharedWorker: leverages BaseWorker to implement a SharedWorker.
    • In the future, when the need arises, a DedicatedWorker could be implemented as well.
  2. Main thread side:
    • WorkerProxy: allows running code in a worker as if it would be running in the main thread. Sends a Request to the worker, and optionally waits for Response 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 Requests, Responses and Events. For example, the sync worker would define these types:

export interface StartSyncRequest extends Request {
    type: "StartSync";
    data: {
        sessionId: string,
    }
}

export interface StartSyncResponse extends Response {
    request: StartSyncRequest;
    data: {}
}

export interface SyncStatusChanged extends Event {
    type: "StatusChanged";
    data: {
        newValue: string,
    }
}

Example usage

Worker side:

export class SyncWorker extends SharedWorker {
    constuctor() {
        super();
        this.setHandler("StartSync", this.startSync.bind(this));
    }

    async startSync(request: StartSyncRequest, response: StartSyncResponse): Promise<StartSyncResponse> {
        // Do something.
        return response;
    }
}

Main thread side:

const worker = new SharedWorker("worker.js");
const workerProxy = new WorkerProxy(worker);

const request: StartSyncRequest = {
    id: "1234",
    type: "StartSync",
    data: {
        sessionId: "abc123",
    }
};

const response = await workerProxy.sendAndWaitForResponse(request) as StartSyncResponse;
console.log(response);

@psrpinto psrpinto self-assigned this Mar 21, 2023
This was referenced Mar 21, 2023
@psrpinto psrpinto marked this pull request as ready for review March 23, 2023 16:23
@psrpinto psrpinto requested a review from ashfame March 23, 2023 16:23
@psrpinto psrpinto added the upstream-contribution-candidate Supposed to go to upstream as a PR once base branch is merged label Mar 27, 2023
@psrpinto psrpinto changed the base branch from integration-sync-worker to master March 27, 2023 15:24
src/platform/workers/main.js Outdated Show resolved Hide resolved
src/platform/web/worker/polyfill.js Show resolved Hide resolved
src/platform/workers/BaseWorker.ts Outdated Show resolved Hide resolved
src/platform/workers/sync/SyncWorker.ts Show resolved Hide resolved
@psrpinto psrpinto force-pushed the sync-worker-02-communication branch from ad4c1f9 to 68f9e94 Compare March 28, 2023 14:49
Copy link
Member

@ashfame ashfame left a comment

Choose a reason for hiding this comment

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

🚀

@psrpinto psrpinto merged commit 9a54dcd into master Mar 28, 2023
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants