Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 5855930

Browse files
committed
Extra worklet module loading and mock it in tests
1 parent 64ba0b4 commit 5855930

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const config: Config = {
3333
"waveWorker\\.min\\.js": "<rootDir>/__mocks__/empty.js",
3434
"workers/(.+)Factory": "<rootDir>/__mocks__/workerFactoryMock.js",
3535
"^!!raw-loader!.*": "jest-raw-loader",
36-
"RecorderWorklet": "<rootDir>/__mocks__/empty.js",
36+
"recorderWorkletFactory": "<rootDir>/__mocks__/empty.js",
3737
},
3838
transformIgnorePatterns: ["/node_modules/(?!matrix-js-sdk).+$"],
3939
collectCoverageFrom: [

src/audio/VoiceRecording.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ import { UPDATE_EVENT } from "../stores/AsyncStore";
2828
import { createAudioContext } from "./compat";
2929
import { FixedRollingArray } from "../utils/FixedRollingArray";
3030
import { clamp } from "../utils/numbers";
31-
// This import is needed for dead code analysis but not actually used because the
32-
// built-in worker / worklet handling in Webpack 5 only supports static paths
33-
// @ts-ignore no-unused-locals
34-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
35-
import mxRecorderWorkletPath from "./RecorderWorklet";
31+
import recorderWorkletFactory from "./recorderWorkletFactory";
3632

3733
const CHANNELS = 1; // stereo isn't important
3834
export const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality.
@@ -133,12 +129,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
133129
if (this.recorderContext.audioWorklet) {
134130
// Set up our worklet. We use this for timing information and waveform analysis: the
135131
// web audio API prefers this be done async to avoid holding the main thread with math.
136-
137-
// The audioWorklet.addModule syntax is required for Webpack 5 to correctly recognise
138-
// this as a worklet rather than an asset. This also requires the parser.javascript.worker
139-
// configuration described in https://github.com/webpack/webpack.js.org/issues/6869.
140-
const audioWorklet = this.recorderContext.audioWorklet;
141-
await audioWorklet.addModule(new URL("./RecorderWorklet.ts", import.meta.url));
132+
await recorderWorkletFactory(this.recorderContext);
142133

143134
this.recorderWorklet = new AudioWorkletNode(this.recorderContext, WORKLET_NAME);
144135
this.recorderSource.connect(this.recorderWorklet);

src/audio/recorderWorkletFactory.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// This import is needed for dead code analysis but not actually used because the
18+
// built-in worker / worklet handling in Webpack 5 only supports static paths
19+
// @ts-ignore no-unused-locals
20+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
21+
import mxRecorderWorkletPath from "./RecorderWorklet";
22+
23+
export default function recorderWorkletFactory(context: AudioContext): Promise<void> {
24+
// The context.audioWorklet.addModule syntax is required for Webpack 5 to correctly recognise
25+
// this as a worklet rather than an asset. This also requires the parser.javascript.worker
26+
// configuration described in https://github.com/webpack/webpack.js.org/issues/6869.
27+
return context.audioWorklet.addModule(new URL("./RecorderWorklet.ts", import.meta.url));
28+
}

0 commit comments

Comments
 (0)