Compare commits

..

1 Commits

Author SHA1 Message Date
e32e5d6f71 Better TTS stoping error suppression
All checks were successful
Build / Publish Docs (push) Successful in 56s
Build / Build NPM Project (push) Successful in 1m15s
Build / Tag Version (push) Successful in 10s
2026-01-19 10:28:35 -05:00
2 changed files with 8 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.28.10", "version": "0.28.11",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@@ -5,7 +5,7 @@ export class TTS {
private _currentUtterance: SpeechSynthesisUtterance | null = null; private _currentUtterance: SpeechSynthesisUtterance | null = null;
private _voicesLoaded: Promise<void>; private _voicesLoaded: Promise<void>;
private _isStopping: boolean = false; private _stoppedUtterances = new WeakSet<SpeechSynthesisUtterance>();
private _rate: number = 1.0; private _rate: number = 1.0;
get rate(): number { return this._rate; } get rate(): number { return this._rate; }
@@ -103,25 +103,25 @@ export class TTS {
await this._voicesLoaded; await this._voicesLoaded;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this._currentUtterance = this.createUtterance(text); this._currentUtterance = this.createUtterance(text);
this._currentUtterance.onend = () => { const utterance = this._currentUtterance;
utterance.onend = () => {
this._currentUtterance = null; this._currentUtterance = null;
resolve(); resolve();
}; };
this._currentUtterance.onerror = (error) => { utterance.onerror = (error) => {
this._currentUtterance = null; this._currentUtterance = null;
if(this._isStopping && error.error === 'interrupted') resolve(); if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve();
else reject(error); else reject(error);
}; };
window.speechSynthesis.speak(this._currentUtterance); window.speechSynthesis.speak(utterance);
}); });
} }
/** Stops all TTS */ /** Stops all TTS */
stop(): void { stop(): void {
this._isStopping = true; if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
window.speechSynthesis.cancel(); window.speechSynthesis.cancel();
this._currentUtterance = null; this._currentUtterance = null;
setTimeout(() => { this._isStopping = false; }, 0);
} }
/** /**