Skip to content

Commit 32dc88d

Browse files
authored
Fix issue #45 - Added setThrottle (#55)
* Added setThrottle * Updated readme
1 parent 6a7e164 commit 32dc88d

4 files changed

Lines changed: 43 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
# [1.1.5]
8+
## Added
9+
- Add `setThrottle` to dynamically change throttle time.
10+
11+
## Changed
12+
- Remove event listeners for `unbindEventHandlers` regardless of throttle value
13+
714
# [1.1.4]
815
## Changed
916
- Update `parentFocusKey` when `removeFocusable`

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ setKeyMap({
265265
});
266266
```
267267

268+
### `setThrottle`
269+
A method for dynamically updating `throttle` and `throttleKeypresses` values. This might be useful if you want to throttle listeners under specific sections or pages.
270+
271+
```jsx
272+
setThrottle({
273+
throttle: 500,
274+
throttleKeypresses: true
275+
});
276+
```
277+
268278
### `destroy`
269279
Resets all the settings and the storage of focusable components. Disables the navigation service.
270280

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@noriginmedia/norigin-spatial-navigation",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "React hooks based Spatial Navigation solution",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/SpatialNavigation.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ class SpatialNavigationService {
540540
this.updateAllLayouts = this.updateAllLayouts.bind(this);
541541
this.navigateByDirection = this.navigateByDirection.bind(this);
542542
this.init = this.init.bind(this);
543+
this.setThrottle = this.setThrottle.bind(this);
543544
this.destroy = this.destroy.bind(this);
544545
this.setKeyMap = this.setKeyMap.bind(this);
545546
this.getCurrentFocusKey = this.getCurrentFocusKey.bind(this);
@@ -577,6 +578,21 @@ class SpatialNavigationService {
577578
}
578579
}
579580

581+
setThrottle({
582+
throttle: throttleParam = 0,
583+
throttleKeypresses = false
584+
} = {}) {
585+
this.throttleKeypresses = throttleKeypresses;
586+
587+
if (!this.nativeMode) {
588+
this.unbindEventHandlers();
589+
if (Number.isInteger(throttleParam)) {
590+
this.throttle = throttleParam;
591+
}
592+
this.bindEventHandlers();
593+
}
594+
}
595+
580596
startDrawLayouts() {
581597
const draw = () => {
582598
requestAnimationFrame(() => {
@@ -701,13 +717,15 @@ class SpatialNavigationService {
701717
unbindEventHandlers() {
702718
// We check both because the React Native remote debugger implements window, but not window.removeEventListener.
703719
if (typeof window !== 'undefined' && window.removeEventListener) {
704-
window.removeEventListener('keydown', this.keyDownEventListener);
705-
this.keyDownEventListener = null;
720+
window.removeEventListener('keyup', this.keyUpEventListener);
721+
this.keyUpEventListener = null;
706722

707-
if (this.throttle) {
708-
window.removeEventListener('keyup', this.keyUpEventListener);
709-
this.keyUpEventListener = null;
710-
}
723+
const listener = this.throttle
724+
? this.keyDownEventListenerThrottled
725+
: this.keyDownEventListener;
726+
727+
window.removeEventListener('keydown', listener);
728+
this.keyDownEventListener = null;
711729
}
712730
}
713731

@@ -1438,4 +1456,4 @@ class SpatialNavigationService {
14381456
/** @internal */
14391457
export const SpatialNavigation = new SpatialNavigationService();
14401458

1441-
export const { init, destroy, setKeyMap } = SpatialNavigation;
1459+
export const { init, setThrottle, destroy, setKeyMap } = SpatialNavigation;

0 commit comments

Comments
 (0)