Skip to content

Commit 0664055

Browse files
committed
handle early cancellation
1 parent bffbc7c commit 0664055

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

js/defaults.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,10 @@ function waitMillis(millis) {
431431
});
432432
}
433433

434+
function wait(observable, value) {
435+
return rxjs.firstValueFrom(observable.pipe(rxjs.filter(x => x == value)))
436+
}
437+
434438
function parseLang(lang) {
435439
var tokens = lang.toLowerCase().replace(/_/g, '-').split(/-/, 2);
436440
return {

js/document.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ function Doc(source, onEnd) {
136136
.then(function() {return source.ready})
137137
.then(function(result) {info = result})
138138
var foundText;
139+
const playbackState = new rxjs.BehaviorSubject("resumed")
139140

140141
this.close = close;
141142
this.play = play;
@@ -149,6 +150,7 @@ function Doc(source, onEnd) {
149150

150151
//method close
151152
function close() {
153+
playbackState.error({name: "CancellationException", message: "Playback cancelled"})
152154
return ready
153155
.catch(function() {})
154156
.then(function() {
@@ -161,12 +163,15 @@ function Doc(source, onEnd) {
161163
async function play() {
162164
if (activeSpeech) return activeSpeech.play();
163165
await ready
166+
await wait(playbackState, "resumed")
164167
currentIndex = await source.getCurrentIndex()
168+
await wait(playbackState, "resumed")
165169
return readCurrent()
166170
}
167171

168172
async function readCurrent(rewinded) {
169173
const texts = await source.getTexts(currentIndex).catch(err => null)
174+
await wait(playbackState, "resumed")
170175
if (texts) {
171176
if (texts.length) {
172177
foundText = true;
@@ -187,10 +192,12 @@ function Doc(source, onEnd) {
187192
texts = texts.map(preprocess)
188193
if (info.detectedLang == null) {
189194
const lang = await detectLanguage(texts)
195+
await wait(playbackState, "resumed")
190196
info.detectedLang = lang || "";
191197
}
192198
if (activeSpeech) return;
193199
activeSpeech = await getSpeech(texts);
200+
await wait(playbackState, "resumed")
194201
activeSpeech.onEnd = function(err) {
195202
if (err) {
196203
if (onEnd) onEnd(err);

js/popup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ $(function() {
7878

7979
function handleError(err) {
8080
if (!err) return;
81+
if (err.name == "CancellationException") return;
8182

8283
if (/^{/.test(err.message)) {
8384
var errInfo = JSON.parse(err.message);

0 commit comments

Comments
 (0)