diff --git a/src/Earwurm.ts b/src/Earwurm.ts index c1b5481..8da54f8 100644 --- a/src/Earwurm.ts +++ b/src/Earwurm.ts @@ -20,6 +20,11 @@ export class Earwurm extends EmittenCommon { static readonly maxStackSize = tokens.maxStackSize; static readonly suspendAfterMs = tokens.suspendAfterMs; + static readonly errorMessage = { + close: 'Failed to close the Earwurm AudioContext.', + resume: 'Failed to resume the Earwurm AudioContext.', + }; + private _volume = 1; private _mute = false; private _keys: LibraryKeys = []; @@ -146,7 +151,7 @@ export class Earwurm extends EmittenCommon { request: this.#request, }); - newStack.on('statechange', this.#handleStackStateChange); + newStack.on('state', this.#handleStackState); return [...collection, newStack]; }, []); @@ -203,7 +208,7 @@ export class Earwurm extends EmittenCommon { }) .catch((error) => { this.emit('error', [ - 'Failed to close the Earwurm AudioContext.', + Earwurm.errorMessage.close, getErrorMessage(error), ]); }); @@ -236,7 +241,7 @@ export class Earwurm extends EmittenCommon { if (this._state === 'suspended' || this._state === 'interrupted') { this.#context.resume().catch((error) => { this.emit('error', [ - 'Failed to resume the Earwurm AudioContext.', + Earwurm.errorMessage.resume, getErrorMessage(error), ]); }); @@ -311,11 +316,11 @@ export class Earwurm extends EmittenCommon { this.#setState(this.#context.state); }; - readonly #handleStackStateChange: StackEventMap['statechange'] = (state) => { + readonly #handleStackState: StackEventMap['state'] = (current) => { // We don't care about re-setting the auto-suspension each time // a new `Sound` is prepared... but it will do that anyways // since `Stack` returns to `idle` once loaded. - if (state === 'loading') return; + if (current === 'loading') return; if (this.playing) { this.#autoResume(); diff --git a/src/tests/Earwurm.test.ts b/src/tests/Earwurm.test.ts index bcec7a6..8e79244 100644 --- a/src/tests/Earwurm.test.ts +++ b/src/tests/Earwurm.test.ts @@ -365,14 +365,14 @@ describe('Earwurm component', () => { mockManager.add(...mockEntries); const stack1 = mockManager.get(mockEntries[1].id); - stack1?.on('statechange', spyStack1State); + stack1?.on('state', spyStack1State); await stack1?.prepare().then((sound) => sound.play()); expect(spyStack1State).toBeCalledTimes(3); expect(stack1?.state).toBe('playing'); const stack2 = mockManager.get(mockEntries[2].id); - stack2?.on('statechange', spyStack2State); + stack2?.on('state', spyStack2State); await stack2?.prepare().then((sound) => sound.play()); expect(spyStack2State).toBeCalledTimes(3); @@ -520,7 +520,7 @@ describe('Earwurm component', () => { it('throws error if AudioContext cannot be closed', async () => { const mockErrorMessage = 'Mock error message'; - const spyError: ManagerEventMap['error'] = vi.fn((_message) => {}); + const spyError: ManagerEventMap['error'] = vi.fn((_messages) => {}); mockManager.on('error', spyError); vi.spyOn(AudioContext.prototype, 'close').mockImplementationOnce(() => { @@ -531,7 +531,7 @@ describe('Earwurm component', () => { /* expect(spyError).toBeCalledWith([ - 'Failed to close the Earwurm AudioContext.', + Earwurm.errorMessage.close, mockErrorMessage, ]); */