clicktone 1.5.2 → 1.5.4

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.
package/.eslintrc.json CHANGED
@@ -1,15 +1,19 @@
1
1
  {
2
2
  "env": {
3
3
  "browser": true,
4
- "es2021": true
4
+ "es2022": true
5
5
  },
6
6
  "extends": [
7
- "airbnb-base"
7
+ "airbnb-base",
8
+ "plugin:@typescript-eslint/recommended"
8
9
  ],
10
+ "parser": "@typescript-eslint/parser",
9
11
  "parserOptions": {
10
12
  "ecmaVersion": "latest",
11
- "sourceType": "module"
13
+ "sourceType": "module",
14
+ "project": "./tsconfig.json"
12
15
  },
16
+ "plugins": ["@typescript-eslint"],
13
17
  "rules": {
14
18
  "max-len": [
15
19
  "off",
package/README.md CHANGED
@@ -33,15 +33,17 @@ import ClickTone from 'clicktone';
33
33
 
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
- const click = new ClickTone({
36
+ const sound = new ClickTone({
37
37
  file: './sound.mp3',
38
38
  volume: 0.7,
39
- throttle: 400,
39
+ throttle: 100,
40
40
  callback: () => { console.log('Playback ended') },
41
41
  debug: true,
42
42
  });
43
43
 
44
- myButton.addEventListener('click', () => click.play());
44
+ const play = () => sound.play();
45
+
46
+ button.addEventListener('pointerdown', play);
45
47
  ```
46
48
  <br>
47
49
 
@@ -1 +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;IAiGM,IAAI,SAAe,MAAM,KAAe,QAAQ,IAAI,CAAC,CAgB1D;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 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 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 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 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 return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\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 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"}
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"}
package/dist/index.js CHANGED
@@ -56,7 +56,8 @@ class $a196c1ed25598f0e$var$ClickTone {
56
56
  this.audioCache[url] = audioData;
57
57
  return audioData;
58
58
  } catch (error) {
59
- if (this.debug) console.error("Audio loading and decoding error: ", error);
59
+ if (this.debug) // eslint-disable-next-line no-console
60
+ console.error("Audio loading and decoding error: ", error);
60
61
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
61
62
  }
62
63
  };
@@ -75,25 +76,25 @@ class $a196c1ed25598f0e$var$ClickTone {
75
76
  };
76
77
  source.start(0);
77
78
  } catch (error) {
78
- if (this.debug) console.error("Audio playback error: ", error);
79
+ if (this.debug) // eslint-disable-next-line no-console
80
+ console.error("Audio playback error: ", error);
79
81
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
80
82
  }
81
83
  };
82
- throttleFn = (func)=>{
83
- return ()=>{
84
+ throttleFn = (func)=>()=>{
84
85
  const now = Date.now();
85
86
  if (now - this.lastClickTime >= this.throttle) {
86
87
  func();
87
88
  this.lastClickTime = now;
88
89
  }
89
90
  };
90
- };
91
91
  play = async (url = this.file)=>{
92
92
  const throttledPlay = this.throttleFn(()=>this.playAudio(url));
93
93
  try {
94
94
  await throttledPlay();
95
95
  } catch (error) {
96
- if (this.debug) console.error("Audio playback error: ", error);
96
+ if (this.debug) // eslint-disable-next-line no-console
97
+ console.error("Audio playback error: ", error);
97
98
  if (this.callback) this.callback(error);
98
99
  else throw error;
99
100
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;AAMA,MAAM;IACa,KAAa;IACb,OAAe;IACf,SAA2C;IAC3C,SAAiB;IACjB,MAAe;IACxB,cAAsB;IACb,WAAwC;IACjD,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;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,qBAAqB;QAC3B,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,C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E/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,CAAC,CAAC;QACzF;IACF,EAAE;IAEM,aAAa,CAAC;QACpB,OAAO;YACL,MAAM,MAAM,KAAK,GAAG;YAEpB,IAAI,MAAM,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,QAAQ,EAAE;gBAC7C;gBAEA,IAAI,CAAC,aAAa,GAAG;YACvB;QACF;IACF,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,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 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 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 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 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 return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\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 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;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;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,qBAAqB;QAC3B,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,KAAK,KAAK,eAAe,kBAAkB,QAAQ;YAC5F,MAAM,SAAS;gBACb,IAAI,IAAI,CAAC,YAAY,CAAE,KAAK,KAAK,aAC/B,IAAI,CAAC,YAAY,CAAE,MAAM,GAAG,IAAI,CAAC;oBAC/B,SAAS,IAAI,CAAC,mBAAmB,CAAC,cAAc;oBAChD,SAAS,IAAI,CAAC,mBAAmB,CAAC,YAAY;gBAChD;YAEJ;YAEA,SAAS,IAAI,CAAC,gBAAgB,CAAC,cAAc,QAAQ;YACrD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AAa,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"}
@@ -44,7 +44,8 @@ class $643fcf18b2d2e76f$var$ClickTone {
44
44
  this.audioCache[url] = audioData;
45
45
  return audioData;
46
46
  } catch (error) {
47
- if (this.debug) console.error("Audio loading and decoding error: ", error);
47
+ if (this.debug) // eslint-disable-next-line no-console
48
+ console.error("Audio loading and decoding error: ", error);
48
49
  throw new Error(`Something went wrong when loading and decoding the audio: ${error.message}`);
49
50
  }
50
51
  };
@@ -63,25 +64,25 @@ class $643fcf18b2d2e76f$var$ClickTone {
63
64
  };
64
65
  source.start(0);
65
66
  } catch (error) {
66
- if (this.debug) console.error("Audio playback error: ", error);
67
+ if (this.debug) // eslint-disable-next-line no-console
68
+ console.error("Audio playback error: ", error);
67
69
  throw new Error(`Something went wrong while playing audio: ${error.message}`);
68
70
  }
69
71
  };
70
- throttleFn = (func)=>{
71
- return ()=>{
72
+ throttleFn = (func)=>()=>{
72
73
  const now = Date.now();
73
74
  if (now - this.lastClickTime >= this.throttle) {
74
75
  func();
75
76
  this.lastClickTime = now;
76
77
  }
77
78
  };
78
- };
79
79
  play = async (url = this.file)=>{
80
80
  const throttledPlay = this.throttleFn(()=>this.playAudio(url));
81
81
  try {
82
82
  await throttledPlay();
83
83
  } catch (error) {
84
- if (this.debug) console.error("Audio playback error: ", error);
84
+ if (this.debug) // eslint-disable-next-line no-console
85
+ console.error("Audio playback error: ", error);
85
86
  if (this.callback) this.callback(error);
86
87
  else throw error;
87
88
  }
@@ -1 +1 @@
1
- {"mappings":"AAMA,MAAM;IACa,KAAa;IACb,OAAe;IACf,SAA2C;IAC3C,SAAiB;IACjB,MAAe;IACxB,cAAsB;IACb,WAAwC;IACjD,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,aA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{\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 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 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 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 return () => {\n const now = Date.now();\n\n if (now - this.lastClickTime >= this.throttle) {\n func();\n\n this.lastClickTime = now;\n }\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 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;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;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,qBAAqB;QAC3B,IAAI,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,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,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.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clicktone",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
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,7 +16,7 @@
16
16
  "scripts": {
17
17
  "clean": "rm -rf dist .parcel-cache",
18
18
  "build": "yarn clean && parcel build",
19
- "lint:js": "eslint --ext .ts,.js",
19
+ "lint:js": "eslint **/*.{ts,js}",
20
20
  "lintfix": "yarn lint:js --fix"
21
21
  },
22
22
  "browserslist": "> 0.5%, last 2 versions, not dead",
@@ -26,10 +26,12 @@
26
26
  "types": "dist/index.d.ts",
27
27
  "devDependencies": {
28
28
  "@parcel/packager-ts": "2.12.0",
29
- "@parcel/transformer-sass": "2.12.0",
30
29
  "@parcel/transformer-typescript-types": "2.12.0",
30
+ "@typescript-eslint/eslint-plugin": "^7.11.0",
31
+ "@typescript-eslint/parser": "^7.11.0",
31
32
  "eslint": "^7.32.0 || ^8.57.0",
32
33
  "eslint-config-airbnb-base": "^15.0.0",
34
+ "eslint-plugin-import": "^2.29.1",
33
35
  "parcel": "^2.12.0",
34
36
  "typescript": "^5.4.5"
35
37
  },
package/src/index.ts CHANGED
@@ -6,12 +6,19 @@ declare global {
6
6
 
7
7
  class ClickTone {
8
8
  private readonly file: string;
9
+
9
10
  private readonly volume: number;
11
+
10
12
  private readonly callback: ((error?: Error) => void) | null;
13
+
11
14
  private readonly throttle: number;
15
+
12
16
  private readonly debug: boolean;
17
+
13
18
  private lastClickTime: number;
19
+
14
20
  private readonly audioCache: Record<string, AudioBuffer>;
21
+
15
22
  private audioContext: AudioContext | null;
16
23
 
17
24
  constructor({
@@ -75,6 +82,7 @@ class ClickTone {
75
82
  return audioData;
76
83
  } catch (error) {
77
84
  if (this.debug) {
85
+ // eslint-disable-next-line no-console
78
86
  console.error('Audio loading and decoding error: ', error);
79
87
  }
80
88
 
@@ -104,6 +112,7 @@ class ClickTone {
104
112
  source.start(0);
105
113
  } catch (error) {
106
114
  if (this.debug) {
115
+ // eslint-disable-next-line no-console
107
116
  console.error('Audio playback error: ', error);
108
117
  }
109
118
 
@@ -111,16 +120,14 @@ class ClickTone {
111
120
  }
112
121
  };
113
122
 
114
- private throttleFn = (func: () => void): (() => void) => {
115
- return () => {
116
- const now = Date.now();
123
+ private throttleFn = (func: () => void): (() => void) => () => {
124
+ const now = Date.now();
117
125
 
118
- if (now - this.lastClickTime >= this.throttle) {
119
- func();
126
+ if (now - this.lastClickTime >= this.throttle) {
127
+ func();
120
128
 
121
- this.lastClickTime = now;
122
- }
123
- };
129
+ this.lastClickTime = now;
130
+ }
124
131
  };
125
132
 
126
133
  public play = async (url: string = this.file): Promise<void> => {
@@ -130,6 +137,7 @@ class ClickTone {
130
137
  await throttledPlay();
131
138
  } catch (error) {
132
139
  if (this.debug) {
140
+ // eslint-disable-next-line no-console
133
141
  console.error('Audio playback error: ', error);
134
142
  }
135
143