Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JW Player Video Adapter: Support multiple setup listeners #9791

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions modules/jwplayerVideoProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,29 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba

function init() {
if (!jwplayer) {
triggerSetupFailure(-1); // TODO: come up with error code schema- player is absent
triggerSetupFailure({ code: -1 }); // TODO: come up with error code schema- player is absent
return;
}

playerVersion = jwplayer.version;

if (playerVersion < minimumSupportedPlayerVersion) {
triggerSetupFailure(-2); // TODO: come up with error code schema - version not supported
triggerSetupFailure({ code: -2 }); // TODO: come up with error code schema - version not supported
return;
}

if (!document.getElementById(divId)) {
triggerSetupFailure(-3); // TODO: come up with error code schema - missing div id
triggerSetupFailure({ code: -3 }); // TODO: come up with error code schema - missing div id
return;
}

player = jwplayer(divId);
if (!player || !player.getState) {
triggerSetupFailure(-4); // TODO: come up with error code schema - factory function failure
triggerSetupFailure({ code: -4 }); // TODO: come up with error code schema - factory function failure
} else if (player.getState() === undefined) {
setupPlayer(playerConfig);
} else {
const payload = getSetupCompletePayload();
setupCompleteCallbacks.forEach(callback => callback(SETUP_COMPLETE, payload));
setupCompleteCallbacks = [];
triggerSetupComplete();
}
}

Expand Down Expand Up @@ -192,8 +190,12 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba
function onEvent(externalEventName, callback, basePayload) {
if (externalEventName === SETUP_COMPLETE) {
setupCompleteCallbacks.push(callback);
} else if (externalEventName === SETUP_FAILED) {
return;
}

if (externalEventName === SETUP_FAILED) {
setupFailedCallbacks.push(callback);
return;
}

if (!player) {
Expand All @@ -203,25 +205,6 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba
let getEventPayload;

switch (externalEventName) {
case SETUP_COMPLETE:
getEventPayload = () => {
setupCompleteCallbacks = [];
return getSetupCompletePayload();
};
break;

case SETUP_FAILED:
getEventPayload = e => {
setupFailedCallbacks = [];
return {
playerVersion,
errorCode: e.code,
errorMessage: e.message,
sourceError: e.sourceError
};
};
break;

case AD_REQUEST:
case AD_PLAY:
case AD_PAUSE:
Expand Down Expand Up @@ -496,7 +479,17 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba
if (!config) {
return;
}
player.setup(utils.getJwConfig(config));
player.setup(utils.getJwConfig(config)).on('ready', triggerSetupComplete).on('setupError', triggerSetupFailure);
}

function triggerSetupComplete() {
if (!setupCompleteCallbacks.length) {
return;
}

const payload = getSetupCompletePayload();
setupCompleteCallbacks.forEach(callback => callback(SETUP_COMPLETE, payload));
setupCompleteCallbacks = [];
}

function getSetupCompletePayload() {
Expand All @@ -511,7 +504,7 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba
};
}

function triggerSetupFailure(errorCode) {
function triggerSetupFailure(e) {
if (!setupFailedCallbacks.length) {
return;
}
Expand All @@ -520,9 +513,9 @@ export function JWPlayerProvider(config, jwplayer_, adState_, timeState_, callba
divId,
playerVersion,
type: SETUP_FAILED,
errorCode,
errorMessage: '',
sourceError: null
errorCode: e.code,
errorMessage: e.message,
sourceError: e.sourceError
};

setupFailedCallbacks.forEach(callback => callback(SETUP_FAILED, payload));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { PLAYBACK_MODE } from 'libraries/video/constants/constants.js';
function getPlayerMock() {
return makePlayerFactoryMock({
getState: function () {},
setup: function () {},
setup: function () { return this; },
getViewable: function () {},
getPercentViewable: function () {},
getMute: function () {},
Expand All @@ -30,8 +30,8 @@ function getPlayerMock() {
getFullscreen: function () {},
getPlaylistItem: function () {},
playAd: function () {},
on: function () {},
off: function () {},
on: function () { return this; },
off: function () { return this; },
remove: function () {},
getAudioTracks: function () {},
getCurrentAudioTrack: function () {},
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('JWPlayerProvider', function () {
it('should instantiate the player when uninstantiated', function () {
const player = getPlayerMock();
config.playerConfig = {};
const setupSpy = player.setup = sinon.spy();
const setupSpy = player.setup = sinon.spy(player.setup);
const provider = JWPlayerProvider(config, makePlayerFactoryMock(player), adState, timeState, callbackStorage, utilsMock, sharedUtils);
provider.init();
expect(setupSpy.calledOnce).to.be.true;
Expand All @@ -158,6 +158,19 @@ describe('JWPlayerProvider', function () {
expect(setupComplete.calledOnce).to.be.true;
});

it('should support multiple setup complete event handlers', function () {
const player = getPlayerMock();
player.getState = () => 'idle';
const provider = JWPlayerProvider(config, makePlayerFactoryMock(player), adState, timeState, callbackStorage, utilsMock, sharedUtils);
const setupComplete = sinon.spy();
const setupComplete2 = sinon.spy();
provider.onEvent(SETUP_COMPLETE, setupComplete, {});
provider.onEvent(SETUP_COMPLETE, setupComplete2, {});
provider.init();
expect(setupComplete.calledOnce).to.be.true;
expect(setupComplete2.calledOnce).to.be.true;
});

it('should not reinstantiate player', function () {
const player = getPlayerMock();
player.getState = () => 'idle';
Expand Down