Skip to content

Commit db46578

Browse files
fix: prevent dispose error and text track duplicate listeners (#6984)
1 parent ffb690a commit db46578

File tree

6 files changed

+50
-19
lines changed

6 files changed

+50
-19
lines changed

src/js/tracks/text-track.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ class TextTrack extends Track {
184184
const activeCues = new TextTrackCueList(this.activeCues_);
185185
let changed = false;
186186
const timeupdateHandler = Fn.bind(this, function() {
187+
if (!this.tech_.isReady_ || this.tech_.isDisposed()) {
188+
return;
187189

190+
}
188191
// Accessing this.activeCues for the side-effects of updating itself
189192
// due to its nature as a getter function. Do not remove or cues will
190193
// stop updating!
@@ -196,10 +199,13 @@ class TextTrack extends Track {
196199
}
197200
});
198201

202+
const disposeHandler = () => {
203+
this.tech_.off('timeupdate', timeupdateHandler);
204+
};
205+
206+
this.tech_.one('dispose', disposeHandler);
199207
if (mode !== 'disabled') {
200-
this.tech_.ready(() => {
201-
this.tech_.on('timeupdate', timeupdateHandler);
202-
}, true);
208+
this.tech_.on('timeupdate', timeupdateHandler);
203209
}
204210

205211
Object.defineProperties(this, {
@@ -236,17 +242,19 @@ class TextTrack extends Track {
236242
if (!TextTrackMode[newMode]) {
237243
return;
238244
}
245+
if (mode === newMode) {
246+
return;
247+
}
248+
239249
mode = newMode;
240250
if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) {
241251
// On-demand load.
242252
loadTrack(this.src, this);
243253
}
254+
this.tech_.off('timeupdate', timeupdateHandler);
255+
244256
if (mode !== 'disabled') {
245-
this.tech_.ready(() => {
246-
this.tech_.on('timeupdate', timeupdateHandler);
247-
}, true);
248-
} else {
249-
this.tech_.off('timeupdate', timeupdateHandler);
257+
this.tech_.on('timeupdate', timeupdateHandler);
250258
}
251259
/**
252260
* An event that fires when mode changes on this track. This allows

test/unit/tech/tech.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ QUnit.test('dispose() should stop time tracking', function(assert) {
142142
QUnit.test('dispose() should clear all tracks that are passed when its created', function(assert) {
143143
const audioTracks = new AudioTrackList([new AudioTrack(), new AudioTrack()]);
144144
const videoTracks = new VideoTrackList([new VideoTrack(), new VideoTrack()]);
145-
const textTracks = new TextTrackList([new TextTrack({tech: {}}),
146-
new TextTrack({tech: {}})]);
145+
const pretech = new Tech();
146+
const textTracks = new TextTrackList([new TextTrack({tech: pretech}),
147+
new TextTrack({tech: pretech})]);
147148

148149
assert.equal(audioTracks.length, 2, 'should have two audio tracks at the start');
149150
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',
167168
'should hold text tracks that we passed'
168169
);
169170

171+
pretech.dispose();
170172
tech.dispose();
171173

172174
assert.equal(audioTracks.length, 0, 'should have zero audio tracks after dispose');

test/unit/tracks/html-track-element-list.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const defaultTech = {
66
textTracks() {},
77
on() {},
88
off() {},
9+
one() {},
910
currentTime() {}
1011
};
1112

test/unit/tracks/html-track-element.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ QUnit.test('fires loadeddata when track cues become populated', function(assert)
6565
changes++;
6666
};
6767
const htmlTrackElement = new HTMLTrackElement({
68-
tech() {}
68+
tech: this.tech
6969
});
7070

7171
htmlTrackElement.addEventListener('load', loadHandler);

test/unit/tracks/text-track-list-converter.test.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ if (Html5.supportsNativeTextTracks()) {
4848
tech: {
4949
crossOrigin() {
5050
return null;
51-
}
51+
},
52+
on() {},
53+
one() {}
5254
}
5355
});
5456

@@ -75,6 +77,7 @@ if (Html5.supportsNativeTextTracks()) {
7577
}
7678
};
7779
},
80+
on() {},
7881
crossOrigin() {
7982
return null;
8083
},
@@ -108,7 +111,9 @@ if (Html5.supportsNativeTextTracks()) {
108111
tech: {
109112
crossOrigin() {
110113
return null;
111-
}
114+
},
115+
on() {},
116+
one() {}
112117
}
113118
});
114119

@@ -140,6 +145,7 @@ if (Html5.supportsNativeTextTracks()) {
140145
crossOrigin() {
141146
return null;
142147
},
148+
on() {},
143149
textTracks() {
144150
return tt;
145151
},
@@ -169,7 +175,9 @@ QUnit.test('trackToJson_ produces correct representation for emulated track obje
169175
tech: {
170176
crossOrigin() {
171177
return null;
172-
}
178+
},
179+
on() {},
180+
one() {}
173181
}
174182
});
175183

@@ -191,7 +199,9 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
191199
tech: {
192200
crossOrigin() {
193201
return null;
194-
}
202+
},
203+
on() {},
204+
one() {}
195205
}
196206
});
197207

@@ -203,7 +213,9 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
203213
tech: {
204214
crossOrigin() {
205215
return null;
206-
}
216+
},
217+
on() {},
218+
one() {}
207219
}
208220
});
209221

@@ -227,6 +239,8 @@ QUnit.test('textTracksToJson produces good json output for emulated only', funct
227239
crossOrigin() {
228240
return null;
229241
},
242+
on() {},
243+
one() {},
230244
textTracks() {
231245
return tt;
232246
}
@@ -259,7 +273,9 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
259273
tech: {
260274
crossOrigin() {
261275
return null;
262-
}
276+
},
277+
on() {},
278+
one() {}
263279
}
264280
});
265281

@@ -271,7 +287,9 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
271287
tech: {
272288
crossOrigin() {
273289
return null;
274-
}
290+
},
291+
on() {},
292+
one() {}
275293
}
276294
});
277295

@@ -296,6 +314,8 @@ QUnit.test('jsonToTextTracks calls addRemoteTextTrack on the tech with emulated
296314
crossOrigin() {
297315
return null;
298316
},
317+
on() {},
318+
one() {},
299319
textTracks() {
300320
return tt;
301321
},

test/unit/tracks/text-tracks.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ QUnit.test('removes cuechange event when text track is hidden for emulated track
343343
numTextTrackChanges++;
344344
});
345345

346-
tt.mode = 'showing';
346+
tt.mode = 'disabled';
347347
this.clock.tick(1);
348348
assert.equal(
349349
numTextTrackChanges, 1,

0 commit comments

Comments
 (0)