UNPKG

2.95 kBJavaScriptView Raw
1var global = require('../internals/global');
2var fails = require('../internals/fails');
3var classof = require('../internals/classof-raw');
4var bind = require('../internals/bind-context');
5var html = require('../internals/html');
6var createElement = require('../internals/document-create-element');
7var userAgent = require('../internals/user-agent');
8
9var location = global.location;
10var set = global.setImmediate;
11var clear = global.clearImmediate;
12var process = global.process;
13var MessageChannel = global.MessageChannel;
14var Dispatch = global.Dispatch;
15var counter = 0;
16var queue = {};
17var ONREADYSTATECHANGE = 'onreadystatechange';
18var defer, channel, port;
19
20var run = function (id) {
21 // eslint-disable-next-line no-prototype-builtins
22 if (queue.hasOwnProperty(id)) {
23 var fn = queue[id];
24 delete queue[id];
25 fn();
26 }
27};
28
29var runner = function (id) {
30 return function () {
31 run(id);
32 };
33};
34
35var listener = function (event) {
36 run(event.data);
37};
38
39var post = function (id) {
40 // old engines have not location.origin
41 global.postMessage(id + '', location.protocol + '//' + location.host);
42};
43
44// Node.js 0.9+ & IE10+ has setImmediate, otherwise:
45if (!set || !clear) {
46 set = function setImmediate(fn) {
47 var args = [];
48 var i = 1;
49 while (arguments.length > i) args.push(arguments[i++]);
50 queue[++counter] = function () {
51 // eslint-disable-next-line no-new-func
52 (typeof fn == 'function' ? fn : Function(fn)).apply(undefined, args);
53 };
54 defer(counter);
55 return counter;
56 };
57 clear = function clearImmediate(id) {
58 delete queue[id];
59 };
60 // Node.js 0.8-
61 if (classof(process) == 'process') {
62 defer = function (id) {
63 process.nextTick(runner(id));
64 };
65 // Sphere (JS game engine) Dispatch API
66 } else if (Dispatch && Dispatch.now) {
67 defer = function (id) {
68 Dispatch.now(runner(id));
69 };
70 // Browsers with MessageChannel, includes WebWorkers
71 // except iOS - https://github.com/zloirock/core-js/issues/624
72 } else if (MessageChannel && !/(iphone|ipod|ipad).*applewebkit/i.test(userAgent)) {
73 channel = new MessageChannel();
74 port = channel.port2;
75 channel.port1.onmessage = listener;
76 defer = bind(port.postMessage, port, 1);
77 // Browsers with postMessage, skip WebWorkers
78 // IE8 has postMessage, but it's sync & typeof its postMessage is 'object'
79 } else if (global.addEventListener && typeof postMessage == 'function' && !global.importScripts && !fails(post)) {
80 defer = post;
81 global.addEventListener('message', listener, false);
82 // IE8-
83 } else if (ONREADYSTATECHANGE in createElement('script')) {
84 defer = function (id) {
85 html.appendChild(createElement('script'))[ONREADYSTATECHANGE] = function () {
86 html.removeChild(this);
87 run(id);
88 };
89 };
90 // Rest old browsers
91 } else {
92 defer = function (id) {
93 setTimeout(runner(id), 0);
94 };
95 }
96}
97
98module.exports = {
99 set: set,
100 clear: clear
101};