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');
9 var location = global.location;
10 var set = global.setImmediate;
11 var clear = global.clearImmediate;
12 var process = global.process;
13 var MessageChannel = global.MessageChannel;
14 var Dispatch = global.Dispatch;
17 var ONREADYSTATECHANGE = 'onreadystatechange';
18 var defer, channel, port;
20 var run = function (id) {
21 // eslint-disable-next-line no-prototype-builtins
22 if (queue.hasOwnProperty(id)) {
29 var runner = function (id) {
35 var listener = function (event) {
39 var post = function (id) {
40 // old engines have not location.origin
41 global.postMessage(id + '', location.protocol + '//' + location.host);
44 // Node.js 0.9+ & IE10+ has setImmediate, otherwise:
46 set = function setImmediate(fn) {
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);
57 clear = function clearImmediate(id) {
62 defer = function (id) {
63 process.nextTick(runner(id));
65 // Sphere (JS game engine) Dispatch API
66 } else if (Dispatch && Dispatch.now) {
67 defer = function (id) {
68 Dispatch.now(runner(id));
70 // Browsers with MessageChannel, includes WebWorkers
71 // except iOS - https://github.com/zloirock/core-js/issues/624
72 } else if (MessageChannel && !IS_IOS) {
73 channel = new MessageChannel();
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'
80 global.addEventListener &&
81 typeof postMessage == 'function' &&
82 !global.importScripts &&
83 location && location.protocol !== 'file:' &&
87 global.addEventListener('message', listener, false);
89 } else if (ONREADYSTATECHANGE in createElement('script')) {
90 defer = function (id) {
91 html.appendChild(createElement('script'))[ONREADYSTATECHANGE] = function () {
92 html.removeChild(this);
98 defer = function (id) {
99 setTimeout(runner(id), 0);