-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
48 lines (41 loc) · 1.27 KB
/
index.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
'use strict';
/**
* Delay function calls only if they are not already ran async.
*
* @param {Function} fn Function that should be forced in async execution
* @returns {Function} A wrapped function that will called the supplied callback.
* @api public
*/
module.exports = function hang(fn) {
var start = +(new Date());
/**
* The wrapped function.
*
* @api private
*/
function bro() {
var self = this;
//
// Time has passed since we've generated this function so we're going to
// assume that this function is already executed async.
//
if (+(new Date()) > start) {
return fn.apply(self, arguments);
}
for (var i = 0, l = arguments.length, args = new Array(l); i < l; i++) {
args[i] = arguments[i];
}
(global.setImmediate || global.setTimeout)(function delay() {
fn.apply(self, args);
self = args = null;
}, 0);
}
//
// To make debugging more easy we want to use the name of the supplied
// function. So when you look at the functions that are assigned to event
// listeners you don't see a load of `onetime` functions but actually the
// names of the functions that this module will call.
//
bro.displayName = fn.displayName || fn.name || bro.displayName || bro.name;
return bro;
};