Skip to content

Commit

Permalink
fix(FEC-10732, FEC-10759): player params are not injected to addition…
Browse files Browse the repository at this point in the history
…al instances config (#385)
  • Loading branch information
yairans authored Dec 8, 2020
1 parent f34b3d8 commit 8c5a6c7
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 70 deletions.
36 changes: 21 additions & 15 deletions src/common/plugins/plugins-config-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const defaultConfig: dataStoreType = {
}
};

let config = Utils.Object.copyDeep(defaultConfig);
const templateRegex = new RegExp('{{.*}}');

/**
Expand Down Expand Up @@ -88,36 +87,43 @@ const removeUndefineds = (obj: Object = {}): Object =>
return product;
}, {});

const pluginConfig = {
class PluginConfigStore {
_config: dataStoreType;

/**
* constructor
* @constructor
*/
constructor() {
this._config = Utils.Object.copyDeep(defaultConfig);
}

/**
* return the token store object
* @private
* @returns {*|any} - token store object
*/
get: (): dataStoreType => {
return config;
},
get(): dataStoreType {
return this._config;
}
/**
* recalculate the token store data, if new config with token is passed then add it to the data store, and if
* an existing token needs to be removed then remove it
* @private
* @param {?dataStoreType} pluginsConfig - the new config object
* @returns {void}
*/
set: (pluginsConfig: ?dataStoreType): void => {
set(pluginsConfig: ?dataStoreType): void {
if (pluginsConfig) {
const newConfig = resolveNewConfig(pluginsConfig);
config = removeUndefineds(Utils.Object.mergeDeep(config, newConfig));
this._config = removeUndefineds(Utils.Object.mergeDeep(this._config, newConfig));
}
},
}
/**
* reset the config store to its initial state
* @private
* @returns {void}
*/
reset: (): void => {
config = Utils.Object.copyDeep(defaultConfig);
reset(): void {
this._config = Utils.Object.copyDeep(defaultConfig);
}
};
}

export {pluginConfig, templateRegex};
export {PluginConfigStore, templateRegex};
91 changes: 50 additions & 41 deletions src/common/plugins/plugins-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@flow
import {pluginConfig, templateRegex} from './plugins-config-store.js';
import {PluginConfigStore, templateRegex} from './plugins-config-store.js';
import evaluate from '../utils/evaluate';
import {getReferrer} from '../utils/kaltura-params';
import {Utils} from '@playkit-js/playkit-js';
Expand Down Expand Up @@ -111,45 +111,6 @@ function getEncodedReferrer(): string {
return encodeURIComponent(referrer);
}

/**
* @param {KPPluginsConfigObject} options - plugins options
* @param {KPOptionsObject} config - player config
* @private
* @return {void}
*/
function evaluatePluginsConfig(options: ?KPPluginsConfigObject, config: KPOptionsObject): void {
if (options) {
pluginConfig.set(options);
const dataModel = getModel(config);
const mergedConfig = Utils.Object.mergeDeep({}, pluginConfig.get(), options);
const evaluatedConfig = _formatConfigString(evaluate(JSON.stringify(mergedConfig), dataModel));
_mergeConfig(options, evaluatedConfig);
}
}

/**
* @param {KPUIOptionsObject} options - UI options
* @param {KPOptionsObject} config - player config
* @private
* @return {void}
*/
function evaluateUIConfig(options: KPUIOptionsObject, config: KPOptionsObject): void {
if (options) {
const defaultUiConfig = {
components: {
share: {
shareUrl: `{{embedBaseUrl}}/index.php/extwidget/preview/partner_id/{{partnerId}}/uiconf_id/{{uiConfId}}/entry_id/{{entryId}}/embed/dynamic`,
embedUrl: `{{embedBaseUrl}}/p/{{partnerId}}/embedPlaykitJs/uiconf_id/{{uiConfId}}?iframeembed=true&entry_id={{entryId}}`
}
}
};
const dataModel = getModel(config);
const mergedConfig = Utils.Object.mergeDeep({}, defaultUiConfig, options);
const evaluatedConfig = _formatConfigString(evaluate(JSON.stringify(mergedConfig), dataModel));
_mergeConfig(options, evaluatedConfig);
}
}

