Skip to content

Commit 3f0ea0e

Browse files
committed
[AUDIO_WORKLET] Wait for Wasm instance to start using audio worklet.
Fixes intermittent test failures where the AudioWorklet was used before the Wasm module was loaded in MINIMAL_RUNTIME.
1 parent eee429a commit 3f0ea0e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/audio_worklet.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// the node constructor's "processorOptions" field, we can share the necessary
1313
// bootstrap information from the main thread to the AudioWorkletGlobalScope.
1414

15+
var instantiatePromise;
1516
if (ENVIRONMENT_IS_AUDIO_WORKLET) {
1617

1718
function createWasmAudioWorkletProcessor(audioParams) {
@@ -161,6 +162,10 @@ class BootstrapMessages extends AudioWorkletProcessor {
161162
messagePort = this.port;
162163
/** @suppress {checkTypes} */
163164
messagePort.onmessage = async (msg) => {
165+
#if MINIMAL_RUNTIME
166+
// Wait for the module instantiation before processing messages.
167+
await instantiatePromise;
168+
#endif
164169
let d = msg.data;
165170
if (d['_wpn']) {
166171
// '_wpn' is short for 'Worklet Processor Node', using an identifier

src/postamble_minimal.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ var imports = {
128128
// precompiled WebAssembly Module.
129129
assert(WebAssembly.instantiateStreaming || Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm before adding compiled output .js script to the DOM');
130130
#endif
131+
#if AUDIO_WORKLET
132+
instantiatePromise =
133+
#endif
131134
(WebAssembly.instantiateStreaming
132135
#if ENVIRONMENT_MAY_BE_NODE
133136
// Node's fetch API cannot be used for local files, so we cannot use instantiateStreaming
@@ -136,6 +139,9 @@ assert(WebAssembly.instantiateStreaming || Module['wasm'], 'Must load WebAssembl
136139
? WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports)
137140
: WebAssembly.instantiate(Module['wasm'], imports)).then((output) => {
138141
#else
142+
#if AUDIO_WORKLET
143+
instantiatePromise =
144+
#endif
139145
WebAssembly.instantiateStreaming(fetch('{{{ TARGET_BASENAME }}}.wasm'), imports).then((output) => {
140146
#endif
141147

@@ -152,6 +158,9 @@ assert(Module['wasm'], 'Must load WebAssembly Module in to variable Module.wasm
152158

153159
// Add missingProperties supression here because closure compiler doesn't know that
154160
// WebAssembly.instantiate is polymorphic in its return value.
161+
#if AUDIO_WORKLET
162+
instantiatePromise =
163+
#endif
155164
WebAssembly.instantiate(Module['wasm'], imports).then(/** @suppress {missingProperties} */ (output) => {
156165
#endif
157166

0 commit comments

Comments
 (0)