-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnexttick.js
More file actions
93 lines (84 loc) · 2.18 KB
/
nexttick.js
File metadata and controls
93 lines (84 loc) · 2.18 KB
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
//
// nextTick
// by stagas
//
// Common functions using process.nextTick()
//
var slice = [].slice
exports = module.exports = function () {
var args = slice.call(arguments)
, fn = args.shift()
, then = null
process.nextTick(function () {
fn.apply(this, args)
then && then()
})
return { then: function (fn) { then = fn } }
}
exports.nextTick = exports.tick = module.exports
function wrapper (loop) {
var context = {}
var then = function () {}
var args = []
loop = loop.bind(context)
context.then = function () {
then.apply(this, args)
}
context.exit = false
context.exitfn = function () {
this.exit = true
args = slice.call(arguments)
}.bind(context)
context.loop = function () {
process.nextTick(loop)
return true
}
context.loop()
return { then: function (fn) { then = fn } }
}
exports.loop = function (fn, times) {
return wrapper(function () {
fn(this.exitfn)
'undefined' !== typeof times
? --times && !this.exit && this.loop() || this.then()
: !this.exit && this.loop() || this.then()
})
}
exports.while = function (truth, fn) {
return wrapper(function () {
fn(this.exitfn)
truth() && !this.exit && this.loop() || this.then()
})
}
exports.forEach = function (array, fn) {
var length = array.length
, index = 0
return wrapper(function () {
fn(array[index], index++, array, this.exitfn)
index < length && !this.exit && this.loop() || this.then()
})
}
exports.slice = function (array, begin, end) {
var length = array.length
begin = 'undefined' === typeof begin ? 0 : begin < 0 ? length + begin : begin
begin = begin < 0 ? 0 : begin > length ? length : begin
end = 'undefined' === typeof end ? length : end < 0 ? length + end : end
end = end < begin ? begin : end > length ? length : end
var sliced = []
return exports.loop(function (exit) {
if (begin < end) {
sliced.push(array[begin++])
} else {
exit(sliced)
}
})
}
exports.in = function (hash, fn) {
var keys = Object.keys(hash)
, key
return wrapper(function () {
key = keys.shift()
fn(hash[key], key, hash, this.exitfn)
keys.length && !this.exit && this.loop() || this.then()
})
}