Skip to content

Commit

Permalink
fix: fix state logging when minified (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
incompl authored Mar 13, 2018
1 parent c9efa94 commit ae38894
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/states/AdsDone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import {ContentState} from '../states.js';

export default class AdsDone extends ContentState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'AdsDone';
}

/*
* For state transitions to work correctly, initialization should
* happen here, not in a constructor.
*/
init(player) {
// From now on, `ended` events won't be redispatched
player.ads._contentHasEnded = true;
Expand Down
11 changes: 11 additions & 0 deletions src/states/BeforePreroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import cancelContentPlay from '../cancelContentPlay.js';
*/
export default class BeforePreroll extends ContentState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'BeforePreroll';
}

/*
* For state transitions to work correctly, initialization should
* happen here, not in a constructor.
*/
init(player) {
this.adsReady = false;
}
Expand Down
11 changes: 11 additions & 0 deletions src/states/ContentPlayback.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import {ContentState, Midroll, Postroll} from '../states.js';
*/
export default class ContentPlayback extends ContentState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'AdsDone';
}

/*
* For state transitions to work correctly, initialization should
* happen here, not in a constructor.
*/
init(player) {
// Play the content if cancelContentPlay happened or we paused on 'contentupdate'
// and we haven't played yet. This happens if there was no preroll or if it
Expand Down
7 changes: 7 additions & 0 deletions src/states/Midroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import adBreak from '../adBreak.js';

export default class Midroll extends AdState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'Midroll';
}

/*
* Midroll breaks happen when the integration calls startLinearAdMode,
* which can happen at any time during content playback.
Expand Down
44 changes: 44 additions & 0 deletions src/states/Postroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import adBreak from '../adBreak.js';

export default class Postroll extends AdState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'Postroll';
}

/*
* For state transitions to work correctly, initialization should
* happen here, not in a constructor.
*/
init(player) {
// Legacy name that now simply means "handling postrolls".
player.ads._contentEnding = true;
Expand Down Expand Up @@ -57,6 +68,9 @@ export default class Postroll extends AdState {
player.removeClass('vjs-ad-loading');
}

/*
* Ending a postroll triggers the ended event.
*/
endLinearAdMode() {
const player = this.player;

Expand All @@ -71,6 +85,9 @@ export default class Postroll extends AdState {
}
}

/*
* Postroll skipped, time to clean up.
*/
skipLinearAdMode() {
const player = this.player;

Expand All @@ -83,11 +100,17 @@ export default class Postroll extends AdState {
}
}

/*
* Postroll timed out, time to clean up.
*/
onAdTimeout(player) {
player.ads.debug('Postroll abort (adtimeout)');
this.abort();
}

/*
* Postroll errored out, time to clean up.
*/
onAdsError(player) {
player.ads.debug('Postroll abort (adserror)');

Expand All @@ -101,6 +124,9 @@ export default class Postroll extends AdState {
this.abort();
}

/*
* On ended, transition to AdsDone state.
*/
onEnded() {
if (this.isContentResuming()) {
this.transitionTo(AdsDone);
Expand All @@ -109,14 +135,25 @@ export default class Postroll extends AdState {
}
}

/*
* Handle content change if we're not in an ad break.
*/
onContentChanged(player) {
// Content resuming after Postroll. Content is paused
// at this point, since it is done playing.
if (this.isContentResuming()) {
this.transitionTo(BeforePreroll);

// Waiting for postroll to start. Content is considered playing
// at this point, since it had to be playing to start the postroll.
} else if (!this.inAdBreak()) {
this.transitionTo(Preroll);
}
}

/*
* Wrap up if there is no postroll.
*/
onNoPostroll(player) {
if (!this.isContentResuming() && !this.inAdBreak()) {
this.transitionTo(AdsDone);
Expand All @@ -125,6 +162,10 @@ export default class Postroll extends AdState {
}
}

/*
* Helper for ending Postrolls. In the future we may want to
* refactor this class so that `cleanup` handles all of this.
*/
abort() {
const player = this.player;

Expand All @@ -135,6 +176,9 @@ export default class Postroll extends AdState {
player.trigger('ended');
}

/*
* Cleanup timeouts and state.
*/
cleanup() {
const player = this.player;

Expand Down
14 changes: 14 additions & 0 deletions src/states/Preroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import adBreak from '../adBreak.js';
*/
export default class Preroll extends AdState {

/*
* Allows state name to be logged even after minification.
*/
static _getName() {
return 'Preroll';
}

/*
* For state transitions to work correctly, initialization should
* happen here, not in a constructor.
*/
init(player, adsReady) {
// Loading spinner from now until ad start or end of ad break.
player.addClass('vjs-ad-loading');
Expand All @@ -35,6 +46,9 @@ export default class Preroll extends AdState {
}
}

/*
* Adsready event after play event.
*/
onAdsReady(player) {
if (!player.ads.inAdBreak() && !player.ads.isContentResuming()) {
player.ads.debug('Received adsready event (Preroll)');
Expand Down
15 changes: 10 additions & 5 deletions src/states/abstract/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import videojs from 'video.js';

export default class State {

static _getName() {
return 'Anonymous State';
}

constructor(player) {
this.player = player;
}
Expand All @@ -19,7 +23,8 @@ export default class State {
const newState = new NewState(player);

player.ads._state = newState;
player.ads.debug(previousState.constructor.name + ' -> ' + newState.constructor.name);
player.ads.debug(previousState.constructor._getName() + ' -> ' +
newState.constructor._getName());
newState.init(player, ...args);
}

Expand Down Expand Up @@ -61,22 +66,22 @@ export default class State {
*/
startLinearAdMode() {
videojs.log.warn('Unexpected startLinearAdMode invocation ' +
'(State via ' + this.constructor.name + ')');
'(State via ' + this.constructor._getName() + ')');
}
endLinearAdMode() {
videojs.log.warn('Unexpected endLinearAdMode invocation ' +
'(State via ' + this.constructor.name + ')');
'(State via ' + this.constructor._getName() + ')');
}
skipLinearAdMode() {
videojs.log.warn('Unexpected skipLinearAdMode invocation ' +
'(State via ' + this.constructor.name + ')');
'(State via ' + this.constructor._getName() + ')');
}

/*
* Overridden by ContentState and AdState. Should not be overriden elsewhere.
*/
isAdState() {
throw new Error('isAdState unimplemented for ' + this.constructor.name);
throw new Error('isAdState unimplemented for ' + this.constructor._getName());
}

/*
Expand Down
5 changes: 4 additions & 1 deletion test/states/abstract/test.State.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ QUnit.test('can transition to another state', function(assert) {
let mockStateInit = false;

class MockState {
static _getName() {
return 'MockState';
}
init() {
mockStateInit = true;
}
Expand All @@ -47,7 +50,7 @@ QUnit.test('throws error if isAdState is not implemented', function(assert) {
} catch(e) {
error = e;
}
assert.equal(error.message, 'isAdState unimplemented for State');
assert.equal(error.message, 'isAdState unimplemented for Anonymous State');
});

QUnit.test('is not resuming content by default', function(assert) {
Expand Down

0 comments on commit ae38894

Please sign in to comment.