Skip to content

Commit 9d86e2d

Browse files
add types and make public methods privte
1 parent 0d0840a commit 9d86e2d

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
"description": "Simple hack to allow web audio even if physical mute switch is on iPhone/iPads",
55
"main": "./src/index.js",
66
"exports": {
7-
".": "./src/index.js"
7+
".": {
8+
"default": "./src/index.js",
9+
"types": "./src/index.d.ts"
10+
}
811
},
12+
"types": "./src/index.d.ts",
913
"scripts": {
1014
"test": "vitest --dom run",
1115
"test:watch": "vitest --dom",

src/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default class SilentHack {
2+
constructor();
3+
tryUnblock(): void;
4+
destroy(): void;
5+
allowed: boolean;
6+
}

src/index.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
const EVENTS = ['auxclick', 'click', 'contextmenu', 'dblclick', 'keydown', 'keyup', 'mousedown', 'mouseup', 'touchend'];
22

33
export default class SilentHack {
4+
#trying = false;
5+
#state = 'blocked';
6+
#audioFile = '';
7+
48
constructor() {
5-
this.trying = false;
6-
this.state = 'blocked';
7-
this.audioFile = this.createAudioData();
9+
this.#audioFile = this.#createAudioData();
810

911
EVENTS.forEach((evtName) => {
10-
window.addEventListener(evtName, this.tryUnblock.bind(this), { capture: true, passive: true });
12+
window.addEventListener(evtName, this.#tryUnblock.bind(this), { capture: true, passive: true });
1113
});
1214
}
1315

14-
tryUnblock() {
15-
if (this.state === 'allowed' || this.trying) return;
16-
this.createHTMLAudio();
16+
#tryUnblock() {
17+
if (this.#state === 'allowed' || this.#trying) return;
18+
this.#createHTMLAudio();
1719
}
1820

19-
createAudioData() {
21+
#createAudioData() {
2022
const rate = 48000;
2123
const arrayBuffer = new ArrayBuffer(10);
2224
const dataView = new DataView(arrayBuffer);
@@ -30,38 +32,38 @@ export default class SilentHack {
3032
return `data:audio/wav;base64,UklGRisAAABXQVZFZm10IBAAAAABAAEA${missingCharacters}AgAZGF0YQcAAACAgICAgICAAAA=`;
3133
}
3234

33-
createHTMLAudio() {
34-
this.trying = true;
35+
#createHTMLAudio() {
36+
this.#trying = true;
3537

3638
let audio = document.createElement('audio');
3739

3840
audio.setAttribute('x-webkit-airplay', 'deny');
3941
audio.preload = 'auto';
4042
audio.loop = true;
41-
audio.src = this.audioFile;
43+
audio.src = this.#audioFile;
4244
audio.load();
4345

4446
audio.play().then(
4547
() => {
46-
this.state = 'allowed';
48+
this.#state = 'allowed';
4749
},
4850
() => {
49-
this.state = 'blocked';
51+
this.#state = 'blocked';
5052
audio.src = 'about:blank';
5153
audio.load();
5254
audio = null;
53-
this.trying = false;
55+
this.#trying = false;
5456
}
5557
);
5658
}
5759

5860
destroy() {
5961
EVENTS.forEach((evtName) => {
60-
window.removeEventListener(evtName, this.tryUnblock.bind(this), { capture: true, passive: true });
62+
window.removeEventListener(evtName, this.#tryUnblock.bind(this), { capture: true, passive: true });
6163
});
6264
}
6365

6466
get allowed() {
65-
return this.state === 'allowed';
67+
return this.#state === 'allowed';
6668
}
6769
}

src/index.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@ describe('SilentHack', () => {
1616
expect(silentHack.allowed).toBe(false);
1717
});
1818

19-
it('should have a trying property of false', () => {
20-
expect(silentHack.trying).toBe(false);
21-
});
22-
2319
it('should call tryUnblock on event', async () => {
20+
expect(silentHack.allowed).toBe(false);
21+
2422
const event = new Event('click');
2523
window.dispatchEvent(event);
26-
expect(silentHack.trying).toBe(true);
2724

2825
await new Promise((resolve) => setTimeout(resolve, 100));
2926

0 commit comments

Comments
 (0)