-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnextJob.mjs
More file actions
59 lines (56 loc) · 1.3 KB
/
nextJob.mjs
File metadata and controls
59 lines (56 loc) · 1.3 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
var callbacks = []
var pending = false
function nextJobHandler () {
pending = false
var length = callbacks.length
if (length === 0) {
return
}
var cbs = callbacks.splice(0)
for (var i = 0; i < length; i++) {
cbs[i]()
}
}
var callNextJob
if (typeof process === 'object'
&& process !== null
&& typeof process['nextTick'] === 'function'
) {
callNextJob = process.nextTick
} else if (typeof MutationObserver !== 'undefined') {
var a = 'a'
var b = 'b'
var value = a
var commentNode = document.createComment(value)
var observer = new MutationObserver(nextJobHandler)
observer.observe(commentNode, {
characterData: true
})
callNextJob = function () {
value = value === a ? b : a
commentNode.data = value
}
} else {
callNextJob = function (callback) {
setTimeout(callback, 0)
}
}
export default function nextJob (callback, context) {
var cb = callback
if (context) {
var args = []
var l = arguments.length
while (--l > 1) {
args.unshift(arguments[l])
}
cb = function () {
callback.apply(context, args)
}
}
callbacks.push(cb)
if (pending) {
return
}
pending = true
callNextJob(nextJobHandler)
}