From 6e60258076d7950ff1e2d78e04f910d1b71e1a8c Mon Sep 17 00:00:00 2001 From: Joel Purra Date: Sun, 21 May 2017 16:46:43 +0200 Subject: [PATCH] Attempting fix for dead objects when closing options popup in firefox during speech --- src/background/background.js | 14 +++++++++++++- src/background/talkie-background.js | 17 +++++++++++++++-- src/background/talkie-speaker.js | 13 ++++++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/background/background.js b/src/background/background.js index 761c313e..3676444d 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -319,7 +319,19 @@ function main() { window.getAllVoices = () => talkieSpeaker.getAllVoices(); window.iconClick = () => talkieBackground.startStopSpeakSelectionOnPage(); window.stopSpeakFromFrontend = () => talkieBackground.stopSpeakingAction(); - window.startSpeakFromFrontend = (text, voice) => talkieBackground.startSpeakingTextInVoiceAction(text, voice); + window.startSpeakFromFrontend = (frontendText, frontendVoice) => { + // NOTE: not sure if copying these variables have any effect. + // NOTE: Hope it helps avoid some vague "TypeError: can't access dead object" in Firefox. + const text = "" + frontendText; + const voice = { + name: "" + frontendVoice.name, + lang: "" + frontendVoice.lang, + rate: 0 + frontendVoice.rate, + pitch: 0 + frontendVoice.pitch, + }; + + talkieBackground.startSpeakingTextInVoiceAction(text, voice); + }; window.getVersionName = () => metadataManager.getVersionName(); window.isFreeVersion = () => metadataManager.isFreeVersion(); diff --git a/src/background/talkie-background.js b/src/background/talkie-background.js index 9ead98cf..dd052646 100644 --- a/src/background/talkie-background.js +++ b/src/background/talkie-background.js @@ -20,6 +20,7 @@ along with Talkie. If not, see . import { logDebug, + logError, } from "../shared/log"; import { @@ -128,7 +129,13 @@ export default class TalkieBackground { () => this.talkieSpeaker.stopSpeaking() .then(() => { // NOTE: keeping the root chain separate from this chain. - this.speechChain.link(() => this.talkieSpeaker.speakTextInVoice(text, voice)); + this.speechChain.link(() => this.talkieSpeaker.speakTextInVoice(text, voice)) + .catch((error) => { + logError("Caught error on the speechChain. Swallowing. Resetting synthesizer just in case.", error); + + // TODO: handle internally in talkieSpeaker? + return this.talkieSpeaker.resetSynthesizer(); + }); return undefined; }) @@ -166,7 +173,13 @@ export default class TalkieBackground { () => this.talkieSpeaker.stopSpeaking() .then(() => { // NOTE: keeping the root chain separate from this chain. - this.speechChain.link(() => this.talkieSpeaker.speakTextInLanguage(text, language)); + this.speechChain.link(() => this.talkieSpeaker.speakTextInLanguage(text, language)) + .catch((error) => { + logError("Caught error on the speechChain. Swallowing. Resetting synthesizer just in case.", error); + + // TODO: handle internally in talkieSpeaker? + return this.talkieSpeaker.resetSynthesizer(); + }); return undefined; }) diff --git a/src/background/talkie-speaker.js b/src/background/talkie-speaker.js index 5c1b9e02..8f486904 100644 --- a/src/background/talkie-speaker.js +++ b/src/background/talkie-speaker.js @@ -55,7 +55,7 @@ export default class TalkieSpeaker { this.shouldContinueSpeakingProvider = shouldContinueSpeakingProvider; this.contentLogger = contentLogger; - this.cachedSynthesizer = null; + this.resetSynthesizer(); this.MAX_UTTERANCE_TEXT_LENGTH = 100; } @@ -153,6 +153,17 @@ export default class TalkieSpeaker { }); } + resetSynthesizer() { + return promiseTry( + () => { + delete this.cachedSynthesizer; + this.cachedSynthesizer = null; + + return undefined; + } + ); + } + getSynthesizer() { return promiseTry( () => {