Skip to content

Commit e60ac0c

Browse files
committed
Use async/await in libwebaudio.js. NFC
1 parent 17516af commit e60ac0c

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

src/lib/libwebaudio.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,22 @@ var LibraryWebAudio = {
119119
#endif
120120
},
121121

122-
emscripten_resume_audio_context_async: (contextHandle, callback, userData) => {
122+
emscripten_resume_audio_context_async: async (contextHandle, callback, userData) => {
123123
function cb(state) {
124124
#if WEBAUDIO_DEBUG
125-
console.log(`emscripten_resume_audio_context_async() callback: New audio state="${emAudio[contextHandle].state}", ID=${state}`);
125+
dbg(`emscripten_resume_audio_context_async() callback: New audio state="${emAudio[contextHandle].state}", ID=${state}`);
126126
#endif
127127
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, state, userData);
128128
}
129129
#if WEBAUDIO_DEBUG
130130
dbg('emscripten_resume_audio_context_async() resuming...');
131131
#endif
132-
emAudio[contextHandle].resume().then(() => { cb(1/*running*/) }).catch(() => { cb(0/*suspended*/) });
132+
try {
133+
await emAudio[contextHandle].resume();
134+
cb(/*running*/);
135+
} catch {
136+
cb(0/*suspended*/);
137+
}
133138
},
134139

135140
emscripten_resume_audio_context_sync: (contextHandle) => {
@@ -171,8 +176,7 @@ var LibraryWebAudio = {
171176
'$_wasmWorkersID',
172177
'$_emAudioDispatchProcessorCallback',
173178
'$stackAlloc', '$stackRestore', '$stackSave'],
174-
emscripten_start_wasm_audio_worklet_thread_async: (contextHandle, stackLowestAddress, stackSize, callback, userData) => {
175-
179+
emscripten_start_wasm_audio_worklet_thread_async: async (contextHandle, stackLowestAddress, stackSize, callback, userData) => {
176180
#if ASSERTIONS || WEBAUDIO_DEBUG
177181
emAudioExpectContext(contextHandle, 'emscripten_start_wasm_audio_worklet_thread_async');
178182
#endif
@@ -212,55 +216,59 @@ var LibraryWebAudio = {
212216
return audioWorkletCreationFailed();
213217
}
214218

215-
audioWorklet.addModule({{{ wasmWorkerJs }}}).then(() => {
219+
try {
220+
await audioWorklet.addModule({{{ wasmWorkerJs }}});
221+
} catch {
222+
return audioWorkletCreationFailed();
223+
}
224+
216225
#if WEBAUDIO_DEBUG
217-
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
226+
dbg(`emscripten_start_wasm_audio_worklet_thread_async() addModule() completed`);
218227
#endif
219228

220229
#if MIN_FIREFOX_VERSION < 138 || MIN_CHROME_VERSION != TARGET_NOT_SUPPORTED || MIN_SAFARI_VERSION != TARGET_NOT_SUPPORTED
221-
// If this browser does not support the up-to-date AudioWorklet standard
222-
// that has a MessagePort over to the AudioWorklet, then polyfill that by
223-
// instantiating a dummy AudioWorkletNode to get a MessagePort over.
224-
// Firefox added support in https://hg-edge.mozilla.org/integration/autoland/rev/ab38a1796126f2b3fc06475ffc5a625059af59c1
225-
// Chrome ticket: https://crbug.com/446920095
226-
// Safari ticket: https://webkit.org/b/299386
227-
if (!audioWorklet['port']) {
228-
audioWorklet['port'] = {
229-
postMessage: (msg) => {
230-
if (msg['_boot']) {
231-
audioWorklet.bootstrapMessage = new AudioWorkletNode(audioContext, 'em-bootstrap', {
232-
processorOptions: msg
233-
});
234-
audioWorklet.bootstrapMessage['port'].onmessage = (msg) => {
235-
audioWorklet['port'].onmessage(msg);
236-
}
237-
} else {
238-
audioWorklet.bootstrapMessage['port'].postMessage(msg);
230+
// If this browser does not support the up-to-date AudioWorklet standard
231+
// that has a MessagePort over to the AudioWorklet, then polyfill that by
232+
// instantiating a dummy AudioWorkletNode to get a MessagePort over.
233+
// Firefox added support in https://hg-edge.mozilla.org/integration/autoland/rev/ab38a1796126f2b3fc06475ffc5a625059af59c1
234+
// Chrome ticket: https://crbug.com/446920095
235+
// Safari ticket: https://webkit.org/b/299386
236+
if (!audioWorklet['port']) {
237+
audioWorklet['port'] = {
238+
postMessage: (msg) => {
239+
if (msg['_boot']) {
240+
audioWorklet.bootstrapMessage = new AudioWorkletNode(audioContext, 'em-bootstrap', {
241+
processorOptions: msg
242+
});
243+
audioWorklet.bootstrapMessage['port'].onmessage = (msg) => {
244+
audioWorklet['port'].onmessage(msg);
239245
}
246+
} else {
247+
audioWorklet.bootstrapMessage['port'].postMessage(msg);
240248
}
241249
}
242250
}
251+
}
243252
#endif
244253

245-
audioWorklet['port'].postMessage({
246-
// This is the bootstrap message to the Audio Worklet.
247-
'_boot': 1,
248-
// Assign the loaded AudioWorkletGlobalScope a Wasm Worker ID so that
249-
// it can utilized its own TLS slots, and it is recognized to not be
250-
// the main browser thread.
251-
wwID: _wasmWorkersID++,
254+
audioWorklet['port'].postMessage({
255+
// This is the bootstrap message to the Audio Worklet.
256+
'_boot': 1,
257+
// Assign the loaded AudioWorkletGlobalScope a Wasm Worker ID so that
258+
// it can utilized its own TLS slots, and it is recognized to not be
259+
// the main browser thread.
260+
wwID: _wasmWorkersID++,
252261
#if MINIMAL_RUNTIME
253-
wasm: Module['wasm'],
262+
wasm: Module['wasm'],
254263
#else
255-
wasm: wasmModule,
264+
wasm: wasmModule,
256265
#endif
257-
wasmMemory,
258-
stackLowestAddress, // sb = stack base
259-
stackSize, // sz = stack size
260-
});
261-
audioWorklet['port'].onmessage = _emAudioDispatchProcessorCallback;
262-
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, 1/*EM_TRUE*/, userData);
263-
}).catch(audioWorkletCreationFailed);
266+
wasmMemory,
267+
stackLowestAddress, // sb = stack base
268+
stackSize, // sz = stack size
269+
});
270+
audioWorklet['port'].onmessage = _emAudioDispatchProcessorCallback;
271+
{{{ makeDynCall('viip', 'callback') }}}(contextHandle, 1/*EM_TRUE*/, userData);
264272
},
265273

266274
$_emAudioDispatchProcessorCallback__deps: ['$getWasmTableEntry'],

0 commit comments

Comments
 (0)