1 var global = require('../internals/global');
2 var apply = require('../internals/function-apply');
3 var bind = require('../internals/function-bind-context');
4 var isCallable = require('../internals/is-callable');
5 var hasOwn = require('../internals/has-own-property');
6 var fails = require('../internals/fails');
7 var html = require('../internals/html');
8 var arraySlice = require('../internals/array-slice');
9 var createElement = require('../internals/document-create-element');
10 var IS_IOS = require('../internals/engine-is-ios');
11 var IS_NODE = require('../internals/engine-is-node');
13 var set = global.setImmediate;
14 var clear = global.clearImmediate;
15 var process = global.process;
16 var Dispatch = global.Dispatch;
17 var Function = global.Function;
18 var MessageChannel = global.MessageChannel;
19 var String = global.String;
22 var ONREADYSTATECHANGE = 'onreadystatechange';
23 var location, defer, channel, port;
26 // Deno throws a ReferenceError on `location` access without `--location` flag
27 location = global.location;
28 } catch (error) { /* empty */ }
30 var run = function (id) {
31 if (hasOwn(queue, id)) {
38 var runner = function (id) {
44 var listener = function (event) {
48 var post = function (id) {
49 // old engines have not location.origin
50 global.postMessage(String(id), location.protocol + '//' + location.host);
53 // Node.js 0.9+ & IE10+ has setImmediate, otherwise:
55 set = function setImmediate(fn) {
56 var args = arraySlice(arguments, 1);
57 queue[++counter] = function () {
58 apply(isCallable(fn) ? fn : Function(fn), undefined, args);
63 clear = function clearImmediate(id) {
68 defer = function (id) {
69 process.nextTick(runner(id));
71 // Sphere (JS game engine) Dispatch API
72 } else if (Dispatch && Dispatch.now) {
73 defer = function (id) {
74 Dispatch.now(runner(id));
76 // Browsers with MessageChannel, includes WebWorkers
77 // except iOS - https://github.com/zloirock/core-js/issues/624
78 } else if (MessageChannel && !IS_IOS) {
79 channel = new MessageChannel();
81 channel.port1.onmessage = listener;
82 defer = bind(port.postMessage, port);
83 // Browsers with postMessage, skip WebWorkers
84 // IE8 has postMessage, but it's sync & typeof its postMessage is 'object'
86 global.addEventListener &&
87 isCallable(global.postMessage) &&
88 !global.importScripts &&
89 location && location.protocol !== 'file:' &&
93 global.addEventListener('message', listener, false);
95 } else if (ONREADYSTATECHANGE in createElement('script')) {
96 defer = function (id) {
97 html.appendChild(createElement('script'))[ONREADYSTATECHANGE] = function () {
98 html.removeChild(this);
104 defer = function (id) {
105 setTimeout(runner(id), 0);