clicktone 1.5.4 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
1
  node_modules
2
- .parcel
3
2
  dist
3
+ .parcel
4
+ .parcel-cache
@@ -0,0 +1,11 @@
1
+ {
2
+ "semi": true,
3
+ "singleQuote": true,
4
+ "printWidth": 100,
5
+ "trailingComma": "all",
6
+ "arrowParens": "always",
7
+ "tabWidth": 2,
8
+ "useTabs": false,
9
+ "bracketSpacing": true,
10
+ "endOfLine": "lf"
11
+ }
package/README.md CHANGED
@@ -34,7 +34,8 @@ import ClickTone from 'clicktone';
34
34
  <sub>ClickTone uses the Web Audio API, which supports many audio file formats: MP3, WAV, OGG, AAC and others. Note that not all browsers support these formats.</sub>
35
35
  ```javascript
36
36
  const sound = new ClickTone({
37
- file: './sound.mp3',
37
+ // file: './sound.mp3',
38
+ file: new URL('./sound.mp3', import.meta.url).href,
38
39
  volume: 0.7,
39
40
  throttle: 100,
40
41
  callback: () => { console.log('Playback ended') },
@@ -11,8 +11,8 @@ export default class ClickTone {
11
11
  throttle?: number;
12
12
  debug?: boolean;
13
13
  });
14
- play: (url?: string) => Promise<void>;
14
+ play(url?: string): void;
15
15
  }
16
16
  export default ClickTone;
17
17
 
18
- //# sourceMappingURL=index.d.ts.map
18
+ //# sourceMappingURL=clicktone.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":"AAAA,QAAQ,MAAM,CAAC;IACb,UAAU,MAAM;QACd,kBAAkB,CAAC,EAAE,OAAO,YAAY,CAAC;KAC1C;CACF;AAED,qBAAM,SAAS;gBAUD,EACV,IAAI,EACJ,MAAY,EACZ,QAAe,EACf,QAAY,EACZ,KAAa,GACd,EAAE;QACD,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;QAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;IA6GM,IAAI,CAAC,GAAG,GAAE,MAAkB,GAAG,IAAI;CAiB3C;AAED,eAAe,SAAS,CAAC","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext(): void {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(url: string): Promise<AudioBuffer> {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(url: string): Promise<void> {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = (): void => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n throttledPlay();\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"clicktone.d.ts.map"}
package/dist/index.js CHANGED
@@ -11,14 +11,6 @@ $parcel$defineInteropFlag(module.exports);
11
11
 
12
12
  $parcel$export(module.exports, "default", function () { return $a196c1ed25598f0e$export$2e2bcd8739ae039; });
13
13
  class $a196c1ed25598f0e$var$ClickTone {
14
- file;
15
- volume;
16
- callback;
17
- throttle;
18
- debug;
19
- lastClickTime;
20
- audioCache;
21
- audioContext;
22
14
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
23
15
  this.file = file;
24
16
  this.volume = volume;
@@ -35,19 +27,21 @@ class $a196c1ed25598f0e$var$ClickTone {
35
27
  this.iOSFixAudioContext();
36
28
  }
37
29
  }
38
- iOSFixAudioContext = ()=>{
39
- if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
30
+ iOSFixAudioContext() {
31
+ if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
40
32
  const unlock = ()=>{
41
- if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
42
- document.body.removeEventListener("touchstart", unlock);
43
- document.body.removeEventListener("touchend", unlock);
33
+ if (this.audioContext.state === 'suspended') this.audioContext.resume().then(()=>{
34
+ document.body.removeEventListener('touchstart', unlock);
35
+ document.body.removeEventListener('touchend', unlock);
36
+ }).catch((error)=>{
37
+ if (this.debug) console.error('AudioContext resume error:', error);
44
38
  });
45
39
  };
46
- document.body.addEventListener("touchstart", unlock, false);
47
- document.body.addEventListener("touchend", unlock, false);
40
+ document.body.addEventListener('touchstart', unlock, false);
41
+ document.body.addEventListener('touchend', unlock, false);
48
42
  }
49
- };
50
- fetchAndDecodeAudio = async (url)=>{
43
+ }
44
+ async fetchAndDecodeAudio(url) {
51
45
  try {
52
46
  if (this.audioCache[url]) return this.audioCache[url];
53
47
  const response = await fetch(url);
@@ -56,12 +50,11 @@ class $a196c1ed25598f0e$var$ClickTone {
56
50
  this.audioCache[url] = audioData;
57
51
  return audioData;
58
52
  } catch (error) {
59
- if (this.debug) // eslint-disable-next-line no-console
60
- console.error("Audio loading and decoding error: ", error);
53
+ if (this.debug) console.error('Audio loading and decoding error: ', error);
61
54
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
62
55
  }
63
- };
64
- playAudio = async (url)=>{
56
+ }
57
+ async playAudio(url) {
65
58
  this.initAudioContext();
66
59
  try {
67
60
  const audioData = await this.fetchAndDecodeAudio(url);
@@ -76,29 +69,31 @@ class $a196c1ed25598f0e$var$ClickTone {
76
69
  };
77
70
  source.start(0);
78
71
  } catch (error) {
79
- if (this.debug) // eslint-disable-next-line no-console
80
- console.error("Audio playback error: ", error);
72
+ if (this.debug) console.error('Audio playback error: ', error);
81
73
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
82
74
  }
83
- };
84
- throttleFn = (func)=>()=>{
75
+ }
76
+ throttleFn(func) {
77
+ return ()=>{
85
78
  const now = Date.now();
86
79
  if (now - this.lastClickTime >= this.throttle) {
87
- func();
80
+ func().catch((error)=>{
81
+ if (this.debug) console.error('Error in throttled function:', error);
82
+ });
88
83
  this.lastClickTime = now;
89
84
  }
90
85
  };
91
- play = async (url = this.file)=>{
86
+ }
87
+ play(url = this.file) {
92
88
  const throttledPlay = this.throttleFn(()=>this.playAudio(url));
93
89
  try {
94
- await throttledPlay();
90
+ throttledPlay();
95
91
  } catch (error) {
96
- if (this.debug) // eslint-disable-next-line no-console
97
- console.error("Audio playback error: ", error);
92
+ if (this.debug) console.error('Audio playback error: ', error);
98
93
  if (this.callback) this.callback(error);
99
94
  else throw error;
100
95
  }
101
- };
96
+ }
102
97
  }
103
98
  var $a196c1ed25598f0e$export$2e2bcd8739ae039 = $a196c1ed25598f0e$var$ClickTone;
104
99
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAMA,MAAM;IACa,KAAa;IAEb,OAAe;IAEf,SAA2C;IAE3C,SAAiB;IAEjB,MAAe;IAExB,cAAsB;IAEb,WAAwC;IAEjD,aAAkC;IAE1C,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,CAAE;QACD,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,MAAM,GAAG;QACd,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,KAAK,GAAG;QACb,IAAI,CAAC,aAAa,GAAG;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG;IAC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mB,CAAC;YACjD,MAAM,SAAS,IAAI,CAAC,YAAY,CAAE,kBAAkB;YACpD,MAAM,WAAW,IAAI,CAAC,YAAY,CAAE,UAAU;YAE9C,OAAO,MAAM,GAAG;YAChB,SAAS,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM;YACjC,OAAO,OAAO,CAAC;YACf,SAAS,OAAO,CAAC,IAAI,CAAC,YAAY,CAAE,WAAW;YAE/C,OAAO,OAAO,GAAG;gBACf,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ;YAEjB;YAEA,OAAO,KAAK,CAAC;QACf,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,sCAAsC;YACtC,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,AAAC,MAAgB,OAAO,CAAC,CAAC;QACzF;IACF,EAAE;IAEM,aAAa,CAAC,OAAmC;YACvD,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF,EAAE;IAEK,OAAO,OAAO,MAAc,IAAI,CAAC,IAAI;QAC1C,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,sCAAsC;YACtC,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;;;AAMA,MAAM;IAUJ,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,CAAE;QACD,IAAI,CAAC,IAAI,GAAG;QACZ,IAAI,CAAC,MAAM,GAAG;QACd,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,QAAQ,GAAG;QAChB,IAAI,CAAC,KAAK,GAAG;QACb,IAAI,CAAC,aAAa,GAAG;QACrB,IAAI,CAAC,UAAU,GAAG,CAAC;QACnB,IAAI,CAAC,YAAY,GAAG;IACtB;IAEQ,mBAAyB;QAC/B,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE;YACtB,IAAI,CAAC,YAAY,GAAG,IAAK,CAAA,OAAO,YAAY,IAAI,OAAO,kBAAkB,AAAD;YACxE,IAAI,CAAC,kBAAkB;QACzB;IACF;IAEQ,qBAA2B;QACjC,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YAC5F,MAAM,SAAS;gBACb,IAAI,IAAI,C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AAC,YAAY,CAAE,WAAW;YAE/C,OAAO,OAAO,GAAG;gBACf,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ;YAEjB;YAEA,OAAO,KAAK,CAAC;QACf,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,MAAM,IAAI,MAAM,CAAC,0CAA0C,EAAE,AAAC,MAAgB,OAAO,EAAE;QACzF;IACF;IAEQ,WAAW,IAAyB,EAAc;QACxD,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBACxC,OAAO,KAAK,CAAC,CAAC;oBACjB,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,gCAAgC;gBAElD;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF;IAEO,KAAK,MAAc,IAAI,CAAC,IAAI,EAAQ;QACzC,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF;QACF,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF;AACF;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext(): void {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(url: string): Promise<AudioBuffer> {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(url: string): Promise<void> {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = (): void => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n throttledPlay();\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,12 +1,4 @@
1
1
  class $643fcf18b2d2e76f$var$ClickTone {
2
- file;
3
- volume;
4
- callback;
5
- throttle;
6
- debug;
7
- lastClickTime;
8
- audioCache;
9
- audioContext;
10
2
  constructor({ file: file, volume: volume = 1.0, callback: callback = null, throttle: throttle = 0, debug: debug = false }){
11
3
  this.file = file;
12
4
  this.volume = volume;
@@ -23,19 +15,21 @@ class $643fcf18b2d2e76f$var$ClickTone {
23
15
  this.iOSFixAudioContext();
24
16
  }
25
17
  }
26
- iOSFixAudioContext = ()=>{
27
- if (this.audioContext && this.audioContext.state === "suspended" && "ontouchstart" in window) {
18
+ iOSFixAudioContext() {
19
+ if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
28
20
  const unlock = ()=>{
29
- if (this.audioContext.state === "suspended") this.audioContext.resume().then(()=>{
30
- document.body.removeEventListener("touchstart", unlock);
31
- document.body.removeEventListener("touchend", unlock);
21
+ if (this.audioContext.state === 'suspended') this.audioContext.resume().then(()=>{
22
+ document.body.removeEventListener('touchstart', unlock);
23
+ document.body.removeEventListener('touchend', unlock);
24
+ }).catch((error)=>{
25
+ if (this.debug) console.error('AudioContext resume error:', error);
32
26
  });
33
27
  };
34
- document.body.addEventListener("touchstart", unlock, false);
35
- document.body.addEventListener("touchend", unlock, false);
28
+ document.body.addEventListener('touchstart', unlock, false);
29
+ document.body.addEventListener('touchend', unlock, false);
36
30
  }
37
- };
38
- fetchAndDecodeAudio = async (url)=>{
31
+ }
32
+ async fetchAndDecodeAudio(url) {
39
33
  try {
40
34
  if (this.audioCache[url]) return this.audioCache[url];
41
35
  const response = await fetch(url);
@@ -44,12 +38,11 @@ class $643fcf18b2d2e76f$var$ClickTone {
44
38
  this.audioCache[url] = audioData;
45
39
  return audioData;
46
40
  } catch (error) {
47
- if (this.debug) // eslint-disable-next-line no-console
48
- console.error("Audio loading and decoding error: ", error);
41
+ if (this.debug) console.error('Audio loading and decoding error: ', error);
49
42
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
50
43
  }
51
- };
52
- playAudio = async (url)=>{
44
+ }
45
+ async playAudio(url) {
53
46
  this.initAudioContext();
54
47
  try {
55
48
  const audioData = await this.fetchAndDecodeAudio(url);
@@ -64,29 +57,31 @@ class $643fcf18b2d2e76f$var$ClickTone {
64
57
  };
65
58
  source.start(0);
66
59
  } catch (error) {
67
- if (this.debug) // eslint-disable-next-line no-console
68
- console.error("Audio playback error: ", error);
60
+ if (this.debug) console.error('Audio playback error: ', error);
69
61
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
70
62
  }
71
- };
72
- throttleFn = (func)=>()=>{
63
+ }
64
+ throttleFn(func) {
65
+ return ()=>{
73
66
  const now = Date.now();
74
67
  if (now - this.lastClickTime >= this.throttle) {
75
- func();
68
+ func().catch((error)=>{
69
+ if (this.debug) console.error('Error in throttled function:', error);
70
+ });
76
71
  this.lastClickTime = now;
77
72
  }
78
73
  };
79
- play = async (url = this.file)=>{
74
+ }
75
+ play(url = this.file) {
80
76
  const throttledPlay = this.throttleFn(()=>this.playAudio(url));
81
77
  try {
82
- await throttledPlay();
78
+ throttledPlay();
83
79
  } catch (error) {
84
- if (this.debug) // eslint-disable-next-line no-console
85
- console.error("Audio playback error: ", error);
80
+ if (this.debug) console.error('Audio playback error: ', error);
86
81
  if (this.callback) this.callback(error);
87
82
  else throw error;
88
83
  }
89
- };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AAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF,EAAE;IAEK,OAAO,OAAO,MAAc,IAAI,CAAC,IAAI;QAC1C,MAAM,gBAAgB,IAAI,CAAC,UAAU,CAAC,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF,MAAM;QACR,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,sCAAsC;YACtC,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF,EAAE;AACJ;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":"AAMA,MAAM;IAUJ,YAAY,QACV,IAAI,UACJ,SAAS,eACT,WAAW,gBACX,WAAW,UACX,QAAQ,OAOT,CAAE;QACD,IAAI,CAAC,IA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,IAAM,IAAI,CAAC,SAAS,CAAC;QAE3D,IAAI;YACF;QACF,EAAE,OAAO,OAAO;YACd,IAAI,IAAI,CAAC,KAAK,EACZ,QAAQ,KAAK,CAAC,0BAA0B;YAG1C,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,QAAQ,CAAC;iBAEd,MAAM;QAEV;IACF;AACF;IAEA,2CAAe","sources":["src/index.ts"],"sourcesContent":["declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n private readonly volume: number;\n private readonly callback: ((error?: Error) => void) | null;\n private readonly throttle: number;\n private readonly debug: boolean;\n private lastClickTime: number;\n private readonly audioCache: Record<string, AudioBuffer>;\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext(): void {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n void this.audioContext!.resume()\n .then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n })\n .catch((error) => {\n if (this.debug) {\n console.error('AudioContext resume error:', error);\n }\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n }\n\n private async fetchAndDecodeAudio(url: string): Promise<AudioBuffer> {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(\n `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,\n );\n }\n }\n\n private async playAudio(url: string): Promise<void> {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = (): void => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n }\n\n private throttleFn(func: () => Promise<void>): () => void {\n return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n void func().catch((error) => {\n if (this.debug) {\n console.error('Error in throttled function:', error);\n }\n });\n\n this.lastClickTime = now;\n }\n };\n }\n\n public play(url: string = this.file): void {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n throttledPlay();\n } catch (error) {\n if (this.debug) {\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n }\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.module.js.map"}
@@ -0,0 +1,72 @@
1
+ import globals from 'globals';
2
+ import eslintPlugin from '@eslint/js';
3
+ import typescriptPlugin from '@typescript-eslint/eslint-plugin';
4
+ import typescriptParser from '@typescript-eslint/parser';
5
+ import prettierConfig from 'eslint-config-prettier';
6
+ import importPlugin from 'eslint-plugin-import';
7
+
8
+ /** @type {import("eslint").Linter.FlatConfig[]} */
9
+ export default [
10
+ {
11
+ ignores: ['node_modules', 'dist', '.parcel', '.parcel-cache'],
12
+ },
13
+ {
14
+ files: ['**/*.ts'],
15
+ languageOptions: {
16
+ ecmaVersion: 'latest',
17
+ sourceType: 'module',
18
+ parser: typescriptParser,
19
+ parserOptions: {
20
+ project: './tsconfig.json',
21
+ },
22
+ globals: {
23
+ ...globals.browser,
24
+ ...globals.es2022,
25
+ globalThis: 'readonly',
26
+ },
27
+ },
28
+ plugins: {
29
+ '@typescript-eslint': typescriptPlugin,
30
+ eslint: eslintPlugin,
31
+ import: importPlugin,
32
+ },
33
+ settings: {
34
+ 'import/resolver': {
35
+ typescript: {
36
+ project: './tsconfig.json',
37
+ },
38
+ },
39
+ },
40
+ rules: {
41
+ ...eslintPlugin.configs.recommended.rules,
42
+ ...typescriptPlugin.configs.recommended.rules,
43
+ ...typescriptPlugin.configs['recommended-requiring-type-checking'].rules,
44
+ '@typescript-eslint/no-unused-vars': 'error',
45
+ '@typescript-eslint/explicit-function-return-type': 'warn',
46
+ '@typescript-eslint/no-explicit-any': 'warn',
47
+ 'import/order': [
48
+ 'error',
49
+ {
50
+ groups: [['builtin', 'external', 'internal']],
51
+ 'newlines-between': 'always',
52
+ },
53
+ ],
54
+ '@typescript-eslint/triple-slash-reference': 'off',
55
+ '@typescript-eslint/no-empty-interface': 'off',
56
+ 'import/no-unresolved': 'error',
57
+ 'import/no-duplicates': 'error',
58
+ 'max-len': [
59
+ 'error',
60
+ {
61
+ code: 100,
62
+ ignoreUrls: true,
63
+ ignoreStrings: true,
64
+ ignoreTemplateLiterals: true,
65
+ ignoreComments: true,
66
+ },
67
+ ],
68
+ 'linebreak-style': ['error', 'unix'],
69
+ },
70
+ },
71
+ prettierConfig,
72
+ ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.5.4",
3
+ "version": "1.6.0",
4
4
  "description": "ClickTone is designed to control audio playback with various settings, including volume control, callback and debug mode. It also includes iOS support.",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
@@ -16,24 +16,30 @@
16
16
  "scripts": {
17
17
  "clean": "rm -rf dist .parcel-cache",
18
18
  "build": "yarn clean && parcel build",
19
- "lint:js": "eslint **/*.{ts,js}",
20
- "lintfix": "yarn lint:js --fix"
19
+ "lint:js": "eslint **/*.ts",
20
+ "lint:fix:js": "eslint **/*.ts --fix",
21
+ "format:js": "prettier --write **/*.ts",
22
+ "lint:fix": "yarn lint:fix:js && yarn format:js"
21
23
  },
22
24
  "browserslist": "> 0.5%, last 2 versions, not dead",
23
25
  "source": "src/index.ts",
24
26
  "main": "dist/index.js",
25
27
  "module": "dist/index.module.js",
26
- "types": "dist/index.d.ts",
28
+ "types": "dist/clicktone.d.ts",
27
29
  "devDependencies": {
28
- "@parcel/packager-ts": "2.12.0",
29
- "@parcel/transformer-typescript-types": "2.12.0",
30
- "@typescript-eslint/eslint-plugin": "^7.11.0",
31
- "@typescript-eslint/parser": "^7.11.0",
32
- "eslint": "^7.32.0 || ^8.57.0",
33
- "eslint-config-airbnb-base": "^15.0.0",
34
- "eslint-plugin-import": "^2.29.1",
35
- "parcel": "^2.12.0",
36
- "typescript": "^5.4.5"
30
+ "@eslint/js": "9.17.0",
31
+ "@parcel/packager-ts": "2.13.3",
32
+ "@parcel/transformer-typescript-types": "2.13.3",
33
+ "@typescript-eslint/eslint-plugin": "8.18.2",
34
+ "@typescript-eslint/parser": "8.18.2",
35
+ "eslint": "9.17.0",
36
+ "eslint-config-prettier": "9.1.0",
37
+ "eslint-import-resolver-typescript": "3.7.0",
38
+ "eslint-plugin-import": "2.31.0",
39
+ "globals": "15.14.0",
40
+ "parcel": "2.13.3",
41
+ "prettier": "3.4.2",
42
+ "typescript": "5.7.2"
37
43
  },
38
44
  "keywords": [
39
45
  "MP3",
package/src/index.ts CHANGED
@@ -6,19 +6,12 @@ declare global {
6
6
 
7
7
  class ClickTone {
8
8
  private readonly file: string;
9
-
10
9
  private readonly volume: number;
11
-
12
10
  private readonly callback: ((error?: Error) => void) | null;
13
-
14
11
  private readonly throttle: number;
15
-
16
12
  private readonly debug: boolean;
17
-
18
13
  private lastClickTime: number;
19
-
20
14
  private readonly audioCache: Record<string, AudioBuffer>;
21
-
22
15
  private audioContext: AudioContext | null;
23
16
 
24
17
  constructor({
@@ -51,23 +44,29 @@ class ClickTone {
51
44
  }
52
45
  }
53
46
 
54
- private iOSFixAudioContext = (): void => {
47
+ private iOSFixAudioContext(): void {
55
48
  if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {
56
49
  const unlock = (): void => {
57
50
  if (this.audioContext!.state === 'suspended') {
58
- this.audioContext!.resume().then(() => {
59
- document.body.removeEventListener('touchstart', unlock);
60
- document.body.removeEventListener('touchend', unlock);
61
- });
51
+ void this.audioContext!.resume()
52
+ .then(() => {
53
+ document.body.removeEventListener('touchstart', unlock);
54
+ document.body.removeEventListener('touchend', unlock);
55
+ })
56
+ .catch((error) => {
57
+ if (this.debug) {
58
+ console.error('AudioContext resume error:', error);
59
+ }
60
+ });
62
61
  }
63
62
  };
64
63
 
65
64
  document.body.addEventListener('touchstart', unlock, false);
66
65
  document.body.addEventListener('touchend', unlock, false);
67
66
  }
68
- };
67
+ }
69
68
 
70
- private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {
69
+ private async fetchAndDecodeAudio(url: string): Promise<AudioBuffer> {
71
70
  try {
72
71
  if (this.audioCache[url]) {
73
72
  return this.audioCache[url];
@@ -82,15 +81,16 @@ class ClickTone {
82
81
  return audioData;
83
82
  } catch (error) {
84
83
  if (this.debug) {
85
- // eslint-disable-next-line no-console
86
84
  console.error('Audio loading and decoding error: ', error);
87
85
  }
88
86
 
89
- throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);
87
+ throw new Error(
88
+ `Something went wrong when loading and decoding the audio: ${(error as Error).message}`,
89
+ );
90
90
  }
91
- };
91
+ }
92
92
 
93
- private playAudio = async (url: string): Promise<void> => {
93
+ private async playAudio(url: string): Promise<void> {
94
94
  this.initAudioContext();
95
95
 
96
96
  try {
@@ -103,7 +103,7 @@ class ClickTone {
103
103
  source.connect(gainNode);
104
104
  gainNode.connect(this.audioContext!.destination);
105
105
 
106
- source.onended = () => {
106
+ source.onended = (): void => {
107
107
  if (this.callback) {
108
108
  this.callback();
109
109
  }
@@ -112,32 +112,36 @@ class ClickTone {
112
112
  source.start(0);
113
113
  } catch (error) {
114
114
  if (this.debug) {
115
- // eslint-disable-next-line no-console
116
115
  console.error('Audio playback error: ', error);
117
116
  }
118
117
 
119
118
  throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);
120
119
  }
121
- };
120
+ }
122
121
 
123
- private throttleFn = (func: () => void): (() => void) => () => {
124
- const now = Date.now();
122
+ private throttleFn(func: () => Promise<void>): () => void {
123
+ return () => {
124
+ const now = Date.now();
125
125
 
126
- if (now - this.lastClickTime >= this.throttle) {
127
- func();
126
+ if (now - this.lastClickTime >= this.throttle) {
127
+ void func().catch((error) => {
128
+ if (this.debug) {
129
+ console.error('Error in throttled function:', error);
130
+ }
131
+ });
128
132
 
129
- this.lastClickTime = now;
130
- }
131
- };
133
+ this.lastClickTime = now;
134
+ }
135
+ };
136
+ }
132
137
 
133
- public play = async (url: string = this.file): Promise<void> => {
138
+ public play(url: string = this.file): void {
134
139
  const throttledPlay = this.throttleFn(() => this.playAudio(url));
135
140
 
136
141
  try {
137
- await throttledPlay();
142
+ throttledPlay();
138
143
  } catch (error) {
139
144
  if (this.debug) {
140
- // eslint-disable-next-line no-console
141
145
  console.error('Audio playback error: ', error);
142
146
  }
143
147
 
@@ -147,7 +151,7 @@ class ClickTone {
147
151
  throw error;
148
152
  }
149
153
  }
150
- };
154
+ }
151
155
  }
152
156
 
153
157
  export default ClickTone;
package/tsconfig.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "isolatedModules": true,
4
- "target": "es2022"
4
+ "target": "ESNext",
5
+ "module": "ESNext",
6
+ "strict": true,
7
+ "strictNullChecks": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "moduleResolution": "node",
12
+ "noImplicitAny": true
5
13
  }
6
14
  }
package/.eslintrc.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "env": {
3
- "browser": true,
4
- "es2022": true
5
- },
6
- "extends": [
7
- "airbnb-base",
8
- "plugin:@typescript-eslint/recommended"
9
- ],
10
- "parser": "@typescript-eslint/parser",
11
- "parserOptions": {
12
- "ecmaVersion": "latest",
13
- "sourceType": "module",
14
- "project": "./tsconfig.json"
15
- },
16
- "plugins": ["@typescript-eslint"],
17
- "rules": {
18
- "max-len": [
19
- "off",
20
- {
21
- "code": 100,
22
- "ignoreUrls": true
23
- }
24
- ]
25
- }
26
- }
@@ -1 +0,0 @@
1
- {"mappings":"AAAA,QAAQ,MAAM,CAAC;IACb,UAAU,MAAM;QACd,kBAAkB,CAAC,EAAE,OAAO,YAAY,CAAC;KAC1C;CACF;AAED,qBAAM,SAAS;gBAiBD,EACV,IAAI,EACJ,MAAY,EACZ,QAAe,EACf,QAAY,EACZ,KAAa,GACd,EAAE;QACD,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;QAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,KAAK,CAAC,EAAE,OAAO,CAAC;KACjB;IAiGM,IAAI,SAAe,MAAM,KAAe,QAAQ,IAAI,CAAC,CAiB1D;CACH;AAED,eAAe,SAAS,CAAC","sources":["src/src/index.ts","src/index.ts"],"sourcesContent":[null,"declare global {\n interface Window {\n webkitAudioContext?: typeof AudioContext;\n }\n}\n\nclass ClickTone {\n private readonly file: string;\n\n private readonly volume: number;\n\n private readonly callback: ((error?: Error) => void) | null;\n\n private readonly throttle: number;\n\n private readonly debug: boolean;\n\n private lastClickTime: number;\n\n private readonly audioCache: Record<string, AudioBuffer>;\n\n private audioContext: AudioContext | null;\n\n constructor({\n file,\n volume = 1.0,\n callback = null,\n throttle = 0,\n debug = false,\n }: {\n file: string;\n volume?: number;\n callback?: ((error?: Error) => void) | null;\n throttle?: number;\n debug?: boolean;\n }) {\n this.file = file;\n this.volume = volume;\n this.callback = callback;\n this.throttle = throttle;\n this.debug = debug;\n this.lastClickTime = 0;\n this.audioCache = {};\n this.audioContext = null;\n }\n\n private initAudioContext(): void {\n if (!this.audioContext) {\n this.audioContext = new (window.AudioContext || window.webkitAudioContext)();\n this.iOSFixAudioContext();\n }\n }\n\n private iOSFixAudioContext = (): void => {\n if (this.audioContext && this.audioContext.state === 'suspended' && 'ontouchstart' in window) {\n const unlock = (): void => {\n if (this.audioContext!.state === 'suspended') {\n this.audioContext!.resume().then(() => {\n document.body.removeEventListener('touchstart', unlock);\n document.body.removeEventListener('touchend', unlock);\n });\n }\n };\n\n document.body.addEventListener('touchstart', unlock, false);\n document.body.addEventListener('touchend', unlock, false);\n }\n };\n\n private fetchAndDecodeAudio = async (url: string): Promise<AudioBuffer> => {\n try {\n if (this.audioCache[url]) {\n return this.audioCache[url];\n }\n\n const response = await fetch(url);\n const buffer = await response.arrayBuffer();\n const audioData = await this.audioContext!.decodeAudioData(buffer);\n\n this.audioCache[url] = audioData;\n\n return audioData;\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio loading and decoding error: ', error);\n }\n\n throw new Error(`Something went wrong when loading and decoding the audio: ${(error as Error).message}`);\n }\n };\n\n private playAudio = async (url: string): Promise<void> => {\n this.initAudioContext();\n\n try {\n const audioData = await this.fetchAndDecodeAudio(url);\n const source = this.audioContext!.createBufferSource();\n const gainNode = this.audioContext!.createGain();\n\n source.buffer = audioData;\n gainNode.gain.value = this.volume;\n source.connect(gainNode);\n gainNode.connect(this.audioContext!.destination);\n\n source.onended = () => {\n if (this.callback) {\n this.callback();\n }\n };\n\n source.start(0);\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n throw new Error(`Something went wrong while playing audio: ${(error as Error).message}`);\n }\n };\n\n private throttleFn = (func: () => void): (() => void) => () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\n };\n\n public play = async (url: string = this.file): Promise<void> => {\n const throttledPlay = this.throttleFn(() => this.playAudio(url));\n\n try {\n await throttledPlay();\n } catch (error) {\n if (this.debug) {\n // eslint-disable-next-line no-console\n console.error('Audio playback error: ', error);\n }\n\n if (this.callback) {\n this.callback(error as Error);\n } else {\n throw error;\n }\n }\n };\n}\n\nexport default ClickTone;\n"],"names":[],"version":3,"file":"index.d.ts.map"}