Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

setImmediate.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import _Object$getPrototypeOf from "babel-runtime/core-js/object/get-prototype-of";
  2. /*
  3. Copyright (c) 2012 Barnesandnoble.com, llc, Donavon West, and Domenic Denicola
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. (function (global, undefined) {
  22. "use strict";
  23. if (global.setImmediate) {
  24. return;
  25. }
  26. var nextHandle = 1; // Spec says greater than zero
  27. var tasksByHandle = {};
  28. var currentlyRunningATask = false;
  29. var doc = global.document;
  30. var setImmediate;
  31. function addFromSetImmediateArguments(args) {
  32. tasksByHandle[nextHandle] = partiallyApplied.apply(undefined, args);
  33. return nextHandle++;
  34. }
  35. // This function accepts the same arguments as setImmediate, but
  36. // returns a function that requires no arguments.
  37. function partiallyApplied(handler) {
  38. var args = [].slice.call(arguments, 1);
  39. return function () {
  40. if (typeof handler === "function") {
  41. handler.apply(undefined, args);
  42. } else {
  43. new Function("" + handler)();
  44. }
  45. };
  46. }
  47. function runIfPresent(handle) {
  48. // From the spec: "Wait until any invocations of this algorithm started before this one have completed."
  49. // So if we're currently running a task, we'll need to delay this invocation.
  50. if (currentlyRunningATask) {
  51. // Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a
  52. // "too much recursion" error.
  53. setTimeout(partiallyApplied(runIfPresent, handle), 0);
  54. } else {
  55. var task = tasksByHandle[handle];
  56. if (task) {
  57. currentlyRunningATask = true;
  58. try {
  59. task();
  60. } finally {
  61. clearImmediate(handle);
  62. currentlyRunningATask = false;
  63. }
  64. }
  65. }
  66. }
  67. function clearImmediate(handle) {
  68. delete tasksByHandle[handle];
  69. }
  70. function installNextTickImplementation() {
  71. setImmediate = function setImmediate() {
  72. var handle = addFromSetImmediateArguments(arguments);
  73. process.nextTick(partiallyApplied(runIfPresent, handle));
  74. return handle;
  75. };
  76. }
  77. function canUsePostMessage() {
  78. // The test against `importScripts` prevents this implementation from being installed inside a web worker,
  79. // where `global.postMessage` means something completely different and can't be used for this purpose.
  80. if (global.postMessage && !global.importScripts) {
  81. var postMessageIsAsynchronous = true;
  82. var oldOnMessage = global.onmessage;
  83. global.onmessage = function () {
  84. postMessageIsAsynchronous = false;
  85. };
  86. global.postMessage("", "*");
  87. global.onmessage = oldOnMessage;
  88. return postMessageIsAsynchronous;
  89. }
  90. }
  91. function installPostMessageImplementation() {
  92. // Installs an event handler on `global` for the `message` event: see
  93. // * https://developer.mozilla.org/en/DOM/window.postMessage
  94. // * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages
  95. var messagePrefix = "setImmediate$" + Math.random() + "$";
  96. var onGlobalMessage = function onGlobalMessage(event) {
  97. if (event.source === global && typeof event.data === "string" && event.data.indexOf(messagePrefix) === 0) {
  98. runIfPresent(+event.data.slice(messagePrefix.length));
  99. }
  100. };
  101. if (global.addEventListener) {
  102. global.addEventListener("message", onGlobalMessage, false);
  103. } else {
  104. global.attachEvent("onmessage", onGlobalMessage);
  105. }
  106. setImmediate = function setImmediate() {
  107. var handle = addFromSetImmediateArguments(arguments);
  108. global.postMessage(messagePrefix + handle, "*");
  109. return handle;
  110. };
  111. }
  112. function installMessageChannelImplementation() {
  113. var channel = new MessageChannel();
  114. channel.port1.onmessage = function (event) {
  115. var handle = event.data;
  116. runIfPresent(handle);
  117. };
  118. setImmediate = function setImmediate() {
  119. var handle = addFromSetImmediateArguments(arguments);
  120. channel.port2.postMessage(handle);
  121. return handle;
  122. };
  123. }
  124. function installReadyStateChangeImplementation() {
  125. var html = doc.documentElement;
  126. setImmediate = function setImmediate() {
  127. var handle = addFromSetImmediateArguments(arguments);
  128. // Create a <script> element; its readystatechange event will be fired asynchronously once it is inserted
  129. // into the document. Do so, thus queuing up the task. Remember to clean up once it's been called.
  130. var script = doc.createElement("script");
  131. script.onreadystatechange = function () {
  132. runIfPresent(handle);
  133. script.onreadystatechange = null;
  134. html.removeChild(script);
  135. script = null;
  136. };
  137. html.appendChild(script);
  138. return handle;
  139. };
  140. }
  141. function installSetTimeoutImplementation() {
  142. setImmediate = function setImmediate() {
  143. var handle = addFromSetImmediateArguments(arguments);
  144. setTimeout(partiallyApplied(runIfPresent, handle), 0);
  145. return handle;
  146. };
  147. }
  148. // If supported, we should attach to the prototype of global, since that is where setTimeout et al. live.
  149. var attachTo = _Object$getPrototypeOf && _Object$getPrototypeOf(global);
  150. attachTo = attachTo && attachTo.setTimeout ? attachTo : global;
  151. // Don't get fooled by e.g. browserify environments.
  152. if ({}.toString.call(global.process) === "[object process]") {
  153. // For Node.js before 0.9
  154. installNextTickImplementation();
  155. } else if (canUsePostMessage()) {
  156. // For non-IE10 modern browsers
  157. installPostMessageImplementation();
  158. } else if (global.MessageChannel) {
  159. // For web workers, where supported
  160. installMessageChannelImplementation();
  161. } else if (doc && "onreadystatechange" in doc.createElement("script")) {
  162. // For IE 6–8
  163. installReadyStateChangeImplementation();
  164. } else {
  165. // For older browsers
  166. installSetTimeoutImplementation();
  167. }
  168. attachTo.setImmediate = setImmediate;
  169. attachTo.clearImmediate = clearImmediate;
  170. })(self);