-
Notifications
You must be signed in to change notification settings - Fork 17
/
enqueue.js
59 lines (49 loc) · 1.18 KB
/
enqueue.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
(function (define) {
define(function () {
"use strict";
var enqueue;
if (typeof process !== "undefined") {
// node
enqueue = process.nextTick;
} else if (typeof msSetImmediate === "function") {
// IE 10. From http://github.com/kriskowal/q
// bind is necessary
enqueue = msSetImmediate.bind(window);
} else if (typeof setImmediate === "function") {
enqueue = setImmediate;
} else if (typeof MessageChannel !== "undefined") {
enqueue = initMessageChannel();
} else {
// older envs w/only setTimeout
enqueue = function (task) {
setTimeout(task, 0);
};
}
return enqueue;
/**
* MessageChannel for browsers that support it
* From http://www.nonblocking.io/2011/06/windownexttick.html
*/
function initMessageChannel() {
var channel, head, tail;
channel = new MessageChannel();
head = {};
tail = head;
channel.port1.onmessage = function () {
var task;
head = head.next;
task = head.task;
delete head.task;
task();
};
return function (task) {
tail = tail.next = {task: task};
channel.port2.postMessage(0);
};
}
});
}(
typeof define == 'function' && define.amd
? define
: function (factory) { module.exports = factory(); }
));