Extract worker creation into factories and mack them in tests
This commit is contained in:
parent
ccee4c9cdb
commit
74961dbfb1
10 changed files with 84 additions and 10 deletions
19
__mocks__/workerFactoryMock.js
Normal file
19
__mocks__/workerFactoryMock.js
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
module.exports = jest.fn();
|
|
|
@ -31,7 +31,7 @@ const config: Config = {
|
||||||
"decoderWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
|
"decoderWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
|
||||||
"decoderWorker\\.min\\.wasm": "<rootDir>/__mocks__/empty.js",
|
"decoderWorker\\.min\\.wasm": "<rootDir>/__mocks__/empty.js",
|
||||||
"waveWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
|
"waveWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
|
||||||
"workers/(.+)\\.worker\\.ts": "<rootDir>/__mocks__/workerMock.js",
|
"workers/(.+)Factory": "<rootDir>/__mocks__/workerFactoryMock.js",
|
||||||
"^!!raw-loader!.*": "jest-raw-loader",
|
"^!!raw-loader!.*": "jest-raw-loader",
|
||||||
"RecorderWorklet": "<rootDir>/__mocks__/empty.js",
|
"RecorderWorklet": "<rootDir>/__mocks__/empty.js",
|
||||||
},
|
},
|
||||||
|
|
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||||
// @ts-ignore - `.ts` is needed here to make TS happy
|
// @ts-ignore - `.ts` is needed here to make TS happy
|
||||||
import { Request, Response } from "./workers/blurhash.worker.ts";
|
import { Request, Response } from "./workers/blurhash.worker.ts";
|
||||||
import { WorkerManager } from "./WorkerManager";
|
import { WorkerManager } from "./WorkerManager";
|
||||||
|
import blurhashWorkerFactory from "./workers/blurhashWorkerFactory.js";
|
||||||
|
|
||||||
export class BlurhashEncoder {
|
export class BlurhashEncoder {
|
||||||
private static internalInstance = new BlurhashEncoder();
|
private static internalInstance = new BlurhashEncoder();
|
||||||
|
@ -25,9 +26,7 @@ export class BlurhashEncoder {
|
||||||
return BlurhashEncoder.internalInstance;
|
return BlurhashEncoder.internalInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly worker = new WorkerManager<Request, Response>(
|
private readonly worker = new WorkerManager<Request, Response>(blurhashWorkerFactory());
|
||||||
new Worker(new URL("./workers/blurhash.worker.ts", import.meta.url)),
|
|
||||||
);
|
|
||||||
|
|
||||||
public getBlurhash(imageData: ImageData): Promise<string> {
|
public getBlurhash(imageData: ImageData): Promise<string> {
|
||||||
return this.worker.call({ imageData }).then((resp) => resp.blurhash);
|
return this.worker.call({ imageData }).then((resp) => resp.blurhash);
|
||||||
|
|
|
@ -29,6 +29,7 @@ import { createAudioContext, decodeOgg } from "./compat";
|
||||||
import { clamp } from "../utils/numbers";
|
import { clamp } from "../utils/numbers";
|
||||||
import { WorkerManager } from "../WorkerManager";
|
import { WorkerManager } from "../WorkerManager";
|
||||||
import { DEFAULT_WAVEFORM, PLAYBACK_WAVEFORM_SAMPLES } from "./consts";
|
import { DEFAULT_WAVEFORM, PLAYBACK_WAVEFORM_SAMPLES } from "./consts";
|
||||||
|
import playbackWorkerFactory from "../workers/playbackWorkerFactory.js";
|
||||||
|
|
||||||
export enum PlaybackState {
|
export enum PlaybackState {
|
||||||
Decoding = "decoding",
|
Decoding = "decoding",
|
||||||
|
@ -63,9 +64,7 @@ export class Playback extends EventEmitter implements IDestroyable, PlaybackInte
|
||||||
private waveformObservable = new SimpleObservable<number[]>();
|
private waveformObservable = new SimpleObservable<number[]>();
|
||||||
private readonly clock: PlaybackClock;
|
private readonly clock: PlaybackClock;
|
||||||
private readonly fileSize: number;
|
private readonly fileSize: number;
|
||||||
private readonly worker = new WorkerManager<Request, Response>(
|
private readonly worker = new WorkerManager<Request, Response>(playbackWorkerFactory());
|
||||||
new Worker(new URL("../workers/playback.worker.ts", import.meta.url)),
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new playback instance from a buffer.
|
* Creates a new playback instance from a buffer.
|
||||||
|
|
|
@ -23,6 +23,7 @@ import {
|
||||||
IndexedDBStore,
|
IndexedDBStore,
|
||||||
LocalStorageCryptoStore,
|
LocalStorageCryptoStore,
|
||||||
} from "matrix-js-sdk/src/matrix";
|
} from "matrix-js-sdk/src/matrix";
|
||||||
|
import indexeddbWorkerFactory from "../workers/indexeddbWorkerFactory";
|
||||||
|
|
||||||
const localStorage = window.localStorage;
|
const localStorage = window.localStorage;
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ export default function createMatrixClient(opts: ICreateClientOpts): MatrixClien
|
||||||
indexedDB: indexedDB,
|
indexedDB: indexedDB,
|
||||||
dbName: "riot-web-sync",
|
dbName: "riot-web-sync",
|
||||||
localStorage,
|
localStorage,
|
||||||
workerFactory: () => new Worker(new URL("../workers/indexeddb.worker.ts", import.meta.url)),
|
workerFactory: indexeddbWorkerFactory,
|
||||||
});
|
});
|
||||||
} else if (localStorage) {
|
} else if (localStorage) {
|
||||||
storeOpts.store = new MemoryStore({ localStorage });
|
storeOpts.store = new MemoryStore({ localStorage });
|
||||||
|
|
19
src/workers/blurhashWorkerFactory.ts
Normal file
19
src/workers/blurhashWorkerFactory.ts
Normal file
|
@ -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);
|
||||||
|
}
|
19
src/workers/indexeddbWorkerFactory.ts
Normal file
19
src/workers/indexeddbWorkerFactory.ts
Normal file
|
@ -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)
|
||||||
|
}
|
19
src/workers/playbackWorkerFactory.ts
Normal file
19
src/workers/playbackWorkerFactory.ts
Normal file
|
@ -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)
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
"emitDecoratorMetadata": false,
|
"emitDecoratorMetadata": false,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"module": "commonjs",
|
"module": "es2022",
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"target": "es2016",
|
"target": "es2016",
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
|
|
Loading…
Reference in a new issue