From 74961dbfb13b61cbe4cb3369c4b5f88033463a8e Mon Sep 17 00:00:00 2001 From: Johannes Marbach Date: Mon, 13 Nov 2023 19:35:04 +0100 Subject: [PATCH] Extract worker creation into factories and mack them in tests --- __mocks__/workerFactoryMock.js | 19 +++++++++++++++++++ __mocks__/workerMock.js | 1 - jest.config.ts | 2 +- src/BlurhashEncoder.ts | 5 ++--- src/audio/Playback.ts | 5 ++--- src/utils/createMatrixClient.ts | 3 ++- src/workers/blurhashWorkerFactory.ts | 19 +++++++++++++++++++ src/workers/indexeddbWorkerFactory.ts | 19 +++++++++++++++++++ src/workers/playbackWorkerFactory.ts | 19 +++++++++++++++++++ tsconfig.json | 2 +- 10 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 __mocks__/workerFactoryMock.js delete mode 100644 __mocks__/workerMock.js create mode 100644 src/workers/blurhashWorkerFactory.ts create mode 100644 src/workers/indexeddbWorkerFactory.ts create mode 100644 src/workers/playbackWorkerFactory.ts diff --git a/__mocks__/workerFactoryMock.js b/__mocks__/workerFactoryMock.js new file mode 100644 index 0000000000..d86d2f0f5f --- /dev/null +++ b/__mocks__/workerFactoryMock.js @@ -0,0 +1,19 @@ +/* +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. +*/ + +export default function workerFactory(options) { + return jest.fn; +} diff --git a/__mocks__/workerMock.js b/__mocks__/workerMock.js deleted file mode 100644 index 6ee585673e..0000000000 --- a/__mocks__/workerMock.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = jest.fn(); diff --git a/jest.config.ts b/jest.config.ts index 58bec7684e..3e282958aa 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -31,7 +31,7 @@ const config: Config = { "decoderWorker\\.min\\.js": "/__mocks__/empty.js", "decoderWorker\\.min\\.wasm": "/__mocks__/empty.js", "waveWorker\\.min\\.js": "/__mocks__/empty.js", - "workers/(.+)\\.worker\\.ts": "/__mocks__/workerMock.js", + "workers/(.+)Factory": "/__mocks__/workerFactoryMock.js", "^!!raw-loader!.*": "jest-raw-loader", "RecorderWorklet": "/__mocks__/empty.js", }, diff --git a/src/BlurhashEncoder.ts b/src/BlurhashEncoder.ts index 30c1bc80d1..90cd9fa8e1 100644 --- a/src/BlurhashEncoder.ts +++ b/src/BlurhashEncoder.ts @@ -17,6 +17,7 @@ limitations under the License. // @ts-ignore - `.ts` is needed here to make TS happy import { Request, Response } from "./workers/blurhash.worker.ts"; import { WorkerManager } from "./WorkerManager"; +import blurhashWorkerFactory from "./workers/blurhashWorkerFactory.js"; export class BlurhashEncoder { private static internalInstance = new BlurhashEncoder(); @@ -25,9 +26,7 @@ export class BlurhashEncoder { return BlurhashEncoder.internalInstance; } - private readonly worker = new WorkerManager( - new Worker(new URL("./workers/blurhash.worker.ts", import.meta.url)), - ); + private readonly worker = new WorkerManager(blurhashWorkerFactory()); public getBlurhash(imageData: ImageData): Promise { return this.worker.call({ imageData }).then((resp) => resp.blurhash); diff --git a/src/audio/Playback.ts b/src/audio/Playback.ts index bd45703d2e..d9482f51af 100644 --- a/src/audio/Playback.ts +++ b/src/audio/Playback.ts @@ -29,6 +29,7 @@ import { createAudioContext, decodeOgg } from "./compat"; import { clamp } from "../utils/numbers"; import { WorkerManager } from "../WorkerManager"; import { DEFAULT_WAVEFORM, PLAYBACK_WAVEFORM_SAMPLES } from "./consts"; +import playbackWorkerFactory from "../workers/playbackWorkerFactory.js"; export enum PlaybackState { Decoding = "decoding", @@ -63,9 +64,7 @@ export class Playback extends EventEmitter implements IDestroyable, PlaybackInte private waveformObservable = new SimpleObservable(); private readonly clock: PlaybackClock; private readonly fileSize: number; - private readonly worker = new WorkerManager( - new Worker(new URL("../workers/playback.worker.ts", import.meta.url)), - ); + private readonly worker = new WorkerManager(playbackWorkerFactory()); /** * Creates a new playback instance from a buffer. diff --git a/src/utils/createMatrixClient.ts b/src/utils/createMatrixClient.ts index 77dc7ef25c..1569231bda 100644 --- a/src/utils/createMatrixClient.ts +++ b/src/utils/createMatrixClient.ts @@ -23,6 +23,7 @@ import { IndexedDBStore, LocalStorageCryptoStore, } from "matrix-js-sdk/src/matrix"; +import indexeddbWorkerFactory from "../workers/indexeddbWorkerFactory"; const localStorage = window.localStorage; @@ -52,7 +53,7 @@ export default function createMatrixClient(opts: ICreateClientOpts): MatrixClien indexedDB: indexedDB, dbName: "riot-web-sync", localStorage, - workerFactory: () => new Worker(new URL("../workers/indexeddb.worker.ts", import.meta.url)), + workerFactory: indexeddbWorkerFactory, }); } else if (localStorage) { storeOpts.store = new MemoryStore({ localStorage }); diff --git a/src/workers/blurhashWorkerFactory.ts b/src/workers/blurhashWorkerFactory.ts new file mode 100644 index 0000000000..ba66a27ef1 --- /dev/null +++ b/src/workers/blurhashWorkerFactory.ts @@ -0,0 +1,19 @@ +/* +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. +*/ + +export default function blurhashWorkerFactory(options?: WorkerOptions | undefined): Worker { + return new Worker(new URL("./workers/blurhash.worker.ts", import.meta.url), options); +} \ No newline at end of file diff --git a/src/workers/indexeddbWorkerFactory.ts b/src/workers/indexeddbWorkerFactory.ts new file mode 100644 index 0000000000..a2cb9c6591 --- /dev/null +++ b/src/workers/indexeddbWorkerFactory.ts @@ -0,0 +1,19 @@ +/* +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. +*/ + +export default function blurhashWorkerFactory(options?: WorkerOptions | undefined): Worker { + return new Worker(new URL("../workers/indexeddb.worker.ts", import.meta.url), options) +} \ No newline at end of file diff --git a/src/workers/playbackWorkerFactory.ts b/src/workers/playbackWorkerFactory.ts new file mode 100644 index 0000000000..6cca41841a --- /dev/null +++ b/src/workers/playbackWorkerFactory.ts @@ -0,0 +1,19 @@ +/* +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. +*/ + +export default function blurhashWorkerFactory(options?: WorkerOptions | undefined): Worker { + return new Worker(new URL("../workers/playback.worker.ts", import.meta.url), options) +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index e7689727ac..3d5f96e1c2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "emitDecoratorMetadata": false, "resolveJsonModule": true, "esModuleInterop": true, - "module": "commonjs", + "module": "es2022", "moduleResolution": "node", "target": "es2016", "noUnusedLocals": true,