Skip to content

Commit

Permalink
Merge pull request #1409 from grid-js/v6.1
Browse files Browse the repository at this point in the history
Fix the throttle function and upgrade to v6.1
  • Loading branch information
afshinm authored Jan 14, 2024
2 parents 4219ea6 + 79b46b4 commit a221513
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions 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": "gridjs",
"version": "6.0.6",
"version": "6.1.0",
"description": "Advanced table plugin",
"author": "Afshin Mehrabani <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function useSelector<T>(selector: (state) => T) {
});

return unsubscribe;
}, [store, current]);
}, []);

return current;
}
4 changes: 2 additions & 2 deletions src/util/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export const throttle = (fn: (...args) => void, wait = 100) => {

if (elapsed >= wait) {
// If enough time has passed since the last call, execute the function immediately
execute(args);
execute(...args);
} else {
// If not enough time has passed, schedule the function call after the remaining delay
if (timeoutId) {
clearTimeout(timeoutId);
}

timeoutId = setTimeout(() => {
execute(args);
execute(...args);
timeoutId = null;
}, wait - elapsed);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/jest/util/throttle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ describe('throttle', () => {
const fn = jest.fn();
const throttled = throttle(fn, wait);

throttled('a');
throttled('a', 'b', 'c');
sleep(wait - 5);
throttled('b');
throttled('b', 'a', 'c');
sleep(wait - 10);
throttled('c');
throttled('c', 'b', 'a');

await sleep(wait);

expect(fn).toBeCalledTimes(1);
expect(fn).toBeCalledWith(['c']);
expect(fn).toBeCalledWith('c', 'b', 'a');
});

it('should execute the first call', async () => {
Expand Down

0 comments on commit a221513

Please sign in to comment.