1
0
mirror of https://github.com/videojs/video.js.git synced 2024-12-25 02:42:10 +02:00

refactor: use the new any event function (#6080)

This commit is contained in:
Brandon Casey 2019-07-29 17:21:42 -04:00 committed by Gary Katsevman
parent 2e495dd5f5
commit 3c932c5f8e
3 changed files with 21 additions and 34 deletions

View File

@ -1528,20 +1528,20 @@ class Player extends Component {
// if the `sourceset` `src` was an empty string
// wait for a `loadstart` to update the cache to `currentSrc`.
// If a sourceset happens before a `loadstart`, we reset the state
// as this function will be called again.
if (!event.src) {
const updateCache = (e) => {
if (e.type !== 'sourceset') {
const techSrc = this.techGet('currentSrc');
this.lastSource_.tech = techSrc;
this.updateSourceCaches_(techSrc);
this.tech_.any(['sourceset', 'loadstart'], (e) => {
// if a sourceset happens before a `loadstart` there
// is nothing to do as this `handleTechSourceset_`
// will be called again and this will be handled there.
if (e.type === 'sourceset') {
return;
}
this.tech_.off(['sourceset', 'loadstart'], updateCache);
};
const techSrc = this.techGet('currentSrc');
this.tech_.one(['sourceset', 'loadstart'], updateCache);
this.lastSource_.tech = techSrc;
this.updateSourceCaches_(techSrc);
});
}
}
this.lastSource_ = {player: this.currentSource().src, tech: event.src};

View File

@ -93,19 +93,13 @@ const loadTrack = function(src, track) {
if (track.tech_) {
// to prevent use before define eslint error, we define loadHandler
// as a let here
let loadHandler;
const errorHandler = () => {
log.error(`vttjs failed to load, stopping trying to process ${track.src}`);
track.tech_.off('vttjsloaded', loadHandler);
};
loadHandler = () => {
track.tech_.off('vttjserror', errorHandler);
track.tech_.any(['vttjsloaded', 'vttjserror'], (event) => {
if (event.type === 'vttjserror') {
log.error(`vttjs failed to load, stopping trying to process ${track.src}`);
return;
}
return parseCues(responseBody, track);
};
track.tech_.one('vttjsloaded', loadHandler);
track.tech_.one('vttjserror', errorHandler);
});
}
} else {
parseCues(responseBody, track);

View File

@ -503,7 +503,6 @@ QUnit.test('stops processing if vttjs loading errored out', function(assert) {
const errorSpy = sinon.spy();
const oldVTT = window.WebVTT;
const oldLogError = log.error;
const parserCreated = false;
const reqs = [];
window.xhr.onCreate = function(req) {
@ -529,20 +528,14 @@ QUnit.test('stops processing if vttjs loading errored out', function(assert) {
reqs.pop().respond(200, null, 'WEBVTT\n');
assert.ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
testTech.trigger('vttjserror');
assert.equal(errorSpy.callCount, 1, 'vttjs failed to load, so log.error was called');
testTech.trigger('vttjserror');
const offSpyCall = testTech.off.getCall(0);
assert.ok(errorSpy.called, 'vttjs failed to load, so log.error was called');
if (errorSpy.called) {
assert.ok(
/^vttjs failed to load, stopping trying to process/.test(errorSpy.getCall(0).args[0]),
'log.error was called with the expected message'
);
}
assert.ok(!parserCreated, 'WebVTT is not loaded, do not try to parse yet');
assert.ok(offSpyCall, 'tech.off was called');
// vttjserror not called again
assert.equal(errorSpy.callCount, 1, 'vttjserror handler not called again');
clock.restore();
window.WebVTT = oldVTT;