Skip to content

Commit

Permalink
handle early cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
ken107 committed Apr 8, 2024
1 parent bffbc7c commit 0664055
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions js/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ function waitMillis(millis) {
});
}

function wait(observable, value) {
return rxjs.firstValueFrom(observable.pipe(rxjs.filter(x => x == value)))
}

function parseLang(lang) {
var tokens = lang.toLowerCase().replace(/_/g, '-').split(/-/, 2);
return {
Expand Down
7 changes: 7 additions & 0 deletions js/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function Doc(source, onEnd) {
.then(function() {return source.ready})
.then(function(result) {info = result})
var foundText;
const playbackState = new rxjs.BehaviorSubject("resumed")

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

//method close
function close() {
playbackState.error({name: "CancellationException", message: "Playback cancelled"})
return ready
.catch(function() {})
.then(function() {
Expand All @@ -161,12 +163,15 @@ function Doc(source, onEnd) {
async function play() {
if (activeSpeech) return activeSpeech.play();
await ready
await wait(playbackState, "resumed")
currentIndex = await source.getCurrentIndex()
await wait(playbackState, "resumed")
return readCurrent()
}

async function readCurrent(rewinded) {
const texts = await source.getTexts(currentIndex).catch(err => null)
await wait(playbackState, "resumed")
if (texts) {
if (texts.length) {
foundText = true;
Expand All @@ -187,10 +192,12 @@ function Doc(source, onEnd) {
texts = texts.map(preprocess)
if (info.detectedLang == null) {
const lang = await detectLanguage(texts)
await wait(playbackState, "resumed")
info.detectedLang = lang || "";
}
if (activeSpeech) return;
activeSpeech = await getSpeech(texts);
await wait(playbackState, "resumed")
activeSpeech.onEnd = function(err) {
if (err) {
if (onEnd) onEnd(err);
Expand Down
1 change: 1 addition & 0 deletions js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ $(function() {

function handleError(err) {
if (!err) return;
if (err.name == "CancellationException") return;

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

0 comments on commit 0664055

Please sign in to comment.