Make doubley sure Date.now, clearTimeout, and setTimeout are mockable.

This commit is contained in:
John-David Dalton
2016-07-29 20:47:16 -07:00
parent 0bfa1ec2d0
commit 1468c74c36

View File

@@ -1282,7 +1282,6 @@
/** Built-in constructor references. */
var Array = context.Array,
Date = context.Date,
Error = context.Error,
Math = context.Math,
RegExp = context.RegExp,
@@ -1342,9 +1341,10 @@
splice = arrayProto.splice,
spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined;
/** Built-in method references that are mockable. */
var clearTimeout = function(id) { return context.clearTimeout.call(root, id); },
setTimeout = function(func, wait) { return context.setTimeout.call(root, func, wait); };
/** Mocked built-ins. */
var ctxClearTimeout = context.clearTimeout !== root.clearTimeout && context.clearTimeout,
ctxNow = context.Date && context.Date.now !== root.Date.now && context.Date.now,
ctxSetTimeout = context.setTimeout !== root.setTimeout && context.setTimeout;
/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeCeil = Math.ceil,
@@ -4157,6 +4157,16 @@
return (!start && end >= length) ? array : baseSlice(array, start, end);
}
/**
* A simple wrapper around the global [`clearTimeout`](https://mdn.io/clearTimeout).
*
* @private
* @param {number} id The id of the timer to clear.
*/
var clearTimeout = ctxClearTimeout || function(id) {
return root.clearTimeout(id);
};
/**
* Creates a clone of `buffer`.
*
@@ -6187,6 +6197,18 @@
};
}());
/**
* A simple wrapper around the global [`setTimeout`](https://mdn.io/setTimeout).
*
* @private
* @param {Function} func The function to delay.
* @param {number} wait The number of milliseconds to delay invocation.
* @returns {number} Returns the timer id.
*/
var setTimeout = ctxSetTimeout || function(func, wait) {
return root.setTimeout(func, wait);
};
/**
* Sets the `toString` method of `wrapper` to mimic the source of `reference`
* with wrapper details in a comment at the top of the source body.
@@ -9487,9 +9509,9 @@
* }, _.now());
* // => Logs the number of milliseconds it took for the deferred invocation.
*/
function now() {
return Date.now();
}
var now = ctxNow || function() {
return root.Date.now();
};
/*------------------------------------------------------------------------*/