@ztimson/utils 0.28.10 → 0.28.11

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/dist/index.mjs CHANGED
@@ -2535,7 +2535,7 @@ class TTS {
2535
2535
  static QUALITY_PATTERNS = ["Google", "Microsoft", "Samantha", "Premium", "Natural", "Neural"];
2536
2536
  _currentUtterance = null;
2537
2537
  _voicesLoaded;
2538
- _isStopping = false;
2538
+ _stoppedUtterances = /* @__PURE__ */ new WeakSet();
2539
2539
  _rate = 1;
2540
2540
  get rate() {
2541
2541
  return this._rate;
@@ -2629,26 +2629,24 @@ class TTS {
2629
2629
  await this._voicesLoaded;
2630
2630
  return new Promise((resolve, reject) => {
2631
2631
  this._currentUtterance = this.createUtterance(text);
2632
- this._currentUtterance.onend = () => {
2632
+ const utterance = this._currentUtterance;
2633
+ utterance.onend = () => {
2633
2634
  this._currentUtterance = null;
2634
2635
  resolve();
2635
2636
  };
2636
- this._currentUtterance.onerror = (error) => {
2637
+ utterance.onerror = (error) => {
2637
2638
  this._currentUtterance = null;
2638
- if (this._isStopping && error.error === "interrupted") resolve();
2639
+ if (this._stoppedUtterances.has(utterance) && error.error === "interrupted") resolve();
2639
2640
  else reject(error);
2640
2641
  };
2641
- window.speechSynthesis.speak(this._currentUtterance);
2642
+ window.speechSynthesis.speak(utterance);
2642
2643
  });
2643
2644
  }
2644
2645
  /** Stops all TTS */
2645
2646
  stop() {
2646
- this._isStopping = true;
2647
+ if (this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
2647
2648
  window.speechSynthesis.cancel();
2648
2649
  this._currentUtterance = null;
2649
- setTimeout(() => {
2650
- this._isStopping = false;
2651
- }, 0);
2652
2650
  }
2653
2651
  /**
2654
2652
  * Initialize a stream that chunks text into sentences and speak them.