From e32e5d6f71aec145f8e69d7e4a758d6d885c855a Mon Sep 17 00:00:00 2001 From: ztimson Date: Mon, 19 Jan 2026 10:28:35 -0500 Subject: [PATCH] Better TTS stoping error suppression --- package.json | 2 +- src/tts.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 9eef052..f0f85d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.28.10", + "version": "0.28.11", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/tts.ts b/src/tts.ts index e3c924e..5b600b0 100644 --- a/src/tts.ts +++ b/src/tts.ts @@ -5,7 +5,7 @@ export class TTS { private _currentUtterance: SpeechSynthesisUtterance | null = null; private _voicesLoaded: Promise; - private _isStopping: boolean = false; + private _stoppedUtterances = new WeakSet(); private _rate: number = 1.0; get rate(): number { return this._rate; } @@ -103,25 +103,25 @@ export class TTS { await this._voicesLoaded; return new Promise((resolve, reject) => { this._currentUtterance = this.createUtterance(text); - this._currentUtterance.onend = () => { + const utterance = this._currentUtterance; + utterance.onend = () => { this._currentUtterance = null; resolve(); }; - this._currentUtterance.onerror = (error) => { + utterance.onerror = (error) => { this._currentUtterance = null; - if(this._isStopping && error.error === 'interrupted') resolve(); + if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve(); else reject(error); }; - window.speechSynthesis.speak(this._currentUtterance); + window.speechSynthesis.speak(utterance); }); } /** Stops all TTS */ stop(): void { - this._isStopping = true; + if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance); window.speechSynthesis.cancel(); this._currentUtterance = null; - setTimeout(() => { this._isStopping = false; }, 0); } /**