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

Commit c3895da

Browse files
predikamentasgvard
authored andcommitted
Slight improvements in throttle implementation (#18)
* 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
1 parent 982300e commit c3895da

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
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.6.0]
10+
### Fixed
11+
- Key up triggers `.cancel()` instead of `.flush()`
12+
### Added
13+
- Throttling now applies options to disable trailing functions
14+
915
## [2.5.0]
1016
### Fixed
1117
- Throttling is now only applied if the throttle option supplied was greater than 0

dist/spatialNavigation.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ var DEFAULT_KEY_MAP = (_DEFAULT_KEY_MAP = {}, _defineProperty(_DEFAULT_KEY_MAP,
8282

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

85+
var THROTTLE_OPTIONS = {
86+
leading: true,
87+
trailing: false
88+
};
89+
8590
/* eslint-disable no-nested-ternary */
8691

8792
var SpatialNavigation = function () {
@@ -467,11 +472,11 @@ var SpatialNavigation = function () {
467472

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

472-
// When throttling then make sure to only throttle key down and flush in the case of key up
477+
// When throttling then make sure to only throttle key down and cancel any queued functions in case of key up
473478
this.keyUpEventListener = function () {
474-
return _this3.keyDownEventListener.flush();
479+
return _this3.keyDownEventListener.cancel();
475480
};
476481

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

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.5.0",
3+
"version": "2.6.0",
44
"description": "HOC-based Spatial Navigation (key navigation) solution for React",
55
"main": "dist/index.js",
66
"scripts": {

src/spatialNavigation.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ const DEFAULT_KEY_MAP = {
4040

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

43+
const THROTTLE_OPTIONS = {
44+
leading: true,
45+
trailing: false
46+
};
47+
4348
/* eslint-disable no-nested-ternary */
4449
class SpatialNavigation {
4550
/**
@@ -399,10 +404,11 @@ class SpatialNavigation {
399404

400405
// Apply throttle only if the option we got is > 0 to avoid limiting the listener to every animation frame
401406
if (this.throttle) {
402-
this.keyDownEventListener = lodashThrottle(this.keyDownEventListener.bind(this), this.throttle);
407+
this.keyDownEventListener =
408+
lodashThrottle(this.keyDownEventListener.bind(this), this.throttle, THROTTLE_OPTIONS);
403409

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();
410+
// When throttling then make sure to only throttle key down and cancel any queued functions in case of key up
411+
this.keyUpEventListener = () => this.keyDownEventListener.cancel();
406412

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

0 commit comments

Comments
 (0)