1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | (function (global, undefined) {
|
25 | "use strict";
|
26 |
|
27 | if (global.setImmediate) {
|
28 | return;
|
29 | }
|
30 |
|
31 | var nextHandle = 1;
|
32 | var tasksByHandle = {};
|
33 | var currentlyRunningATask = false;
|
34 | var doc = global.document;
|
35 | var setImmediate;
|
36 |
|
37 | function addFromSetImmediateArguments(args) {
|
38 | tasksByHandle[nextHandle] = partiallyApplied.apply(undefined, args);
|
39 | return nextHandle++;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 | function partiallyApplied(handler) {
|
45 | var args = [].slice.call(arguments, 1);
|
46 | return function() {
|
47 | if (typeof handler === "function") {
|
48 | handler.apply(undefined, args);
|
49 | } else {
|
50 | (new Function("" + handler))();
|
51 | }
|
52 | };
|
53 | }
|
54 |
|
55 | function runIfPresent(handle) {
|
56 |
|
57 |
|
58 | if (currentlyRunningATask) {
|
59 |
|
60 |
|
61 | setTimeout(partiallyApplied(runIfPresent, handle), 0);
|
62 | } else {
|
63 | var task = tasksByHandle[handle];
|
64 | if (task) {
|
65 | currentlyRunningATask = true;
|
66 | try {
|
67 | task();
|
68 | } finally {
|
69 | clearImmediate(handle);
|
70 | currentlyRunningATask = false;
|
71 | }
|
72 | }
|
73 | }
|
74 | }
|
75 |
|
76 | function clearImmediate(handle) {
|
77 | delete tasksByHandle[handle];
|
78 | }
|
79 |
|
80 | function installNextTickImplementation() {
|
81 | setImmediate = function() {
|
82 | var handle = addFromSetImmediateArguments(arguments);
|
83 | process.nextTick(partiallyApplied(runIfPresent, handle));
|
84 | return handle;
|
85 | };
|
86 | }
|
87 |
|
88 | function canUsePostMessage() {
|
89 |
|
90 |
|
91 | if (global.postMessage && !global.importScripts) {
|
92 | var postMessageIsAsynchronous = true;
|
93 | var oldOnMessage = global.onmessage;
|
94 | global.onmessage = function() {
|
95 | postMessageIsAsynchronous = false;
|
96 | };
|
97 | global.postMessage("", "*");
|
98 | global.onmessage = oldOnMessage;
|
99 | return postMessageIsAsynchronous;
|
100 | }
|
101 | }
|
102 |
|
103 | function installPostMessageImplementation() {
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | var messagePrefix = "setImmediate$" + Math.random() + "$";
|
109 | var onGlobalMessage = function(event) {
|
110 | if (event.source === global &&
|
111 | typeof event.data === "string" &&
|
112 | event.data.indexOf(messagePrefix) === 0) {
|
113 | runIfPresent(+event.data.slice(messagePrefix.length));
|
114 | }
|
115 | };
|
116 |
|
117 | if (global.addEventListener) {
|
118 | global.addEventListener("message", onGlobalMessage, false);
|
119 | } else {
|
120 | global.attachEvent("onmessage", onGlobalMessage);
|
121 | }
|
122 |
|
123 | setImmediate = function() {
|
124 | var handle = addFromSetImmediateArguments(arguments);
|
125 | global.postMessage(messagePrefix + handle, "*");
|
126 | return handle;
|
127 | };
|
128 | }
|
129 |
|
130 | function installMessageChannelImplementation() {
|
131 | var channel = new MessageChannel();
|
132 | channel.port1.onmessage = function(event) {
|
133 | var handle = event.data;
|
134 | runIfPresent(handle);
|
135 | };
|
136 |
|
137 | setImmediate = function() {
|
138 | var handle = addFromSetImmediateArguments(arguments);
|
139 | channel.port2.postMessage(handle);
|
140 | return handle;
|
141 | };
|
142 | }
|
143 |
|
144 | function installReadyStateChangeImplementation() {
|
145 | var html = doc.documentElement;
|
146 | setImmediate = function() {
|
147 | var handle = addFromSetImmediateArguments(arguments);
|
148 |
|
149 |
|
150 | var script = doc.createElement("script");
|
151 | script.onreadystatechange = function () {
|
152 | runIfPresent(handle);
|
153 | script.onreadystatechange = null;
|
154 | html.removeChild(script);
|
155 | script = null;
|
156 | };
|
157 | html.appendChild(script);
|
158 | return handle;
|
159 | };
|
160 | }
|
161 |
|
162 | function installSetTimeoutImplementation() {
|
163 | setImmediate = function() {
|
164 | var handle = addFromSetImmediateArguments(arguments);
|
165 | setTimeout(partiallyApplied(runIfPresent, handle), 0);
|
166 | return handle;
|
167 | };
|
168 | }
|
169 |
|
170 |
|
171 | var attachTo = Object.getPrototypeOf && Object.getPrototypeOf(global);
|
172 | attachTo = attachTo && attachTo.setTimeout ? attachTo : global;
|
173 |
|
174 |
|
175 | if ({}.toString.call(global.process) === "[object process]") {
|
176 |
|
177 | installNextTickImplementation();
|
178 |
|
179 | } else if (canUsePostMessage()) {
|
180 |
|
181 | installPostMessageImplementation();
|
182 |
|
183 | } else if (global.MessageChannel) {
|
184 |
|
185 | installMessageChannelImplementation();
|
186 |
|
187 | } else if (doc && "onreadystatechange" in doc.createElement("script")) {
|
188 |
|
189 | installReadyStateChangeImplementation();
|
190 |
|
191 | } else {
|
192 |
|
193 | installSetTimeoutImplementation();
|
194 | }
|
195 |
|
196 | attachTo.setImmediate = setImmediate;
|
197 | attachTo.clearImmediate = clearImmediate;
|
198 | }(self));
|