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