process-es6.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // shim for using process in browser
  2. // based off https://github.com/defunctzombie/node-process/blob/master/browser.js
  3. function defaultSetTimout() {
  4. throw new Error('setTimeout has not been defined');
  5. }
  6. function defaultClearTimeout () {
  7. throw new Error('clearTimeout has not been defined');
  8. }
  9. var cachedSetTimeout = defaultSetTimout;
  10. var cachedClearTimeout = defaultClearTimeout;
  11. if (typeof global.setTimeout === 'function') {
  12. cachedSetTimeout = setTimeout;
  13. }
  14. if (typeof global.clearTimeout === 'function') {
  15. cachedClearTimeout = clearTimeout;
  16. }
  17. function runTimeout(fun) {
  18. if (cachedSetTimeout === setTimeout) {
  19. //normal enviroments in sane situations
  20. return setTimeout(fun, 0);
  21. }
  22. // if setTimeout wasn't available but was latter defined
  23. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  24. cachedSetTimeout = setTimeout;
  25. return setTimeout(fun, 0);
  26. }
  27. try {
  28. // when when somebody has screwed with setTimeout but no I.E. maddness
  29. return cachedSetTimeout(fun, 0);
  30. } catch(e){
  31. try {
  32. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  33. return cachedSetTimeout.call(null, fun, 0);
  34. } catch(e){
  35. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  36. return cachedSetTimeout.call(this, fun, 0);
  37. }
  38. }
  39. }
  40. function runClearTimeout(marker) {
  41. if (cachedClearTimeout === clearTimeout) {
  42. //normal enviroments in sane situations
  43. return clearTimeout(marker);
  44. }
  45. // if clearTimeout wasn't available but was latter defined
  46. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  47. cachedClearTimeout = clearTimeout;
  48. return clearTimeout(marker);
  49. }
  50. try {
  51. // when when somebody has screwed with setTimeout but no I.E. maddness
  52. return cachedClearTimeout(marker);
  53. } catch (e){
  54. try {
  55. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  56. return cachedClearTimeout.call(null, marker);
  57. } catch (e){
  58. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  59. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  60. return cachedClearTimeout.call(this, marker);
  61. }
  62. }
  63. }
  64. var queue = [];
  65. var draining = false;
  66. var currentQueue;
  67. var queueIndex = -1;
  68. function cleanUpNextTick() {
  69. if (!draining || !currentQueue) {
  70. return;
  71. }
  72. draining = false;
  73. if (currentQueue.length) {
  74. queue = currentQueue.concat(queue);
  75. } else {
  76. queueIndex = -1;
  77. }
  78. if (queue.length) {
  79. drainQueue();
  80. }
  81. }
  82. function drainQueue() {
  83. if (draining) {
  84. return;
  85. }
  86. var timeout = runTimeout(cleanUpNextTick);
  87. draining = true;
  88. var len = queue.length;
  89. while(len) {
  90. currentQueue = queue;
  91. queue = [];
  92. while (++queueIndex < len) {
  93. if (currentQueue) {
  94. currentQueue[queueIndex].run();
  95. }
  96. }
  97. queueIndex = -1;
  98. len = queue.length;
  99. }
  100. currentQueue = null;
  101. draining = false;
  102. runClearTimeout(timeout);
  103. }
  104. function nextTick(fun) {
  105. var args = new Array(arguments.length - 1);
  106. if (arguments.length > 1) {
  107. for (var i = 1; i < arguments.length; i++) {
  108. args[i - 1] = arguments[i];
  109. }
  110. }
  111. queue.push(new Item(fun, args));
  112. if (queue.length === 1 && !draining) {
  113. runTimeout(drainQueue);
  114. }
  115. }
  116. // v8 likes predictible objects
  117. function Item(fun, array) {
  118. this.fun = fun;
  119. this.array = array;
  120. }
  121. Item.prototype.run = function () {
  122. this.fun.apply(null, this.array);
  123. };
  124. var title = 'browser';
  125. var platform = 'browser';
  126. var browser = true;
  127. var env = {};
  128. var argv = [];
  129. var version = ''; // empty string to avoid regexp issues
  130. var versions = {};
  131. var release = {};
  132. var config = {};
  133. function noop() {}
  134. var on = noop;
  135. var addListener = noop;
  136. var once = noop;
  137. var off = noop;
  138. var removeListener = noop;
  139. var removeAllListeners = noop;
  140. var emit = noop;
  141. function binding(name) {
  142. throw new Error('process.binding is not supported');
  143. }
  144. function cwd () { return '/' }
  145. function chdir (dir) {
  146. throw new Error('process.chdir is not supported');
  147. }function umask() { return 0; }
  148. // from https://github.com/kumavis/browser-process-hrtime/blob/master/index.js
  149. var performance = global.performance || {};
  150. var performanceNow =
  151. performance.now ||
  152. performance.mozNow ||
  153. performance.msNow ||
  154. performance.oNow ||
  155. performance.webkitNow ||
  156. function(){ return (new Date()).getTime() };
  157. // generate timestamp or delta
  158. // see http://nodejs.org/api/process.html#process_process_hrtime
  159. function hrtime(previousTimestamp){
  160. var clocktime = performanceNow.call(performance)*1e-3;
  161. var seconds = Math.floor(clocktime);
  162. var nanoseconds = Math.floor((clocktime%1)*1e9);
  163. if (previousTimestamp) {
  164. seconds = seconds - previousTimestamp[0];
  165. nanoseconds = nanoseconds - previousTimestamp[1];
  166. if (nanoseconds<0) {
  167. seconds--;
  168. nanoseconds += 1e9;
  169. }
  170. }
  171. return [seconds,nanoseconds]
  172. }
  173. var startTime = new Date();
  174. function uptime() {
  175. var currentTime = new Date();
  176. var dif = currentTime - startTime;
  177. return dif / 1000;
  178. }
  179. var browser$1 = {
  180. nextTick: nextTick,
  181. title: title,
  182. browser: browser,
  183. env: env,
  184. argv: argv,
  185. version: version,
  186. versions: versions,
  187. on: on,
  188. addListener: addListener,
  189. once: once,
  190. off: off,
  191. removeListener: removeListener,
  192. removeAllListeners: removeAllListeners,
  193. emit: emit,
  194. binding: binding,
  195. cwd: cwd,
  196. chdir: chdir,
  197. umask: umask,
  198. hrtime: hrtime,
  199. platform: platform,
  200. release: release,
  201. config: config,
  202. uptime: uptime
  203. };
  204. export default browser$1;
  205. export { addListener, argv, binding, browser, chdir, config, cwd, emit, env, hrtime, nextTick, off, on, once, platform, release, removeAllListeners, removeListener, title, umask, uptime, version, versions };