forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebounce-and-throttle.js
167 lines (134 loc) · 4.37 KB
/
debounce-and-throttle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import assert from 'assert';
import lodashStable from 'lodash';
import { _, noop, push, isModularize } from './utils.js';
import runInContext from '../runInContext.js';
describe('debounce and throttle', function() {
lodashStable.each(['debounce', 'throttle'], function(methodName) {
var func = _[methodName],
isDebounce = methodName == 'debounce';
it('`_.' + methodName + '` should not error for non-object `options` values', function() {
func(noop, 32, 1);
assert.ok(true);
});
it('`_.' + methodName + '` should use a default `wait` of `0`', function(done) {
var callCount = 0,
funced = func(function() { callCount++; });
funced();
setTimeout(function() {
funced();
assert.strictEqual(callCount, isDebounce ? 1 : 2);
done();
}, 32);
});
it('`_.' + methodName + '` should invoke `func` with the correct `this` binding', function(done) {
var actual = [],
object = { 'funced': func(function() { actual.push(this); }, 32) },
expected = lodashStable.times(isDebounce ? 1 : 2, lodashStable.constant(object));
object.funced();
if (!isDebounce) {
object.funced();
}
setTimeout(function() {
assert.deepStrictEqual(actual, expected);
done();
}, 64);
});
it('`_.' + methodName + '` supports recursive calls', function(done) {
var actual = [],
args = lodashStable.map(['a', 'b', 'c'], function(chr) { return [{}, chr]; }),
expected = args.slice(),
queue = args.slice();
var funced = func(function() {
var current = [this];
push.apply(current, arguments);
actual.push(current);
var next = queue.shift();
if (next) {
funced.call(next[0], next[1]);
}
}, 32);
var next = queue.shift();
funced.call(next[0], next[1]);
assert.deepStrictEqual(actual, expected.slice(0, isDebounce ? 0 : 1));
setTimeout(function() {
assert.deepStrictEqual(actual, expected.slice(0, actual.length));
done();
}, 256);
});
it('`_.' + methodName + '` should work if the system time is set backwards', function(done) {
if (!isModularize) {
var callCount = 0,
dateCount = 0;
var lodash = runInContext({
'Date': {
'now': function() {
return ++dateCount == 4
? +new Date(2012, 3, 23, 23, 27, 18)
: +new Date;
}
}
});
var funced = lodash[methodName](function() {
callCount++;
}, 32);
funced();
setTimeout(function() {
funced();
assert.strictEqual(callCount, isDebounce ? 1 : 2);
done();
}, 64);
}
else {
done();
}
});
it('`_.' + methodName + '` should support cancelling delayed calls', function(done) {
var callCount = 0;
var funced = func(function() {
callCount++;
}, 32, { 'leading': false });
funced();
funced.cancel();
setTimeout(function() {
assert.strictEqual(callCount, 0);
done();
}, 64);
});
it('`_.' + methodName + '` should reset `lastCalled` after cancelling', function(done) {
var callCount = 0;
var funced = func(function() {
return ++callCount;
}, 32, { 'leading': true });
assert.strictEqual(funced(), 1);
funced.cancel();
assert.strictEqual(funced(), 2);
funced();
setTimeout(function() {
assert.strictEqual(callCount, 3);
done();
}, 64);
});
it('`_.' + methodName + '` should support flushing delayed calls', function(done) {
var callCount = 0;
var funced = func(function() {
return ++callCount;
}, 32, { 'leading': false });
funced();
assert.strictEqual(funced.flush(), 1);
setTimeout(function() {
assert.strictEqual(callCount, 1);
done();
}, 64);
});
it('`_.' + methodName + '` should noop `cancel` and `flush` when nothing is queued', function(done) {
var callCount = 0,
funced = func(function() { callCount++; }, 32);
funced.cancel();
assert.strictEqual(funced.flush(), undefined);
setTimeout(function() {
assert.strictEqual(callCount, 0);
done();
}, 64);
});
});
});