diff --git a/src/states/AdsDone.js b/src/states/AdsDone.js index 3be52045..a2a44d6f 100644 --- a/src/states/AdsDone.js +++ b/src/states/AdsDone.js @@ -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; diff --git a/src/states/BeforePreroll.js b/src/states/BeforePreroll.js index 1db964e9..c1b27858 100644 --- a/src/states/BeforePreroll.js +++ b/src/states/BeforePreroll.js @@ -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; } diff --git a/src/states/ContentPlayback.js b/src/states/ContentPlayback.js index aa64ec99..8ff4187a 100644 --- a/src/states/ContentPlayback.js +++ b/src/states/ContentPlayback.js @@ -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 diff --git a/src/states/Midroll.js b/src/states/Midroll.js index b033bc0b..27c43fe9 100644 --- a/src/states/Midroll.js +++ b/src/states/Midroll.js @@ -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. diff --git a/src/states/Postroll.js b/src/states/Postroll.js index 495047b3..fb01e922 100644 --- a/src/states/Postroll.js +++ b/src/states/Postroll.js @@ -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; @@ -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; @@ -71,6 +85,9 @@ export default class Postroll extends AdState { } } + /* + * Postroll skipped, time to clean up. + */ skipLinearAdMode() { const player = this.player; @@ -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)'); @@ -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); @@ -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); @@ -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; @@ -135,6 +176,9 @@ export default class Postroll extends AdState { player.trigger('ended'); } + /* + * Cleanup timeouts and state. + */ cleanup() { const player = this.player; diff --git a/src/states/Preroll.js b/src/states/Preroll.js index 175f8274..2775fc91 100644 --- a/src/states/Preroll.js +++ b/src/states/Preroll.js @@ -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'); @@ -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)'); diff --git a/src/states/abstract/State.js b/src/states/abstract/State.js index 3d7bf5ed..3e4f5798 100644 --- a/src/states/abstract/State.js +++ b/src/states/abstract/State.js @@ -2,6 +2,10 @@ import videojs from 'video.js'; export default class State { + static _getName() { + return 'Anonymous State'; + } + constructor(player) { this.player = player; } @@ -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); } @@ -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()); } /* diff --git a/test/states/abstract/test.State.js b/test/states/abstract/test.State.js index 305cc687..731c63be 100644 --- a/test/states/abstract/test.State.js +++ b/test/states/abstract/test.State.js @@ -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; } @@ -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) {