Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Slight improvements in throttle implementation (#18)
Browse files Browse the repository at this point in the history
* Added throttling options and key up now calls .cancel instead of .flush

* New build

* Bumped minor version to reflect changes (v2.5.0 -> v2.6.0)

* New version and changes reflected in CHANGELOG.md
  • Loading branch information
predikament authored and asgvard committed Jul 10, 2019
1 parent 982300e commit c3895da
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.6.0]
### Fixed
- Key up triggers `.cancel()` instead of `.flush()`
### Added
- Throttling now applies options to disable trailing functions

## [2.5.0]
### Fixed
- Throttling is now only applied if the throttle option supplied was greater than 0
Expand Down
11 changes: 8 additions & 3 deletions dist/spatialNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ var DEFAULT_KEY_MAP = (_DEFAULT_KEY_MAP = {}, _defineProperty(_DEFAULT_KEY_MAP,

var DEBUG_FN_COLORS = ['#0FF', '#FF0', '#F0F'];

var THROTTLE_OPTIONS = {
leading: true,
trailing: false
};

/* eslint-disable no-nested-ternary */

var SpatialNavigation = function () {
Expand Down Expand Up @@ -467,11 +472,11 @@ var SpatialNavigation = function () {

// Apply throttle only if the option we got is > 0 to avoid limiting the listener to every animation frame
if (this.throttle) {
this.keyDownEventListener = (0, _throttle2.default)(this.keyDownEventListener.bind(this), this.throttle);
this.keyDownEventListener = (0, _throttle2.default)(this.keyDownEventListener.bind(this), this.throttle, THROTTLE_OPTIONS);

// When throttling then make sure to only throttle key down and flush in the case of key up
// When throttling then make sure to only throttle key down and cancel any queued functions in case of key up
this.keyUpEventListener = function () {
return _this3.keyDownEventListener.flush();
return _this3.keyDownEventListener.cancel();
};

window.addEventListener('keyup', this.keyUpEventListener);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noriginmedia/react-spatial-navigation",
"version": "2.5.0",
"version": "2.6.0",
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
"main": "dist/index.js",
"scripts": {
Expand Down
12 changes: 9 additions & 3 deletions src/spatialNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ const DEFAULT_KEY_MAP = {

const DEBUG_FN_COLORS = ['#0FF', '#FF0', '#F0F'];

const THROTTLE_OPTIONS = {
leading: true,
trailing: false
};

/* eslint-disable no-nested-ternary */
class SpatialNavigation {
/**
Expand Down Expand Up @@ -399,10 +404,11 @@ class SpatialNavigation {

// Apply throttle only if the option we got is > 0 to avoid limiting the listener to every animation frame
if (this.throttle) {
this.keyDownEventListener = lodashThrottle(this.keyDownEventListener.bind(this), this.throttle);
this.keyDownEventListener =
lodashThrottle(this.keyDownEventListener.bind(this), this.throttle, THROTTLE_OPTIONS);

// When throttling then make sure to only throttle key down and flush in the case of key up
this.keyUpEventListener = () => this.keyDownEventListener.flush();
// When throttling then make sure to only throttle key down and cancel any queued functions in case of key up
this.keyUpEventListener = () => this.keyDownEventListener.cancel();

window.addEventListener('keyup', this.keyUpEventListener);
}
Expand Down

0 comments on commit c3895da

Please sign in to comment.