Skip to content

Commit

Permalink
Use Worker abstraction in SyncWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Feb 21, 2023
1 parent bf765e4 commit 0020493
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
48 changes: 16 additions & 32 deletions src/platform/web/worker-sync/SyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ import {FeatureSet} from "../../../features";
import {Logger} from "../../../logging/Logger";
import {ConsoleReporter} from "../../../logging/ConsoleReporter";
import assetPaths from "../sdk/paths/vite";
import {MessageBody, ResultBody, Worker} from "./Worker";

type Payload = object;

export enum SyncWorkerMessageType {
StartSync,
export enum SyncMessageType {
StartSync = "StartSync",
}

interface Message {
type: SyncWorkerMessageType,
payload: Payload
export interface StartSyncMessage extends MessageBody {
sessionInfo: ISessionInfo,
}

export interface StartSyncPayload extends Payload {
sessionInfo: ISessionInfo,
export interface StartSyncResult extends ResultBody {
success: boolean,
}

class SyncWorker {
export class SyncWorker extends Worker {
private _reconnector: Reconnector;
private _platform: WorkerPlatform;
private _storage: Storage;
private _sync: Sync;

async start(payload: StartSyncPayload): Promise<Payload> {
const sessionInfo = payload.sessionInfo;
constructor() {
super();
super.addHandler(SyncMessageType.StartSync, this.startSync.bind(this));
}

async startSync(message: StartSyncMessage): Promise<StartSyncResult> {
const sessionInfo = message.sessionInfo;
console.log(`Starting sync worker for session with id ${sessionInfo.id}`);

this._platform = new WorkerPlatform({assetPaths});
Expand Down Expand Up @@ -91,25 +94,6 @@ class SyncWorker {
logger,
});

return payload;
return { success: true };
}
}

const worker = new SyncWorker();
// @ts-ignore
self.syncWorker = worker;

self.onmessage = (event: MessageEvent) => {
const data: Message = event.data;

let promise: Promise<Payload>;
switch (data.type) {
case SyncWorkerMessageType.StartSync:
promise = worker.start(data.payload as StartSyncPayload);
break;
}

promise.then((reply: Payload) => {
postMessage(reply);
}).catch(error => console.error(error))
};
6 changes: 3 additions & 3 deletions src/platform/web/worker-sync/SyncWorkerPool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {SyncWorkerMessageType} from "./SyncWorker";
import {SyncMessageType} from "./SyncWorker";
import {SessionInfoStorage} from "../../../matrix/sessioninfo/localstorage/SessionInfoStorage";

export type SessionId = string;
Expand Down Expand Up @@ -31,8 +31,8 @@ export class SyncWorkerPool {

const sessionInfo = await this._sessionInfoStorage.get(sessionId);
worker.postMessage({
type: SyncWorkerMessageType.StartSync,
payload: {
type: SyncMessageType.StartSync,
body: {
sessionInfo: sessionInfo
},
});
Expand Down
6 changes: 5 additions & 1 deletion src/platform/web/worker-sync/sync-worker.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import "./SyncWorker";
import {SyncWorker} from "./SyncWorker";

const worker = new SyncWorker();
worker.start();
self.syncWorker = worker;

0 comments on commit 0020493

Please sign in to comment.