Skip to content

Commit

Permalink
Create instance of HomeserverApi
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Feb 20, 2023
1 parent 7f75ee5 commit 274b73c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/platform/web/Platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ export class Platform {
this._serviceWorkerHandler.registerAndStart(assetPaths.serviceWorker);
}
this._syncWorkerPool = null;
if (assetPaths.syncWorker && window.Worker) {
this._syncWorkerPool = new SyncWorkerPool(this._assetPaths.syncWorker);
this._syncWorkerPool.add("1646528482480255");
}
this.notificationService = undefined;
// Only try to use crypto when olm is provided
if(this._assetPaths.olm) {
Expand All @@ -179,6 +175,11 @@ export class Platform {
this.mediaDevices = new MediaDevicesWrapper(navigator.mediaDevices);
this.webRTC = new DOMWebRTC();
this._themeLoader = import.meta.env.DEV? null: new ThemeLoader(this);

if (assetPaths.syncWorker && window.Worker) {
this._syncWorkerPool = new SyncWorkerPool(this._assetPaths.syncWorker, this.sessionInfoStorage);
this._syncWorkerPool.add("1646528482480255");
}
}

async init() {
Expand Down
29 changes: 28 additions & 1 deletion src/platform/web/worker-sync/SyncWorker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {ISessionInfo} from "../../../matrix/sessioninfo/localstorage/SessionInfoStorage";
import {HomeServerApi} from "../../../matrix/net/HomeServerApi";
import {createFetchRequest} from "../dom/request/fetch";
import {Clock} from "../dom/Clock";
import {Reconnector} from "../../../matrix/net/Reconnector";
import {ExponentialRetryDelay} from "../../../matrix/net/ExponentialRetryDelay";
import {OnlineStatus} from "../dom/OnlineStatus";

type Payload = object;

Expand All @@ -16,8 +22,29 @@ export interface StartSyncPayload extends Payload {
}

class SyncWorker {
private _clock: Clock;
private _reconnector: Reconnector;

async start(payload: StartSyncPayload): Promise<Payload> {
console.log(`Starting sync for session with id ${payload.sessionInfo.id}`);
const sessionInfo = payload.sessionInfo;
console.log(`Starting sync worker for session with id ${sessionInfo.id}`);

this._clock = new Clock;

this._reconnector = new Reconnector({
onlineStatus: new OnlineStatus(),
retryDelay: new ExponentialRetryDelay(this._clock.createTimeout),
createMeasure: this._clock.createMeasure
});

const hsApi = new HomeServerApi({
homeserver: sessionInfo.homeserver,
accessToken: sessionInfo.accessToken,
request: createFetchRequest(this._clock.createTimeout),
reconnector: this._reconnector,
});


return payload;
}
}
Expand Down

0 comments on commit 274b73c

Please sign in to comment.