Skip to content

Commit 9d2f467

Browse files
committed
Fix throttle() bug
- now trailing invoke has additional parameter properly.
1 parent 9bbd441 commit 9d2f467

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/tricks.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
delay = delay || 0;
5151

5252
function debounced() {
53-
args = arguments;
53+
args = aps.call(arguments);
5454

5555
window.clearTimeout(timer);
5656
timer = window.setTimeout(function() {
@@ -129,7 +129,12 @@
129129

130130
base = base || stamp;
131131

132-
debounced();
132+
// pass array directly because `debounce()`, `tick()` are already use
133+
// `apply()` method to invoke developer's `fn` handler.
134+
//
135+
// also, this `debounced` line invoked every time for implements
136+
// `trailing` features.
137+
debounced(args);
133138

134139
if ((stamp - base) >= interval) {
135140
tick(args);

test/tricks.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ describe('tricks', function() {
8181
expect(tui.util.debounce).toHaveBeenCalled();
8282
});
8383

84+
it('debounced method must invoke with additional parameter', function() {
85+
var magazine = [1, 3, 6, 8, 9],
86+
fireGun = reload(magazine),
87+
fn;
88+
89+
spyOn(tui.util, 'timestamp').and.callFake(function() {
90+
return fireGun();
91+
});
92+
93+
var debounced = jasmine.createSpy('debounced');
94+
spyOn(tui.util, 'debounce').and.returnValue(debounced);
95+
96+
fn = tui.util.throttle(spy, 7);
97+
98+
fn('hello');
99+
fn('hello');
100+
fn('hello');
101+
fn('hello');
102+
fn('hello');
103+
104+
expect(spy.calls.count()).toBe(2);
105+
expect(tui.util.debounce).toHaveBeenCalled();
106+
107+
expect(debounced.calls.count()).toBe(4);
108+
// debounce가 이미 콜백을 apply 처리하고 있는데, Mock을 했기 때문에
109+
// args 를 그냥 넘겨준다 따라서 TC에서만은 각 debounce의 파라미터가 배열 형태임.
110+
expect(debounced.calls.allArgs()).toEqual([
111+
[['hello']],
112+
[['hello']],
113+
[['hello']],
114+
[['hello']]
115+
]);
116+
});
117+
84118
it('reset can remove slugs related with throttling.', function() {
85119
var magazine = [1, 3, 6, 8, 9],
86120
fireGun = reload(magazine),

0 commit comments

Comments
 (0)