-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·151 lines (125 loc) · 3.74 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
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
/*!
*
* https://github.com/awesomehimanshu/nodeloops
*
* Author:- Himanshu sharma
*
*/
(function () {
var nodeloops = {};
function noop() {}
// global on the server, window in the browser
var root = typeof self === 'object' && self.self === self && self ||
typeof global === 'object' && global.global === global && global ||
this;
if (root != null) {
previous_async = root.async;
}
//
function only_once(fn) {
return function() {
if (fn === null) throw new Error("Callback was already called.");
fn.apply(this, arguments);
fn = null;
};
}
function _once(fn) {
return function() {
if (fn === null) return;
fn.apply(this, arguments);
fn = null;
};
}
//// cross-browser compatiblity functions ////
function _restParam(func, startIndex) {
startIndex = startIndex == null ? func.length - 1 : +startIndex;
return function() {
var length = Math.max(arguments.length - startIndex, 0);
var rest = Array(length);
for (var index = 0; index < length; index++) {
rest[index] = arguments[index + startIndex];
}
switch (startIndex) {
case 0: return func.call(this, rest);
case 1: return func.call(this, arguments[0], rest);
}
};
}
var _setImmediate = typeof setImmediate === 'function' && setImmediate;
var _delay = _setImmediate ? function(fn) {
_setImmediate(fn);
} : function(fn) {
setTimeout(fn, 0);
};
if (typeof process === 'object' && typeof process.nextTick === 'function') {
nodeloops.nextTick = process.nextTick;
} else {
nodeloops.nextTick = _delay;
}
nodeloops.setImmediate = _setImmediate ? _delay : nodeloops.nextTick;
nodeloops.fastforloops = function (fn, callback) {
var done = only_once(callback || noop);
var task = ensureloops(fn);
function next(err) {
if (err) {
return done(err);
}
task(next);
}
next();
};
nodeloops.mediumforloops = function(start, stop,microsecond, callback, done) {
var task, iterator;
var current = start;
iterator = function() {
if (current >= stop) {
clearInterval(task);
if (done) {
done();
}
} else {
callback(current++);
}
}
task = setInterval(iterator, microsecond);
};
function ensureloops(fn) {
return _restParam(function (args) {
var callback = args.pop();
args.push(function () {
var innerArgs = arguments;
if (sync) {
nodeloops.setImmediate(function () {
callback.apply(null, innerArgs);
});
} else {
callback.apply(null, innerArgs);
}
});
var sync = true;
fn.apply(this, args);
sync = false;
});
}
nodeloops.ensureloops = ensureloops;
nodeloops.constant = _restParam(function(values) {
var args = [null].concat(values);
return function (callback) {
return callback.apply(this, args);
};
});
// Node.js
if (typeof module === 'object' && module.exports) {
module.exports = nodeloops;
}
// AMD / RequireJS
/* else if (typeof define === 'function' && define.amd) {
define([], function () {
return nodeloops;
});
}
// included directly via <script> tag
else {
root.nodeloops = nodeloops;
}*/
}());