forked from element-hq/hydrogen-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Automattic/sync-worker-kickoff
Sync worker kickoff
- Loading branch information
Showing
12 changed files
with
224 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import {ObservableValue} from "../observable/value"; | ||
import {SyncStatus} from "./Sync"; | ||
|
||
export interface ISync { | ||
get status(): ObservableValue<SyncStatus>; | ||
get error(): Error | null; | ||
start(): Promise<void>; | ||
stop(): void; | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import {ISync} from "../../../matrix/ISync"; | ||
import {Sync} from "../../../matrix/Sync"; | ||
import {RequestScheduler} from "../../../matrix/net/RequestScheduler"; | ||
import {Session} from "../../../matrix/Session"; | ||
import {Logger} from "../../../logging/Logger"; | ||
import {Storage} from "../../../matrix/storage/idb/Storage"; | ||
import {FeatureSet} from "../../../features"; | ||
import {SyncProxy} from "./SyncProxy"; | ||
|
||
type Options = { | ||
logger: Logger; | ||
features: FeatureSet; | ||
} | ||
|
||
type MakeOptions = { | ||
scheduler: RequestScheduler; | ||
storage: Storage; | ||
session: Session; | ||
} | ||
|
||
export class SyncFactory { | ||
private readonly _logger: Logger; | ||
private readonly _features: FeatureSet; | ||
|
||
constructor(options: Options) { | ||
const {logger, features} = options; | ||
this._logger = logger; | ||
this._features = features; | ||
} | ||
|
||
make(options: MakeOptions): ISync { | ||
const {scheduler, storage, session} = options; | ||
let runSyncInWorker = this._features.sameSessionInMultipleTabs; | ||
|
||
if (typeof SharedWorker === "undefined") { | ||
runSyncInWorker = false; | ||
} | ||
|
||
if (runSyncInWorker) { | ||
return new SyncProxy({session}); | ||
} | ||
|
||
return new Sync({ | ||
logger: this._logger, | ||
hsApi: scheduler.hsApi, | ||
storage, | ||
session, | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import {ISync} from "../../../matrix/ISync"; | ||
import {ObservableValue} from "../../../observable/value"; | ||
import {SyncStatus} from "../../../matrix/Sync"; | ||
import {Session} from "../../../matrix/Session"; | ||
|
||
type Options = { | ||
session: Session; | ||
} | ||
|
||
export class SyncProxy implements ISync { | ||
private _session: Session; | ||
private readonly _status: ObservableValue<SyncStatus> = new ObservableValue(SyncStatus.Stopped); | ||
private _error: Error | null = null; | ||
private _worker?: SharedWorker; | ||
|
||
constructor(options: Options) { | ||
const {session} = options; | ||
this._session = session; | ||
} | ||
|
||
get status(): ObservableValue<SyncStatus> { | ||
return this._status; | ||
} | ||
|
||
get error(): Error | null { | ||
return this._error; | ||
} | ||
|
||
async start(): Promise<void> { | ||
this._worker = new SharedWorker(new URL("./sync-worker", import.meta.url), { | ||
type: "module", | ||
}); | ||
this._worker.port.onmessage = (event: MessageEvent) => { | ||
// TODO | ||
console.log(event); | ||
}; | ||
} | ||
|
||
stop(): void { | ||
// TODO | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// TODO: Figure out how to get WebWorkers Typescript lib working. For now we just disable checks on the whole file. | ||
// @ts-nocheck | ||
|
||
// The empty export makes this a module. It can be removed once there's at least one import. | ||
export {} | ||
|
||
declare let self: SharedWorkerGlobalScope; | ||
|
||
self.onconnect = (event: MessageEvent) => { | ||
const port = event.ports[0]; | ||
port.postMessage("hello from sync worker"); | ||
console.log("hello from sync worker"); | ||
} |