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

fix: throttle clickable component #8450

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
18 changes: 17 additions & 1 deletion src/js/clickable-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Component from './component';
import * as Dom from './utils/dom.js';
import log from './utils/log.js';
import keycode from 'keycode';
import { throttle } from './utils/fn';

/**
* Component which is clickable or keyboard actionable, but is not a
Expand Down Expand Up @@ -32,6 +33,10 @@ class ClickableComponent extends Component {
* @param {string} [options.className]
* A class or space separated list of classes to add the component
*
* @param {number | boolean} [options.throttle]
* A throttle will be applied to the clickHander if the number is >= 1 or the value is `true`
mister-ben marked this conversation as resolved.
Show resolved Hide resolved
* A number specifies the desired wait time in ms or a default wait of 50ms will be applied
Copy link
Member

Choose a reason for hiding this comment

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

any details on how 50 was chosen as the default?

Copy link
Contributor Author

@Essk Essk Sep 29, 2023

Choose a reason for hiding this comment

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

Nothing empirical, just a guess on my part based on previous similar solutions I've worked on. However, this is a good point so I measured the gap on my fairly modern Windows PC, and got 1-2 ms. Sadly FF doesn't have the same performance spoofing options as Chrome (which doesn't receive double input) but maybe I can get more data from Browserstack or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad - I had hoped Browserstack might have options to set hardware limitations, but it does not.

*
*/
constructor(player, options) {

Expand All @@ -41,9 +46,20 @@ class ClickableComponent extends Component {
this.controlText(this.options_.controlText);
}

const selectClickHandler = () => {
if (typeof this.options_.throttle === 'number' || this.options_.throttle === true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: You could create a variable and reuse it here, something like:

const isNumber = typeof this.options_.throttle === 'number';

const wait = typeof this.options_.throttle === 'number' ? parseInt(this.options_.throttle, 10) : 50;
Copy link
Member

Choose a reason for hiding this comment

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

Also, should this be parseFloat instead of parseInt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured since throttle's key condition is (now - last >= wait) where now - last will always be a whole number of milliseconds, parseInt was most appropriate.

Looking with fresh eyes, maybe the parseX(arg-that-might-be-a-number) should be removed entirely as I really just put it in out of habit and I see throttle itself doesn't enforce anything.


return throttle(this.handleClick.bind(this), wait);
}
return this.handleClick.bind(this);
};

const selectedClickHander = selectClickHandler();
Copy link
Contributor

Choose a reason for hiding this comment

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

nit sp:

Suggested change
const selectedClickHander = selectClickHandler();
const selectedClickHandler = selectClickHandler();


this.handleMouseOver_ = (e) => this.handleMouseOver(e);
this.handleMouseOut_ = (e) => this.handleMouseOut(e);
this.handleClick_ = (e) => this.handleClick(e);
Copy link
Member

Choose a reason for hiding this comment

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

since we still want a bound handleClick inside of selectedClickHandler, should we keep this around somewhere and use that rather than rebinding inside selectedClickHandler?

this.handleClick_ = (e) => selectedClickHander(e);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this.handleClick_ = (e) => selectedClickHander(e);
this.handleClick_ = (e) => selectedClickHandler(e);

this.handleKeyDown_ = (e) => this.handleKeyDown(e);

this.emitTapEvents();
Expand Down
1 change: 1 addition & 0 deletions src/js/control-bar/fullscreen-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FullscreenToggle extends Button {
* The key/value store of player options.
*/
constructor(player, options) {
options = Object.assign({}, options, {throttle: true});
super(player, options);
this.setIcon('fullscreen-enter');
this.on(player, 'fullscreenchange', (e) => this.handleFullscreenChange(e));
Expand Down
1 change: 1 addition & 0 deletions src/js/control-bar/mute-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MuteToggle extends Button {
* The key/value store of player options.
*/
constructor(player, options) {
options = Object.assign({}, options, {throttle: true});
super(player, options);

// hide this control if volume support is missing
Expand Down
1 change: 1 addition & 0 deletions src/js/control-bar/picture-in-picture-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PictureInPictureToggle extends Button {
* @listens Player#leavepictureinpicture
*/
constructor(player, options) {
options = Object.assign({}, options, {throttle: true});
super(player, options);

this.setIcon('picture-in-picture-enter');
Expand Down
1 change: 1 addition & 0 deletions src/js/control-bar/play-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PlayToggle extends Button {
* The key/value store of player options.
*/
constructor(player, options = {}) {
options = Object.assign({}, options, {throttle: true});
super(player, options);

// show or hide replay icon
Expand Down
64 changes: 63 additions & 1 deletion test/unit/clickable-component.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
/* eslint-env qunit */
import { useFakeTimers } from 'sinon';
import ClickableComponent from '../../src/js/clickable-component.js';
import TestHelpers from './test-helpers.js';
import * as Events from '../../src/js/utils/events.js';

QUnit.module('ClickableComponent');
QUnit.module('ClickableComponent', {
beforeEach() {
this.clock = useFakeTimers();
},

afterEach() {
this.clock.restore();
}
});

QUnit.test('should create a div with role="button"', function(assert) {
assert.expect(2);
Expand Down Expand Up @@ -154,3 +163,56 @@ QUnit.test('class and text should be settable from options', function(assert) {
testClickableComponent.dispose();
player.dispose();
});

QUnit.test('should respect throttle option', function(assert) {
assert.expect(8);
let clicks = 0;

class ThrottlableComponent extends ClickableComponent {
constructor(player, options) {
super(player, options);
}
handleClick() {
clicks++;
}
}

const player = TestHelpers.makePlayer({});
const noThrottleComponent = new ThrottlableComponent(player);
const throttledComponent = new ThrottlableComponent(player, {throttle: true});
const customThrottledComponent1 = new ThrottlableComponent(player, {throttle: 10 });
const customThrottledComponent2 = new ThrottlableComponent(player, {throttle: 0 });
const noThrottleEl = noThrottleComponent.el();
const throttledEl = throttledComponent.el();
const customThrottledEl1 = customThrottledComponent1.el();
const customThrottledEl2 = customThrottledComponent2.el();

const testThrottledClicks = (el, wait, elName) => {
clicks = 0;
// We need to wait for the durarion of the throttle wait
// parameter before proceeding to test throttled functions
this.clock.tick(wait);
Events.trigger(el, 'click');
assert.equal(clicks, 1, `${elName}: First click is handled`);
Events.trigger(el, 'click');
assert.equal(clicks, 1, `${elName}: Second click is ignored`);
// allow time before next click
this.clock.tick(wait);
Events.trigger(el, 'click');
assert.equal(clicks, 2, `${elName}: third click is handled`);
};

// 2 instantaneous clicks on non-throttled el
Events.trigger(noThrottleEl, 'click');
Events.trigger(noThrottleEl, 'click');
assert.equal(clicks, 2, 'click on enabled ClickableComponent is handled twice');

clicks = 0;
// 0 wait is not throttled
Events.trigger(customThrottledEl2, 'click');
Events.trigger(customThrottledEl2, 'click');
assert.equal(clicks, 2, 'click on enabled ClickableComponent with wait of 0 is handled twice');

testThrottledClicks(throttledEl, 50, 'default wait');
testThrottledClicks(customThrottledEl1, 10, '10ms wait');
});
3 changes: 3 additions & 0 deletions test/unit/controls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import sinon from 'sinon';
QUnit.module('Controls', {
beforeEach(assert) {
this.clock = sinon.useFakeTimers();
// because some click events are throttled we need to tick the clock
// forward for test click events to be handled
this.clock.tick(50);
},
afterEach(assert) {
this.clock.restore();
Expand Down