Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the throttle function and upgrade to v6.1 #1409

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading