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

Commit 982300e

Browse files
predikamentasgvard
authored andcommitted
Throttle improvements (#17)
* 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)
1 parent 3435e15 commit 982300e

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [2.5.0]
10+
### Fixed
11+
- Throttling is now only applied if the throttle option supplied was greater than 0
12+
### Added
13+
- Key up now flushes any throttled input
14+
915
## [2.4.0]
1016
### Added
1117
- added support for `onArrowPress` property, it enables to add a custom behavior when arrows are pressed and can prevent the default navigation.

dist/spatialNavigation.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ var SpatialNavigation = function () {
343343
*/
344344
this.paused = false;
345345

346-
this.keyEventListener = null;
346+
this.keyDownEventListener = null;
347+
this.keyUpEventListener = null;
347348
this.keyMap = DEFAULT_KEY_MAP;
348349

349350
this.onKeyEvent = this.onKeyEvent.bind(this);
@@ -429,7 +430,7 @@ var SpatialNavigation = function () {
429430
var _this3 = this;
430431

431432
if (window) {
432-
this.keyEventListener = function (event) {
433+
this.keyDownEventListener = function (event) {
433434
if (_this3.paused === true) {
434435
return;
435436
}
@@ -456,23 +457,40 @@ var SpatialNavigation = function () {
456457
var preventDefaultNavigation = _this3.onArrowPress(eventType) === false;
457458

458459
if (preventDefaultNavigation) {
459-
_this3.log('keyEventListener', 'default navigation prevented');
460+
_this3.log('keyDownEventListener', 'default navigation prevented');
460461
_this3.visualDebugger && _this3.visualDebugger.clear();
461462
} else {
462463
_this3.onKeyEvent(event.keyCode);
463464
}
464465
}
465466
};
466467

467-
window.addEventListener('keydown', (0, _throttle2.default)(this.keyEventListener, this.throttle));
468+
// Apply throttle only if the option we got is > 0 to avoid limiting the listener to every animation frame
469+
if (this.throttle) {
470+
this.keyDownEventListener = (0, _throttle2.default)(this.keyDownEventListener.bind(this), this.throttle);
471+
472+
// When throttling then make sure to only throttle key down and flush in the case of key up
473+
this.keyUpEventListener = function () {
474+
return _this3.keyDownEventListener.flush();
475+
};
476+
477+
window.addEventListener('keyup', this.keyUpEventListener);
478+
}
479+
480+
window.addEventListener('keydown', this.keyDownEventListener);
468481
}
469482
}
470483
}, {
471484
key: 'unbindEventHandlers',
472485
value: function unbindEventHandlers() {
473486
if (window) {
474-
window.removeEventListener('keydown', this.keyEventListener);
475-
this.keyEventListener = null;
487+
window.removeEventListener('keydown', this.keyDownEventListener);
488+
this.keyDownEventListener = null;
489+
490+
if (this.throttle) {
491+
window.removeEventListener('keyup', this.keyUpEventListener);
492+
this.keyUpEventListener = null;
493+
}
476494
}
477495
}
478496
}, {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@noriginmedia/react-spatial-navigation",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
55
"main": "dist/index.js",
66
"scripts": {

src/spatialNavigation.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ class SpatialNavigation {
287287
*/
288288
this.paused = false;
289289

290-
this.keyEventListener = null;
290+
this.keyDownEventListener = null;
291+
this.keyUpEventListener = null;
291292
this.keyMap = DEFAULT_KEY_MAP;
292293

293294
this.onKeyEvent = this.onKeyEvent.bind(this);
@@ -363,7 +364,7 @@ class SpatialNavigation {
363364

364365
bindEventHandlers() {
365366
if (window) {
366-
this.keyEventListener = (event) => {
367+
this.keyDownEventListener = (event) => {
367368
if (this.paused === true) {
368369
return;
369370
}
@@ -388,22 +389,37 @@ class SpatialNavigation {
388389
const preventDefaultNavigation = this.onArrowPress(eventType) === false;
389390

390391
if (preventDefaultNavigation) {
391-
this.log('keyEventListener', 'default navigation prevented');
392+
this.log('keyDownEventListener', 'default navigation prevented');
392393
this.visualDebugger && this.visualDebugger.clear();
393394
} else {
394395
this.onKeyEvent(event.keyCode);
395396
}
396397
}
397398
};
398399

399-
window.addEventListener('keydown', lodashThrottle(this.keyEventListener, this.throttle));
400+
// Apply throttle only if the option we got is > 0 to avoid limiting the listener to every animation frame
401+
if (this.throttle) {
402+
this.keyDownEventListener = lodashThrottle(this.keyDownEventListener.bind(this), this.throttle);
403+
404+
// When throttling then make sure to only throttle key down and flush in the case of key up
405+
this.keyUpEventListener = () => this.keyDownEventListener.flush();
406+
407+
window.addEventListener('keyup', this.keyUpEventListener);
408+
}
409+
410+
window.addEventListener('keydown', this.keyDownEventListener);
400411
}
401412
}
402413

403414
unbindEventHandlers() {
404415
if (window) {
405-
window.removeEventListener('keydown', this.keyEventListener);
406-
this.keyEventListener = null;
416+
window.removeEventListener('keydown', this.keyDownEventListener);
417+
this.keyDownEventListener = null;
418+
419+
if (this.throttle) {
420+
window.removeEventListener('keyup', this.keyUpEventListener);
421+
this.keyUpEventListener = null;
422+
}
407423
}
408424
}
409425

0 commit comments

Comments
 (0)