8000 Simplify `_.debounce` and `_.throttle` for the `underscore` build. · lodash/lodash@c3ac172 · GitHub
[go: up one dir, main page]

Skip to content

Commit c3ac172

Browse files
committed
Simplify _.debounce and _.throttle for the underscore build.
Former-commit-id: 56dc526cf71130084651d06b8caadce573471131
1 parent 562b4eb commit c3ac172

File tree

3 files changed

+104
-60
lines changed

3 files changed

+104
-60
lines changed

build.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,36 @@
20592059
'}'
20602060
].join('\n'));
20612061

2062+
// replace `_.debounce`
2063+
source = replaceFunction(source, 'debounce', [
2064+
'function debounce(func, wait, immediate) {',
2065+
' var args,',
2066+
' result,',
2067+
' thisArg,',
2068+
' timeoutId;',
2069+
'',
2070+
' function delayed() {',
2071+
' timeoutId = null;',
2072+
' if (!immediate) {',
2073+
' result = func.apply(thisArg, args);',
2074+
' }',
2075+
' }',
2076+
' return function() {',
2077+
' var isImmediate = immediate && !timeoutId;',
2078+
' args = arguments;',
2079+
' thisArg = this;',
2080+
'',
2081+
' clearTimeout(timeoutId);',
2082+
' timeoutId = setTimeout(delayed, wait);',
2083+
'',
2084+
' if (isImmediate) {',
2085+
' result = func.apply(thisArg, args);',
2086+
' }',
2087+
' return result;',
2088+
' };',
2089+
'}'
2090+
].join('\n'));
2091+
20622092
// replace `_.defaults`
20632093
source = replaceFunction(source, 'defaults', [
20642094
'function defaults(object) {',
@@ -2374,6 +2404,41 @@
23742404
'}'
23752405
].join('\n'));
23762406

2407+
// replace `_.throttle`
2408+
source = replaceFunction(source, 'throttle', [
2409+
'function throttle(func, wait) {',
2410+
' var args,',
2411+
' result,',
2412+
' thisArg,',
2413+
' timeoutId,',
2414+
' lastCalled = 0;',
2415+
'',
2416+
' function trailingCall() {',
2417+
' lastCalled = new Date;',
2418+
' timeoutId = null;',
2419+
' result = func.apply(thisArg, args);',
2420+
' }',
2421+
' return function() {',
2422+
' var now = new Date,',
2423+
' remaining = wait - (now - lastCalled);',
2424+
'',
2425+
' args = arguments;',
2426+
' thisArg = this;',
2427+
'',
2428+
' if (remaining <= 0) {',
2429+
' clearTimeout(timeoutId);',
2430+
' timeoutId = null;',
2431+
' lastCalled = now;',
2432+
' result = func.apply(thisArg, args);',
2433+
' }',
2434+
' else if (!timeoutId) {',
2435+
' timeoutId = setTimeout(trailingCall, remaining);',
2436+
' }',
2437+
' return result;',
2438+
' };',
2439+
'}'
2440+
].join('\n'));
2441+
23772442
// replace `_.times`
23782443
source = replaceFunction(source, 'times', [
23792444
'function times(n, callback, thisArg) {',

dist/lodash.underscore.js

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3482,35 +3482,27 @@
34823482
* var lazyLayout = _.debounce(calculateLayout, 300);
34833483
* jQuery(window).on('resize', lazyLayout);
34843484
*/
3485-
function debounce(func, wait, options) {
3485+
function debounce(func, wait, immediate) {
34863486
var args,
34873487
result,
34883488
thisArg,
3489-
timeoutId,
3490-
trailing = true;
3489+
timeoutId;
34913490

34923491
function delayed() {
34933492
timeoutId = null;
3494-
if (trailing) {
3493+
if (!immediate) {
34953494
result = func.apply(thisArg, args);
34963495
}
34973496
}
3498-
if (options === true) {
3499-
var leading = true;
3500-
trailing = false;
3501-
} else if (options && objectTypes[typeof options]) {
3502-
leading = options.leading;
3503-
trailing = 'trailing' in options ? options.trailing : trailing;
3504-
}
35053497
return function() {
3506-
var isLeading = leading && !timeoutId;
3498+
var isImmediate = immediate && !timeoutId;
35073499
args = arguments;
35083500
thisArg = this;
3509350 77F4 1

35103502
clearTimeout(timeoutId);
35113503
timeoutId = setTimeout(delayed, wait);
35123504

3513-
if (isLeading) {
3505+
if (isImmediate) {
35143506
result = func.apply(thisArg, args);
35153507
}
35163508
return result;
@@ -3667,35 +3659,22 @@
36673659
* var throttled = _.throttle(updatePosition, 100);
36683660
* jQuery(window).on('scroll', throttled);
36693661
*/
3670-
function throttle(func, wait, options) {
3662+
function throttle(func, wait) {
36713663
var args,
36723664
result,
36733665
thisArg,
36743666
timeoutId,
3675-
lastCalled = 0,
3676-
leading = true,
3677-
trailing = true;
3667+
lastCalled = 0;
36783668

36793669
function trailingCall() {
36803670
lastCalled = new Date;
36813671
timeoutId = null;
3682-
3683-
if (trailing) {
3684-
result = func.apply(thisArg, args);
3685-
}
3686-
}
3687-
if (options === false) {
3688-
leading = false;
3689-
} else if (options && objectTypes[typeof options]) {
3690-
leading = 'leading' in options ? options.leading : leading;
3691-
trailing = 'trailing' in options ? options.trailing : trailing;
3672+
result = func.apply(thisArg, args);
36923673
}
36933674
return function() {
3694-
var now = new Date;
3695-
if (!timeoutId && !leading) {
3696-
lastCalled = now;
3697-
}
3698-
var remaining = wait - (now - lastCalled);
3675+ var now = new Date,
3676+
remaining = wait - (now - lastCalled);
3677+
36993678
args = arguments;
37003679
thisArg = this;
37013680

0 commit comments

Comments
 (0)
0