Skip to content

Commit dde7aa4

Browse files
committed
Add 'waitForReady' API
1 parent ede99a4 commit dde7aa4

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/main/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import * as Constants from './Constants';
33
import ISequencer from './ISequencer';
44
import ISynthesizer from './ISynthesizer';
55
import Synthesizer from './Synthesizer';
6+
import waitForReady from './waitForReady';
67
import AudioWorkletNodeSynthesizer from './AudioWorkletNodeSynthesizer';
78

89
export {
910
Constants,
1011
ISequencer,
1112
ISynthesizer,
1213
Synthesizer,
14+
waitForReady,
1315
AudioWorkletNodeSynthesizer
1416
};

src/main/registerAudioWorkletProcessor.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import Synthesizer from './Synthesizer';
3+
import waitForReady from './waitForReady';
34

45
import {
56
Constants,
@@ -12,20 +13,7 @@ import {
1213
ReturnMessageInstance
1314
} from './MethodMessaging';
1415

15-
const _module: any = AudioWorkletGlobalScope.wasmModule;
16-
const promiseWasmInitialized = new Promise<void>((resolve) => {
17-
if (_module.calledRun) {
18-
resolve();
19-
} else {
20-
const fn: (() => void) | undefined = _module.onRuntimeInitialized;
21-
_module.onRuntimeInitialized = () => {
22-
resolve();
23-
if (fn) {
24-
fn();
25-
}
26-
};
27-
}
28-
});
16+
const promiseWasmInitialized = waitForReady();
2917

3018
/** Registers processor using Synthesizer for AudioWorklet. */
3119
export default function registerAudioWorkletProcessor() {

src/main/waitForReady.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/** @internal */
3+
declare global {
4+
var Module: any;
5+
}
6+
7+
const _module: any = typeof AudioWorkletGlobalScope !== 'undefined' ?
8+
AudioWorkletGlobalScope.wasmModule : Module;
9+
const promiseWasmInitialized = new Promise<void>((resolve) => {
10+
if (_module.calledRun) {
11+
resolve();
12+
} else {
13+
const fn: (() => void) | undefined = _module.onRuntimeInitialized;
14+
_module.onRuntimeInitialized = () => {
15+
resolve();
16+
if (fn) {
17+
fn();
18+
}
19+
};
20+
}
21+
});
22+
23+
/**
24+
* Returns the Promise object which resolves when the synthesizer engine is ready.
25+
*/
26+
export default function waitForReady(): Promise<void> {
27+
return promiseWasmInitialized;
28+
}

0 commit comments

Comments
 (0)