Skip to content

Commit

Permalink
fix: prevent dispose error and text track duplicate listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Dec 7, 2020
1 parent 5a13e90 commit a4d71b3
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/js/tracks/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ class TextTrack extends Track {
const activeCues = new TextTrackCueList(this.activeCues_);
let changed = false;
const timeupdateHandler = Fn.bind(this, function() {
if (!this.tech_.isReady_ || this.tech_.isDisposed()) {
return;

}
// Accessing this.activeCues for the side-effects of updating itself
// due to its nature as a getter function. Do not remove or cues will
// stop updating!
Expand All @@ -196,10 +199,14 @@ class TextTrack extends Track {
}
});

const disposeHandler = () => {
this.tech_.off('timeupdate', timeupdateHandler);
this.tech_.off('dispose', disposeHandler);
};

this.tech_.on('dispose', disposeHandler);
if (mode !== 'disabled') {
this.tech_.ready(() => {
this.tech_.on('timeupdate', timeupdateHandler);
}, true);
this.tech_.on('timeupdate', timeupdateHandler);
}

Object.defineProperties(this, {
Expand Down Expand Up @@ -241,12 +248,10 @@ class TextTrack extends Track {
// On-demand load.
loadTrack(this.src, this);
}
this.tech_.off('timeupdate', timeupdateHandler);

if (mode !== 'disabled') {
this.tech_.ready(() => {
this.tech_.on('timeupdate', timeupdateHandler);
}, true);
} else {
this.tech_.off('timeupdate', timeupdateHandler);
this.tech_.on('timeupdate', timeupdateHandler);
}
/**
* An event that fires when mode changes on this track. This allows
Expand Down

0 comments on commit a4d71b3

Please sign in to comment.