-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (45 loc) · 1.13 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
/*!
* Breeze - process.nextTick browser shim
* Copyright(c) 2012 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/
/**
* ### .nextTick (fn)
*
* Cross-compatible `nextTick` implementation. Uses
* `process.nextTick` for node and `setTimeout(fn, 0)`
* for the browser.
*
* @param {Function} callback
* @name nextTick
* @api public
*/
module.exports = 'function' === typeof setImmediate
? setImmediate
: browserNextTick();
/*!
* Prepares a cross-browser capable nextTick implementation
* using either `postMessage` or `setTimeout(0)`.
*
* @attr http://dbaron.org/log/20100309-faster-timeouts
* @api private
*/
function browserNextTick () {
if (!window || !window.postMessage || window.ActiveXObject) {
return function (fn) {
setTimeout(fn, 0);
}
}
var timeouts = []
, name = 'breeze-zero-timeout';
window.addEventListener('message', function (e) {
if (e.source === window && e.data === name) {
if (e.stopPropagation) e.stopPropagation();
if (timeouts.length) timeouts.shift()();
}
});
return function (fn) {
timeouts.push(fn);
window.postMessage(name, '*');
}
}