/**
*
* @param {string} config - the config string
Expand Down Expand Up @@ -190,4 +151,52 @@ function _mergeConfig(data: Object, evaluatedConfig: Object): void {
}
}

export {evaluatePluginsConfig, evaluateUIConfig, getEncodedReferrer};
class ConfigEvaluator {
_pluginConfigStore: PluginConfigStore;
/**
* constructor
* @constructor
*/
constructor() {
this._pluginConfigStore = new PluginConfigStore();
}

/**
* @param {KPPluginsConfigObject} options - plugins options
* @param {KPOptionsObject} config - player config
* @return {void}
*/
evaluatePluginsConfig(options: ?KPPluginsConfigObject, config: KPOptionsObject): void {
if (options) {
this._pluginConfigStore.set(options);
const dataModel = getModel(config);
const mergedConfig = Utils.Object.mergeDeep({}, this._pluginConfigStore.get(), options);
const evaluatedConfig = _formatConfigString(evaluate(JSON.stringify(mergedConfig), dataModel));
_mergeConfig(options, evaluatedConfig);
}
}

/**
* @param {KPUIOptionsObject} options - UI options
* @param {KPOptionsObject} config - player config
* @return {void}
*/
evaluateUIConfig(options: KPUIOptionsObject, config: KPOptionsObject): void {
if (options) {
const defaultUiConfig = {
components: {
share: {
shareUrl: `{{embedBaseUrl}}/index.php/extwidget/preview/partner_id/{{partnerId}}/uiconf_id/{{uiConfId}}/entry_id/{{entryId}}/embed/dynamic`,
embedUrl: `{{embedBaseUrl}}/p/{{partnerId}}/embedPlaykitJs/uiconf_id/{{uiConfId}}?iframeembed=true&entry_id={{entryId}}`
}
}
};
const dataModel = getModel(config);
const mergedConfig = Utils.Object.mergeDeep({}, defaultUiConfig, options);
const evaluatedConfig = _formatConfigString(evaluate(JSON.stringify(mergedConfig), dataModel));
_mergeConfig(options, evaluatedConfig);
}
}
}

