Skip to content

Commit

Permalink
✨ [Earwurm] Move pre-defined error messages to static member
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi committed Dec 22, 2023
1 parent 0be9185 commit 9c652e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 10 additions & 5 deletions src/Earwurm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
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 = [];
Expand Down Expand Up @@ -146,7 +151,7 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
request: this.#request,
});

newStack.on('statechange', this.#handleStackStateChange);
newStack.on('state', this.#handleStackState);

return [...collection, newStack];
}, []);
Expand Down Expand Up @@ -203,7 +208,7 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
})
.catch((error) => {
this.emit('error', [
'Failed to close the Earwurm AudioContext.',
Earwurm.errorMessage.close,
getErrorMessage(error),
]);
});
Expand Down Expand Up @@ -236,7 +241,7 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
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),
]);
});
Expand Down Expand Up @@ -311,11 +316,11 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
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();
Expand Down
8 changes: 4 additions & 4 deletions src/tests/Earwurm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(() => {
Expand All @@ -531,7 +531,7 @@ describe('Earwurm component', () => {

/*
expect(spyError).toBeCalledWith([
'Failed to close the Earwurm AudioContext.',
Earwurm.errorMessage.close,
mockErrorMessage,
]);
*/
Expand Down

0 comments on commit 9c652e4

Please sign in to comment.