1
0
mirror of https://github.com/videojs/video.js.git synced 2025-07-17 01:42:41 +02:00

fix: prevent dispose error and text track duplicate listeners (#6984)

This commit is contained in:
Brandon Casey
2021-01-21 17:02:15 -05:00
committed by GitHub
parent ffb690af6f
commit db46578ac6
6 changed files with 50 additions and 19 deletions

View File

@ -184,7 +184,10 @@ class TextTrack extends Track {
const activeCues = new TextTrackCueList(this.activeCues_); const activeCues = new TextTrackCueList(this.activeCues_);
let changed = false; let changed = false;
const timeupdateHandler = Fn.bind(this, function() { const timeupdateHandler = Fn.bind(this, function() {
if (!this.tech_.isReady_ || this.tech_.isDisposed()) {
return;
}
// Accessing this.activeCues for the side-effects of updating itself // Accessing this.activeCues for the side-effects of updating itself
// due to its nature as a getter function. Do not remove or cues will // due to its nature as a getter function. Do not remove or cues will
// stop updating! // stop updating!
@ -196,10 +199,13 @@ class TextTrack extends Track {
} }
}); });
const disposeHandler = () => {
this.tech_.off('timeupdate', timeupdateHandler);
};
this.tech_.one('dispose', disposeHandler);
if (mode !== 'disabled') { if (mode !== 'disabled') {
this.tech_.ready(() => { this.tech_.on('timeupdate', timeupdateHandler);
this.tech_.on('timeupdate', timeupdateHandler);
}, true);
} }
Object.defineProperties(this, { Object.defineProperties(this, {
@ -236,17 +242,19 @@ class TextTrack extends Track {
if (!TextTrackMode[newMode]) { if (!TextTrackMode[newMode]) {
return; return;
} }
if (mode === newMode) {
return;
}
mode = newMode; mode = newMode;
if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) { if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) {
// On-demand load. // On-demand load.
loadTrack(this.src, this); loadTrack(this.src, this);
} }
this.tech_.off('timeupdate', timeupdateHandler);
if (mode !== 'disabled') { if (mode !== 'disabled') {
this.tech_.ready(() => { this.tech_.on('timeupdate', timeupdateHandler);
this.tech_.on('timeupdate', timeupdateHandler);
}, true);
} else {
this.tech_.off('timeupdate', timeupdateHandler);
} }
/** /**
* An event that fires when mode changes on this track. This allows * An event that fires when mode changes on this track. This allows

View File

@ -142,8 +142,9 @@ QUnit.test('dispose() should stop time tracking', function(assert) {
QUnit.test('dispose() should clear all tracks that are passed when its created', function(assert) { QUnit.test('dispose() should clear all tracks that are passed when its created', function(assert) {
const audioTracks = new AudioTrackList([new AudioTrack(), new AudioTrack()]); const audioTracks = new AudioTrackList([new AudioTrack(), new AudioTrack()]);
const videoTracks = new VideoTrackList([new VideoTrack(), new VideoTrack()]); const videoTracks = new VideoTrackList([new VideoTrack(), new VideoTrack()]);
const textTracks = new TextTrackList([new TextTrack({tech: {}}), const pretech = new Tech();
new TextTrack({tech: {}})]); const textTracks = new TextTrackList([new TextTrack({tech: pretech}),
new TextTrack({tech: pretech})]);
assert.equal(audioTracks.length, 2, 'should have two audio tracks at the start'); assert.equal(audioTracks.length, 2, 'should have two audio tracks at the start');
assert.equal(videoTracks.length, 2, 'should have two video tracks at the start'); assert.equal(videoTracks.length, 2, 'should have two video tracks at the start');
@ -167,6 +168,7 @@ QUnit.test('dispose() should clear all tracks that are passed when its created',
'should hold text tracks that we passed' 'should hold text tracks that we passed'
); );
pretech.dispose();
tech.dispose(); tech.dispose();
assert.equal(audioTracks.length, 0, 'should have zero audio tracks after dispose'); assert.equal(audioTracks.length, 0, 'should have zero audio tracks after dispose');

View File

@ -6,6 +6,7 @@ const defaultTech = {
textTracks() {}, textTracks() {},
on() {}, on() {},
off() {}, off() {},
one() {},
currentTime() {} currentTime() {}
}; };

View File

@ -65,7 +65,7 @@ QUnit.test('fires loadeddata when track cues become populated', function(assert)
changes++; changes++;
}; };
const htmlTrackElement = new HTMLTrackElement({ const htmlTrackElement = new HTMLTrackElement({
tech() {} tech: this.tech
}); });
htmlTrackElement.addEventListener('load', loadHandler); htmlTrackElement.addEventListener('load', loadHandler);

View File

@ -48,7 +48,9 @@ if (Html5.supportsNativeTextTracks()) {
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -75,6 +77,7 @@ if (Html5.supportsNativeTextTracks()) {
} }
}; };
}, },
on() {},
crossOrigin() { crossOrigin() {
return null; return null;
}, },
@ -108,7 +111,9 @@ if (Html5.supportsNativeTextTracks()) {
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -140,6 +145,7 @@ if (Html5.supportsNativeTextTracks()) {
crossOrigin() { crossOrigin() {
return null; return null;
}, },
on() {},
textTracks() { textTracks() {
return tt; return tt;
}, },
@ -169,7 +175,9 @@ QUnit.test('trackToJson_ produces correct representation for emulated track obje
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -191,7 +199,9 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -203,7 +213,9 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -227,6 +239,8 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
crossOrigin() { crossOrigin() {
return null; return null;
}, },
on() {},
one() {},
textTracks() { textTracks() {
return tt; return tt;
} }
@ -259,7 +273,9 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -271,7 +287,9 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
tech: { tech: {
crossOrigin() { crossOrigin() {
return null; return null;
} },
on() {},
one() {}
} }
}); });
@ -296,6 +314,8 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
crossOrigin() { crossOrigin() {
return null; return null;
}, },
on() {},
one() {},
textTracks() { textTracks() {
return tt; return tt;
}, },

View File

@ -343,7 +343,7 @@ QUnit.test('removes cuechange event when text track is hidden for emulated track
numTextTrackChanges++; numTextTrackChanges++;
}); });
tt.mode = 'showing'; tt.mode = 'disabled';
this.clock.tick(1); this.clock.tick(1);
assert.equal( assert.equal(
numTextTrackChanges, 1, numTextTrackChanges, 1,