export {ConfigEvaluator, getEncodedReferrer};
14 changes: 8 additions & 6 deletions src/kaltura-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {EventType as UIEventType} from '@playkit-js/playkit-js-ui';
import {Provider} from 'playkit-js-providers';
import {supportLegacyOptions, maybeSetStreamPriority, hasYoutubeSource} from './common/utils/setup-helpers';
import {addKalturaParams} from './common/utils/kaltura-params';
import {evaluatePluginsConfig, evaluateUIConfig} from './common/plugins';
import {ConfigEvaluator} from './common/plugins';
import {addKalturaPoster} from 'poster';
import './assets/style.css';
import {UIWrapper} from './common/ui-wrapper';
Expand Down Expand Up @@ -52,10 +52,13 @@ class KalturaPlayer extends FakeEventTarget {
_firstPlay: boolean = true;
_sourceSelected: boolean = false;
_pluginReadinessMiddleware: PluginReadinessMiddleware;
_configEvaluator: ConfigEvaluator;

constructor(options: KPOptionsObject) {
super();
const {sources, plugins} = options;
this._configEvaluator = new ConfigEvaluator();
this._configEvaluator.evaluatePluginsConfig(plugins, options);
const noSourcesOptions = Utils.Object.mergeDeep({}, options, {sources: null});
delete noSourcesOptions.plugins;
this._localPlayer = loadPlayer(noSourcesOptions);
Expand Down Expand Up @@ -194,8 +197,7 @@ class KalturaPlayer extends FakeEventTarget {
Object.keys(this._pluginsConfig).forEach(name => {
config.plugins[name] = {};
});
// $FlowFixMe
evaluatePluginsConfig(config.plugins, config);
this._configEvaluator.evaluatePluginsConfig(config.plugins, config);
this._configureOrLoadPlugins(config.plugins);
this._maybeCreateAdsController();
this._playlistManager.load(playlistData, playlistConfig, entryList);
Expand Down Expand Up @@ -225,15 +227,15 @@ class KalturaPlayer extends FakeEventTarget {
configure(config: Object = {}): void {
config = supportLegacyOptions(config);
const configDictionary = Utils.Object.mergeDeep({}, this.config, config);
evaluatePluginsConfig(config.plugins, configDictionary);
this._configEvaluator.evaluatePluginsConfig(config.plugins, configDictionary);
this._configureOrLoadPlugins(config.plugins);
const localPlayerConfig = Utils.Object.mergeDeep({}, config);
delete localPlayerConfig.plugins;
this._localPlayer.configure(localPlayerConfig);
this._maybeCreateAdsController();
const uiConfig = config.ui;
if (uiConfig) {
evaluateUIConfig(uiConfig, this.config);
this._configEvaluator.evaluateUIConfig(uiConfig, this.config);
this._uiWrapper.setConfig(uiConfig);
}
if (config.playlist) {
Expand Down Expand Up @@ -762,7 +764,7 @@ class KalturaPlayer extends FakeEventTarget {
_maybeSetEmbedConfig(): void {
const ui = this.config.ui;
if (ui && ui.components && ui.components.share) {
evaluateUIConfig(ui, this.config);
this._configEvaluator.evaluateUIConfig(ui, this.config);
this._uiWrapper.setConfig(ui);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import {KalturaPlayer} from './kaltura-player';
import {getPlayerProxy} from './proxy';
import {evaluatePluginsConfig} from './common/plugins';
import {
applyCastSupport,
applyStorageSupport,
Expand Down Expand Up @@ -29,7 +28,6 @@ function setup(options: PartialKPOptionsObject | LegacyPartialKPOptionsObject):
const defaultOptions = getDefaultOptions(options);
setLogOptions(defaultOptions);
printSetupMessages();
evaluatePluginsConfig(defaultOptions.plugins, defaultOptions);
setStorageConfig(defaultOptions);
const player = getPlayerProxy(defaultOptions);
setStorageTextStyle(player);
Expand Down
12 changes: 7 additions & 5 deletions test/src/common/plugin/plugins-config.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {evaluatePluginsConfig, getEncodedReferrer} from '../../../../src/common/plugins';
import {ConfigEvaluator, getEncodedReferrer} from '../../../../src/common/plugins';

let sandbox = sinon.createSandbox();

Expand All @@ -17,25 +17,27 @@ describe('evaluatePluginsConfig', function () {
}
};

const configEvaluator = new ConfigEvaluator();

it('should save the function after evaluatePluginsConfig called', function () {
evaluatePluginsConfig(pluginsConfig, playerConfig);
configEvaluator.evaluatePluginsConfig(pluginsConfig, playerConfig);
pluginsConfig.kava.myHandler.should.exist;
pluginsConfig.kava.myHandler.should.be.instanceof(Function);
});

it('should evaluate plugins config', function () {
evaluatePluginsConfig(pluginsConfig, playerConfig);
configEvaluator.evaluatePluginsConfig(pluginsConfig, playerConfig);
pluginsConfig.kava.partnerId.should.exist;
pluginsConfig.kava.partnerId.should.equal(1234);
});

it('should remove unevaluated plugins config', function () {
evaluatePluginsConfig(pluginsConfig, playerConfig);
configEvaluator.evaluatePluginsConfig(pluginsConfig, playerConfig);
pluginsConfig.kava.should.not.have.property('myUnevaluatedConfig');
});

it('should remove unevaluated plugins config from array', function () {
evaluatePluginsConfig(pluginsConfig, playerConfig);
configEvaluator.evaluatePluginsConfig(pluginsConfig, playerConfig);
pluginsConfig.kava.myArray.should.deep.equal([1, 'value', 0, true, false]);
});
});
Expand Down
Loading

0 comments on commit 8c5a6c7

Please sign in to comment.