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

feat: configure refactor #122

Merged
merged 4 commits into from
Sep 25, 2017
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
5 changes: 4 additions & 1 deletion src/player-config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"sources": {},
"plugins": {},
"metadata": {
"poster": ""
},
"plugins": {},
"playback": {
"audioLanguage": "",
"textLanguage": "",
"volume": 1,
"playsinline": false,
"preload": "none",
Expand Down
102 changes: 51 additions & 51 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ export default class Player extends FakeEventTarget {
super();
this._env = Env;
this._tracks = [];
this._config = {};
this._firstPlay = true;
this._config = Player._defaultConfig;
this._eventManager = new EventManager();
this._posterManager = new PosterManager();
this._stateManager = new StateManager(this);
Expand All @@ -172,7 +172,6 @@ export default class Player extends FakeEventTarget {
this._createReadyPromise();
this._createPlayerContainer();
this._appendPosterEl();
this._loadPlugins(config);
this.configure(config);
}

Expand All @@ -182,25 +181,58 @@ export default class Player extends FakeEventTarget {
* @returns {void}
*/
configure(config: Object): void {
this._maybeResetPlayer(config);
this._config = Utils.Object.mergeDeep(Utils.Object.isEmptyObject(this._config) ? Player._defaultConfig : this._config, config);
if (this._selectEngine()) {
this._appendEngineEl();
this._posterManager.setSrc(this._config.metadata.poster);
this._posterManager.show();
this._attachMedia();
this._handlePlaybackConfig();
Utils.Object.mergeDeep(this._config, config);
this._configureOrLoadPlugins(config.plugins);
if (config.sources) {
this._maybeResetPlayer();
if (this._selectEngineByPriority()) {
this._appendEngineEl();
this._posterManager.setSrc(this._config.metadata.poster);
this._posterManager.show();
this._attachMedia();
this._handlePlaybackConfig();
}
}
}

/**
* Configures or load the plugins defined in the configuration.
* @param {Object} plugins - The new received plugins configuration.
* @private
* @returns {void}
*/
_configureOrLoadPlugins(plugins: Object = {}): void {
Object.keys(plugins).forEach((name) => {
// If the plugin is already exists in the registry we are updating his config
const plugin = this._pluginManager.get(name);
if (plugin) {
plugin.updateConfig(plugins[name]);
this._config.plugins[name] = plugin.getConfig();
} else {
// We allow to load plugins as long as the player has no engine
if (!this._engine) {
this._pluginManager.load(name, this, plugins[name]);
let plugin = this._pluginManager.get(name);
if (plugin) {
this._config.plugins[name] = plugin.getConfig();
if (typeof plugin.getMiddlewareImpl === "function") {
this._playbackMiddleware.use(plugin.getMiddlewareImpl());
}
}
} else {
delete this._config.plugins[name];
}
}
});
}

/**
* Resets the player in case of new sources with existing engine.
* @param {Object} config - The player configuration.
* @private
* @returns {void}
*/
_maybeResetPlayer(config: Object): void {
if (this._engine && config.sources) {
_maybeResetPlayer(): void {
if (this._engine) {
Player._logger.debug('New sources on existing engine: reset engine to change media');
this._reset();
}
Expand Down Expand Up @@ -228,9 +260,7 @@ export default class Player extends FakeEventTarget {
*/
_createReadyPromise(): void {
this._readyPromise = new Promise((resolve, reject) => {
this._eventManager.listen(this, CustomEvents.TRACKS_CHANGED, () => {
resolve();
});
this._eventManager.listen(this, CustomEvents.TRACKS_CHANGED, resolve);
this._eventManager.listen(this, Html5Events.ERROR, reject);
});
}
Expand Down Expand Up @@ -262,36 +292,6 @@ export default class Player extends FakeEventTarget {
return Utils.Object.copyDeep(DefaultPlayerConfig);
}

/**
* Loads the configured plugins.
* @param {Object} config - The player configuration.
* @private
* @returns {void}
*/
_loadPlugins(config: Object): void {
Player._logger.debug('Load plugins');
let plugins = config.plugins;
for (let name in plugins) {
this._pluginManager.load(name, this, plugins[name]);
let plugin = this._pluginManager.get(name);
if (plugin && typeof plugin.getMiddlewareImpl === "function") {
this._playbackMiddleware.use(plugin.getMiddlewareImpl());
}
}
}

/**
* Selects the engine to create based on a given configuration.
* @private
* @returns {boolean} - Whether a proper engine was found.
*/
_selectEngine(): boolean {
if (this._config.sources && this._config.playback && this._config.playback.streamPriority) {
return this._selectEngineByPriority();
}
return false;
}

/**
* Selects an engine to play a source according to a given stream priority.
* @return {boolean} - Whether a proper engine was found to play the given sources
Expand Down Expand Up @@ -370,11 +370,11 @@ export default class Player extends FakeEventTarget {
if (typeof this._config.playback.volume === 'number') {
this.volume = this._config.playback.volume;
}
if (this._config.playback.muted) {
this.muted = true;
if (typeof this._config.playback.muted === 'boolean') {
this.muted = this._config.playback.muted;
}
if (this._config.playback.playsinline) {
this.playsinline = true;
if (typeof this._config.playback.playsinline === 'boolean') {
this.playsinline = this._config.playback.playsinline;
}
if (this._config.playback.preload === "auto") {
/**
Expand Down Expand Up @@ -404,7 +404,7 @@ export default class Player extends FakeEventTarget {
}
let device = this._env.device.type;
let os = this._env.os.name;
if (device === 'mobile' || device === 'tablet') {
if (device) {
return (os === 'iOS') ? this.muted && this.playsinline : this.muted;
}
return true;
Expand Down
Loading