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

perf: Reduce updates #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 12 additions & 17 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,17 @@ const defaults = {
* A Video.js player.
* @param {Object} [options={}]
* An object of options left to the plugin author to define.
* @return {Function} disposal function for re initialization
*/
const onPlayerReady = (player, options) => {
player.addClass('vjs-quality-menu');

const controlBar = player.getChild('controlBar');

const button = controlBar.addChild(
controlBar.qualityMenuButton = controlBar.addChild(
'QualityMenuButton',
options,
controlBar.children_.length - 2
);

return function() {
player.removeClass('vjs-quality-menu');
controlBar.removeChild(button);
button.dispose();
};
};

/**
Expand All @@ -50,24 +43,26 @@ const onPlayerReady = (player, options) => {
* @param {Object} [options] an object with plugin options
*/
const initPlugin = function(player, options) {
if (typeof player.qualityLevels !== 'undefined') {
if (player.hasPlugin('qualityLevels')) {
// call qualityLevels to initialize it in case it hasnt been initialized yet
player.qualityLevels();

let disposeFn = () => {};

player.ready(() => {
disposeFn = onPlayerReady(player, options);

player.on('loadstart', () => {
disposeFn();
disposeFn = onPlayerReady(player, options);
});
onPlayerReady(player, options);
});

// reinitialization is no-op for now
player.qualityMenu = () => {};
player.qualityMenu.VERSION = VERSION;

player.qualityMenu.dispose = () => {
console.log('disposing plugin');
player.removeClass('vjs-quality-menu');
player.controlBar.removeChild(player.controlBar.qualityMenuButton);
player.controlBar.qualityMenuButton.dispose();
};

player.on('dispose', player.qualityMenu.dispose);
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/quality-menu-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class QualityMenuButton extends MenuButton {

this.qualityLevels_ = player.qualityLevels();

this.update = this.update.bind(this);
// Update is debounced because levels are generally added or removed in quick succession
this.update = videojs.fn.debounce(this.update.bind(this), 50);
this.hide = this.hide.bind(this);

this.handleQualityChange_ = this.handleQualityChange_.bind(this);
Expand All @@ -75,6 +76,7 @@ class QualityMenuButton extends MenuButton {

this.on(this.qualityLevels_, 'addqualitylevel', this.update);
this.on(this.qualityLevels_, 'removequalitylevel', this.update);
this.on(player, 'loadstart', this.update);
this.on(this.qualityLevels_, 'change', this.handleQualityChange_);

// TODO: Remove this and the `defaultResolution` option once videojs/http-streaming supports comparable functionality
Expand Down