Skip to content

Commit

Permalink
Fix issue #45 - Added setThrottle (#55)
Browse files Browse the repository at this point in the history
* Added setThrottle

* Updated readme
  • Loading branch information
guilleccc authored Dec 20, 2022
1 parent 6a7e164 commit 32dc88d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# [1.1.5]
## Added
- Add `setThrottle` to dynamically change throttle time.

## Changed
- Remove event listeners for `unbindEventHandlers` regardless of throttle value

# [1.1.4]
## Changed
- Update `parentFocusKey` when `removeFocusable`
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ setKeyMap({
});
```

### `setThrottle`
A method for dynamically updating `throttle` and `throttleKeypresses` values. This might be useful if you want to throttle listeners under specific sections or pages.

```jsx
setThrottle({
throttle: 500,
throttleKeypresses: true
});
```

### `destroy`
Resets all the settings and the storage of focusable components. Disables the navigation service.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noriginmedia/norigin-spatial-navigation",
"version": "1.1.4",
"version": "1.1.5",
"description": "React hooks based Spatial Navigation solution",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
32 changes: 25 additions & 7 deletions src/SpatialNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ class SpatialNavigationService {
this.updateAllLayouts = this.updateAllLayouts.bind(this);
this.navigateByDirection = this.navigateByDirection.bind(this);
this.init = this.init.bind(this);
this.setThrottle = this.setThrottle.bind(this);
this.destroy = this.destroy.bind(this);
this.setKeyMap = this.setKeyMap.bind(this);
this.getCurrentFocusKey = this.getCurrentFocusKey.bind(this);
Expand Down Expand Up @@ -577,6 +578,21 @@ class SpatialNavigationService {
}
}

setThrottle({
throttle: throttleParam = 0,
throttleKeypresses = false
} = {}) {
this.throttleKeypresses = throttleKeypresses;

if (!this.nativeMode) {
this.unbindEventHandlers();
if (Number.isInteger(throttleParam)) {
this.throttle = throttleParam;
}
this.bindEventHandlers();
}
}

startDrawLayouts() {
const draw = () => {
requestAnimationFrame(() => {
Expand Down Expand Up @@ -701,13 +717,15 @@ class SpatialNavigationService {
unbindEventHandlers() {
// We check both because the React Native remote debugger implements window, but not window.removeEventListener.
if (typeof window !== 'undefined' && window.removeEventListener) {
window.removeEventListener('keydown', this.keyDownEventListener);
this.keyDownEventListener = null;
window.removeEventListener('keyup', this.keyUpEventListener);
this.keyUpEventListener = null;

if (this.throttle) {
window.removeEventListener('keyup', this.keyUpEventListener);
this.keyUpEventListener = null;
}
const listener = this.throttle
? this.keyDownEventListenerThrottled
: this.keyDownEventListener;

window.removeEventListener('keydown', listener);
this.keyDownEventListener = null;
}
}

Expand Down Expand Up @@ -1438,4 +1456,4 @@ class SpatialNavigationService {
/** @internal */
export const SpatialNavigation = new SpatialNavigationService();

export const { init, destroy, setKeyMap } = SpatialNavigation;
export const { init, setThrottle, destroy, setKeyMap } = SpatialNavigation;

0 comments on commit 32dc88d

Please sign in to comment.