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