Skip to content

Commit

Permalink
refactor: use the new any event function (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored and gkatsev committed Jul 29, 2019
1 parent 2e495dd commit 3c932c5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 35 deletions.
20 changes: 10 additions & 10 deletions src/js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
18 changes: 6 additions & 12 deletions src/js/tracks/text-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 6 additions & 13 deletions test/unit/tracks/text-track.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 3c932c5

Please sign in to comment.