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: new 'player' method with support async loading & profiles #678

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions src/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ export const videoPlayer = cloudinary.videoPlayer;
export const videoPlayers = cloudinary.videoPlayers;
export const videoPlayerWithProfile = cloudinary.videoPlayerWithProfile;

export const player = cloudinary.player;

export default cloudinary;
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'assets/styles/main.scss';
import pick from 'lodash/pick';
import VideoPlayer from './video-player';
import createVideoPlayerProfile from './video-player-profile';
import createPlayer from './player';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we having two separate methods that does exactly the same?
If we really must, just do export const videoPlayerWithProfile = player

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is small difference between them (profile property as optional one) and there is possibility that some people are using it - in this case first we should deprecate it and later after some time just remove it

import { convertKeysToSnakeCase } from './utils/object';
import { CLOUDINARY_CONFIG_PARAM } from './video-player.const';

Expand All @@ -22,10 +23,14 @@ const getVideoPlayers = config => (selector, playerOptions, ready) =>

const getVideoPlayerWithProfile = config => (id, playerOptions, ready) => createVideoPlayerProfile(id, getConfig(playerOptions, config), ready);

const getPlayer = config => (id, playerOptions, ready) => createPlayer(id, getConfig(playerOptions, config), ready);

export const videoPlayer = getVideoPlayer();
export const videoPlayers = getVideoPlayers();
export const videoPlayerWithProfile = getVideoPlayerWithProfile();

export const player = getPlayer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what abut players method similar to videoPlayers

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const cloudinaryVideoPlayerLegacyConfig = config => {
console.warn(
'Cloudinary.new() is deprecated and will be removed. Please use cloudinary.videoPlayer() instead.'
Expand All @@ -41,6 +46,7 @@ const cloudinary = {
videoPlayer,
videoPlayers,
videoPlayerWithProfile,
player,
Cloudinary: {
// Backwards compatibility with SDK v1
new: cloudinaryVideoPlayerLegacyConfig
Expand Down
6 changes: 6 additions & 0 deletions src/index.player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is bundled as `player.js` to be imported as a tree-shaken module.

// Usage:
// import player from 'cloudinary-video-player/player';

export { player as default } from './index.js';
34 changes: 34 additions & 0 deletions src/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import VideoPlayer from './video-player';
import { defaultProfiles } from './config/profiles';

export const getProfile = async (cloudName, profile) => {
if (Object.keys(defaultProfiles).includes(profile)) {
return defaultProfiles[profile];
}

console.warn('Custom profiles loading mechanism will be changed soon');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

As a developer, if I see this, what do I do?

return await fetch(profile, { method: 'GET' }).then(res => res.json());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The await here is redundant (So is the async in the function declaration)

Suggested change
return await fetch(profile, { method: 'GET' }).then(res => res.json());
return fetch(profile, { method: 'GET' }).then(res => res.json());

};

const player = async (elem, initOptions, ready) => {
const { profile, ...filteredInitOptions } = initOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const { profile, ...filteredInitOptions } = initOptions;
const { profile, ...otherInitOptions } = initOptions;

try {
const profileOptions = profile ? await getProfile(filteredInitOptions.cloud_name, profile) : {};
const options = Object.assign({}, profileOptions.playerOptions, filteredInitOptions);
const videoPlayer = new VideoPlayer(elem, options, ready);

const nativeVideoPlayerSourceMethod = videoPlayer.source;
videoPlayer.source = (id, options) => {
const extendedOptions = Object.assign({}, profileOptions.sourceOptions, options);
return nativeVideoPlayerSourceMethod.call(videoPlayer, id, extendedOptions);
};

return videoPlayer;
} catch (e) {
const videoPlayer = new VideoPlayer(elem, filteredInitOptions);
videoPlayer.videojs.error('Invalid profile');
throw e;
}
};

export default player;
2 changes: 2 additions & 0 deletions src/video-player-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const getProfile = async (cloudName, profile) => {
};

const videoPlayerProfile = async (elem, initOptions, ready) => {
console.warn('videoPlayerProfile method is DEPRECATED and will be removed soon, please use new `player` method instead');
ehab-cl marked this conversation as resolved.
Show resolved Hide resolved

if (!initOptions.profile) {
throw new Error('VideoPlayerProfile method requires "profile" property');
}
Expand Down
1 change: 1 addition & 0 deletions webpack/es6.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = merge(webpackCommon, {
'cld-video-player': './index.es.js', // default
'videoPlayer': './index.videoPlayer.js',
'videoPlayerWithProfile': './index.videoPlayerWithProfile.js',
'player': './index.player.js',
'all': './index.all.js'
},

Expand Down
Loading