Skip to content

Commit

Permalink
🧪 [Earwurm] Complete test suite (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi authored Mar 5, 2023
1 parent 7dd1d83 commit 2e81cd3
Show file tree
Hide file tree
Showing 16 changed files with 795 additions and 177 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-apricots-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'earwurm': minor
---

Earwurm now empties all events on teardown.
5 changes: 5 additions & 0 deletions .changeset/shiny-snakes-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'earwurm': minor
---

Earwurm now triggers autoSuspend conditionally on init and whenever state changes to "running".
5 changes: 5 additions & 0 deletions .changeset/spicy-plums-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'earwurm': minor
---

Revise some method types for TypeScript strict mode.
1 change: 0 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"no-console": "warn",
"prettier/prettier": ["error"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/method-signature-style": "off",
"@typescript-eslint/strict-boolean-expressions": "off"
}
}
234 changes: 122 additions & 112 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@
"lint": "eslint . --ext .ts,.tsx",
"test": "vitest",
"test:ui": "vitest --ui",
"coverage": "vitest --coverage",
"coverage": "vitest --run --coverage",
"report": "changeset",
"release": "npm run build && changeset publish"
},
"dependencies": {
"emitten": "^0.4.2"
"emitten": "^0.4.3"
},
"devDependencies": {
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.0",
"@types/node": "^18.14.1",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@vitest/coverage-c8": "^0.29.1",
"@vitest/ui": "^0.29.1",
"eslint": "^8.34.0",
"@types/node": "^18.14.6",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@vitest/coverage-c8": "^0.29.2",
"@vitest/ui": "^0.29.2",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"eslint-config-standard-with-typescript": "^34.0.0",
"eslint-plugin-import": "^2.27.5",
Expand All @@ -72,9 +72,9 @@
"typescript": "^4.9.5",
"vite": "^4.1.4",
"vite-plugin-dts": "^2.0.2",
"vitest": "^0.29.1"
"vitest": "^0.29.2"
},
"peerDependencies": {
"emitten": "^0.4.2"
"emitten": "^0.4.3"
}
}
30 changes: 19 additions & 11 deletions src/Earwurm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
ManagerConfig,
LibraryEntry,
LibraryKeys,
StackEventMap,
} from './types';

import {Stack} from './Stack';
Expand Down Expand Up @@ -46,7 +47,8 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {

this.#gainNode.connect(this.#context.destination);
this.#gainNode.gain.setValueAtTime(this._volume, this.#context.currentTime);
this.#autoSuspend();

if (this._unlocked) this.#autoSuspend();

this.#context.addEventListener('statechange', this.#handleStateChange);
}
Expand Down Expand Up @@ -178,13 +180,6 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
this.#library.forEach((stack) => stack.teardown());
this.#setLibrary([]);

this.#queuedResume = false;

if (this.#suspendId) {
clearTimeout(this.#suspendId);
this.#suspendId = 0;
}

this.#context
.close()
.then(() => {
Expand All @@ -200,6 +195,8 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
]);
});

this.empty();

return this;
}

Expand Down Expand Up @@ -230,10 +227,14 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
getErrorMessage(error),
]);
});

this.#queuedResume = false;
}

this.#clearSuspendResume();
}

#clearSuspendResume() {
this.#queuedResume = false;

if (this.#suspendId) {
clearTimeout(this.#suspendId);
this.#suspendId = 0;
Expand All @@ -253,8 +254,10 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {

if (value === 'running') {
this._unlocked = true;
this.#autoSuspend();
} else if (value === 'closed') {
this._unlocked = false;
this.#clearSuspendResume();
}
}

Expand Down Expand Up @@ -287,7 +290,12 @@ export class Earwurm extends EmittenCommon<ManagerEventMap> {
this.#setState(this.#context.state);
};

#handleStackStateChange = () => {
#handleStackStateChange: StackEventMap['statechange'] = (state) => {
// 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 (this.playing) {
this.#autoResume();
} else {
Expand Down
Loading

0 comments on commit 2e81cd3

Please sign in to comment.