-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esl-utils): aggregate decorator function introduced
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Aggregate is the function decorator similar to {@link debounce} which is collect calls until the `time` from | ||
* the last call is exceeded and them coll the `callback` with the list of happened original calls arguments. | ||
*/ | ||
export function aggregate<T>(callback: (this: void, args: T[]) => void, time: number) { | ||
let calls: T[] = []; | ||
let timeout = 0; | ||
|
||
const emit = () => { | ||
callback(calls); | ||
calls = []; | ||
timeout = 0; | ||
}; | ||
|
||
return function (arg: T) { | ||
calls.push(arg); | ||
timeout = timeout || window.setTimeout(emit, time); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {aggregate} from '../aggregate'; | ||
|
||
describe('async/aggregate', () => { | ||
test('single call', (done) => { | ||
const fn = jest.fn(); | ||
const agFn = aggregate(fn, 10); | ||
|
||
agFn(1); | ||
|
||
setTimeout(() => { | ||
expect(fn).toBeCalledTimes(1); | ||
expect(fn).lastCalledWith([1]); | ||
|
||
agFn(2); | ||
setTimeout(() => { | ||
expect(fn).toBeCalledTimes(2); | ||
expect(fn).lastCalledWith([2]); | ||
|
||
done(); | ||
}, 40); | ||
}, 40); | ||
}, 200); | ||
test('multiple calls', (done) => { | ||
const fn = jest.fn(); | ||
const agFn = aggregate(fn, 10); | ||
|
||
agFn(1); | ||
agFn(2); | ||
|
||
setTimeout(() => agFn(3)); | ||
setTimeout(() => agFn(4), 5); | ||
|
||
expect(fn).toBeCalledTimes(0); | ||
|
||
setTimeout(() => { | ||
expect(fn).toBeCalledTimes(1); | ||
expect(fn).lastCalledWith([1, 2, 3, 4]); | ||
done(); | ||
}, 100); | ||
}, 200); | ||
}); |