1 | 'use strict';
|
2 | var global = require('../internals/global');
|
3 | var apply = require('../internals/function-apply');
|
4 | var bind = require('../internals/function-bind-context');
|
5 | var isCallable = require('../internals/is-callable');
|
6 | var hasOwn = require('../internals/has-own-property');
|
7 | var fails = require('../internals/fails');
|
8 | var html = require('../internals/html');
|
9 | var arraySlice = require('../internals/array-slice');
|
10 | var createElement = require('../internals/document-create-element');
|
11 | var validateArgumentsLength = require('../internals/validate-arguments-length');
|
12 | var IS_IOS = require('../internals/engine-is-ios');
|
13 | var IS_NODE = require('../internals/engine-is-node');
|
14 |
|
15 | var set = global.setImmediate;
|
16 | var clear = global.clearImmediate;
|
17 | var process = global.process;
|
18 | var Dispatch = global.Dispatch;
|
19 | var Function = global.Function;
|
20 | var MessageChannel = global.MessageChannel;
|
21 | var String = global.String;
|
22 | var counter = 0;
|
23 | var queue = {};
|
24 | var ONREADYSTATECHANGE = 'onreadystatechange';
|
25 | var $location, defer, channel, port;
|
26 |
|
27 | fails(function () {
|
28 |
|
29 | $location = global.location;
|
30 | });
|
31 |
|
32 | var run = function (id) {
|
33 | if (hasOwn(queue, id)) {
|
34 | var fn = queue[id];
|
35 | delete queue[id];
|
36 | fn();
|
37 | }
|
38 | };
|
39 |
|
40 | var runner = function (id) {
|
41 | return function () {
|
42 | run(id);
|
43 | };
|
44 | };
|
45 |
|
46 | var eventListener = function (event) {
|
47 | run(event.data);
|
48 | };
|
49 |
|
50 | var globalPostMessageDefer = function (id) {
|
51 |
|
52 | global.postMessage(String(id), $location.protocol + '//' + $location.host);
|
53 | };
|
54 |
|
55 |
|
56 | if (!set || !clear) {
|
57 | set = function setImmediate(handler) {
|
58 | validateArgumentsLength(arguments.length, 1);
|
59 | var fn = isCallable(handler) ? handler : Function(handler);
|
60 | var args = arraySlice(arguments, 1);
|
61 | queue[++counter] = function () {
|
62 | apply(fn, undefined, args);
|
63 | };
|
64 | defer(counter);
|
65 | return counter;
|
66 | };
|
67 | clear = function clearImmediate(id) {
|
68 | delete queue[id];
|
69 | };
|
70 |
|
71 | if (IS_NODE) {
|
72 | defer = function (id) {
|
73 | process.nextTick(runner(id));
|
74 | };
|
75 |
|
76 | } else if (Dispatch && Dispatch.now) {
|
77 | defer = function (id) {
|
78 | Dispatch.now(runner(id));
|
79 | };
|
80 |
|
81 |
|
82 | } else if (MessageChannel && !IS_IOS) {
|
83 | channel = new MessageChannel();
|
84 | port = channel.port2;
|
85 | channel.port1.onmessage = eventListener;
|
86 | defer = bind(port.postMessage, port);
|
87 |
|
88 |
|
89 | } else if (
|
90 | global.addEventListener &&
|
91 | isCallable(global.postMessage) &&
|
92 | !global.importScripts &&
|
93 | $location && $location.protocol !== 'file:' &&
|
94 | !fails(globalPostMessageDefer)
|
95 | ) {
|
96 | defer = globalPostMessageDefer;
|
97 | global.addEventListener('message', eventListener, false);
|
98 |
|
99 | } else if (ONREADYSTATECHANGE in createElement('script')) {
|
100 | defer = function (id) {
|
101 | html.appendChild(createElement('script'))[ONREADYSTATECHANGE] = function () {
|
102 | html.removeChild(this);
|
103 | run(id);
|
104 | };
|
105 | };
|
106 |
|
107 | } else {
|
108 | defer = function (id) {
|
109 | setTimeout(runner(id), 0);
|
110 | };
|
111 | }
|
112 | }
|
113 |
|
114 | module.exports = {
|
115 | set: set,
|
116 | clear: clear
|
117 | };
|