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

Commit

Permalink
Throttle improvements (#17)
Browse files Browse the repository at this point in the history
* New build

* Fixed:
- Throttling is now only applied if the throttle option supplied was greater than 0
Added:
- Key up now flushes any throttled input

* Updated CHANGELOG

* Bumped minor version (2.4.0 -> 2.5.0)
  • Loading branch information
predikament authored and asgvard committed Jul 5, 2019
1 parent 3435e15 commit 982300e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 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.5.0]
### Fixed
- Throttling is now only applied if the throttle option supplied was greater than 0
### Added
- Key up now flushes any throttled input

## [2.4.0]
### Added
- added support for `onArrowPress` property, it enables to add a custom behavior when arrows are pressed and can prevent the default navigation.
Expand Down
30 changes: 24 additions & 6 deletions dist/spatialNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ var SpatialNavigation = function () {
*/
this.paused = false;

this.keyEventListener = null;
this.keyDownEventListener = null;
this.keyUpEventListener = null;
this.keyMap = DEFAULT_KEY_MAP;

this.onKeyEvent = this.onKeyEvent.bind(this);
Expand Down Expand Up @@ -429,7 +430,7 @@ var SpatialNavigation = function () {
var _this3 = this;

if (window) {
this.keyEventListener = function (event) {
this.keyDownEventListener = function (event) {
if (_this3.paused === true) {
return;
}
Expand All @@ -456,23 +457,40 @@ var SpatialNavigation = function () {
var preventDefaultNavigation = _this3.onArrowPress(eventType) === false;

if (preventDefaultNavigation) {
_this3.log('keyEventListener', 'default navigation prevented');
_this3.log('keyDownEventListener', 'default navigation prevented');
_this3.visualDebugger && _this3.visualDebugger.clear();
} else {
_this3.onKeyEvent(event.keyCode);
}
}
};

window.addEventListener('keydown', (0, _throttle2.default)(this.keyEventListener, this.throttle));
// 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);

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

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

window.addEventListener('keydown', this.keyDownEventListener);
}
}
}, {
key: 'unbindEventHandlers',
value: function unbindEventHandlers() {
if (window) {
window.removeEventListener('keydown', this.keyEventListener);
this.keyEventListener = null;
window.removeEventListener('keydown', this.keyDownEventListener);
this.keyDownEventListener = null;

if (this.throttle) {
window.removeEventListener('keyup', this.keyUpEventListener);
this.keyUpEventListener = null;
}
}
}
}, {
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.4.0",
"version": "2.5.0",
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
"main": "dist/index.js",
"scripts": {
Expand Down
28 changes: 22 additions & 6 deletions src/spatialNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ class SpatialNavigation {
*/
this.paused = false;

this.keyEventListener = null;
this.keyDownEventListener = null;
this.keyUpEventListener = null;
this.keyMap = DEFAULT_KEY_MAP;

this.onKeyEvent = this.onKeyEvent.bind(this);
Expand Down Expand Up @@ -363,7 +364,7 @@ class SpatialNavigation {

bindEventHandlers() {
if (window) {
this.keyEventListener = (event) => {
this.keyDownEventListener = (event) => {
if (this.paused === true) {
return;
}
Expand All @@ -388,22 +389,37 @@ class SpatialNavigation {
const preventDefaultNavigation = this.onArrowPress(eventType) === false;

if (preventDefaultNavigation) {
this.log('keyEventListener', 'default navigation prevented');
this.log('keyDownEventListener', 'default navigation prevented');
this.visualDebugger && this.visualDebugger.clear();
} else {
this.onKeyEvent(event.keyCode);
}
}
};

window.addEventListener('keydown', lodashThrottle(this.keyEventListener, this.throttle));
// 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);

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

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

window.addEventListener('keydown', this.keyDownEventListener);
}
}

unbindEventHandlers() {
if (window) {
window.removeEventListener('keydown', this.keyEventListener);
this.keyEventListener = null;
window.removeEventListener('keydown', this.keyDownEventListener);
this.keyDownEventListener = null;

if (this.throttle) {
window.removeEventListener('keyup', this.keyUpEventListener);
this.keyUpEventListener = null;
}
}
}

Expand Down

0 comments on commit 982300e

Please sign in to comment.