Skip to content

Commit

Permalink
Resolve WebAudio engine isReadyPromise when done initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
docEdub committed Nov 21, 2024
1 parent 7d21bb3 commit c558cd4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/dev/core/src/Audio/v2/webAudio/webAudioEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export class _WebAudioEngine extends AudioEngineV2 {

private _resolveIsReadyPromise: () => void;

/** @internal */
public readonly isUnlockedPromise: Promise<void> = new Promise((resolve) => {
this._resolveIsUnlockedPromise = resolve;
});

private _resolveIsUnlockedPromise: () => void;

/** @internal */
public get currentTime(): number {
return this._audioContext?.currentTime ?? 0;
Expand All @@ -86,7 +93,7 @@ export class _WebAudioEngine extends AudioEngineV2 {
this._audioContext.addEventListener("statechange", this._onAudioContextStateChange);

if (this.state === "running") {
this._resolveIsReadyPromise?.();
this._resolveIsUnlockedPromise();
} else if (this._resumeOnInteraction) {
document.addEventListener("click", this._onUserInteraction, { once: true });
}
Expand All @@ -103,7 +110,7 @@ export class _WebAudioEngine extends AudioEngineV2 {
if (this.state === "running") {
this._audioContextStarted = true;
document.removeEventListener("click", this._onUserInteraction);
this._resolveIsReadyPromise?.();
this._resolveIsUnlockedPromise?.();
}
if (this.state === "suspended" || this.state === "interrupted") {
if (this._resumeOnInteraction) {
Expand Down Expand Up @@ -152,7 +159,7 @@ export class _WebAudioEngine extends AudioEngineV2 {
this._resumeOnInteraction = options?.resumeOnInteraction ?? true;

await this._initAudioContext();
await this.isReadyPromise;
this._resolveIsReadyPromise();
}

/** @internal */
Expand Down

0 comments on commit c558cd4

Please sign in to comment.