Skip to content

Commit

Permalink
fix throttle params + 6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinm committed Jan 14, 2024
1 parent 01c83cd commit 79b46b4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 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
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 79b46b4

Please sign in to comment.