Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
build
coverage
.DS_Store
.DS_Store
.idea
7 changes: 5 additions & 2 deletions src/function/__tests__/throttleEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ describe('utils/function/throttleEnd', () => {
throttled.call({ a: 1 }, 1, 2);
throttled.call({ a: 2 }, 3, 4);
throttled.call({ a: 3 }, 5, 6);

expect(f.mock.calls).toHaveLength(0);
jest.runAllTimers();
expect(f.mock.calls).toHaveLength(1);
expect(f).toBeCalledWith(1, 2);
expect(context).toEqual({ a: 1 });
expect(f).toBeCalledWith(5, 6);
expect(context).toEqual({ a: 3 });

throttled.call({ a: 4 }, 7);

expect(f.mock.calls).toHaveLength(1);
jest.runAllTimers();
expect(f.mock.calls).toHaveLength(2);
Expand Down
6 changes: 5 additions & 1 deletion src/function/throttleEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import curryN from './curryN';
*/
export default curryN(2, (wait, fn) => {
let lastCalled;
let lastArgs;
let lastThis;
let timeout;

return function(...args) {
Expand All @@ -21,11 +23,13 @@ export default curryN(2, (wait, fn) => {
fn.apply(this, args);
} else if (!timeout) {
timeout = setTimeout(() => {
fn.apply(this, args);
fn.apply(lastThis, lastArgs);
timeout = null;
}, wait);
}

lastCalled = now;
lastArgs = args;
lastThis = this;
};
});