browser.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // shim for using process in browser
  2. var process = module.exports = {};
  3. process.nextTick = (function () {
  4. var canSetImmediate = typeof window !== 'undefined'
  5. && window.setImmediate;
  6. var canPost = typeof window !== 'undefined'
  7. && window.postMessage && window.addEventListener
  8. ;
  9. if (canSetImmediate) {
  10. return function (f) { return window.setImmediate(f) };
  11. }
  12. if (canPost) {
  13. var queue = [];
  14. window.addEventListener('message', function (ev) {
  15. var source = ev.source;
  16. if ((source === window || source === null) && ev.data === 'process-tick') {
  17. ev.stopPropagation();
  18. if (queue.length > 0) {
  19. var fn = queue.shift();
  20. fn();
  21. }
  22. }
  23. }, true);
  24. return function nextTick(fn) {
  25. queue.push(fn);
  26. window.postMessage('process-tick', '*');
  27. };
  28. }
  29. return function nextTick(fn) {
  30. setTimeout(fn, 0);
  31. };
  32. })();
  33. process.title = 'browser';
  34. process.browser = true;
  35. process.env = {};
  36. process.argv = [];
  37. process.binding = function (name) {
  38. throw new Error('process.binding is not supported');
  39. }
  40. // TODO(shtylman)
  41. process.cwd = function () { return '/' };
  42. process.chdir = function (dir) {
  43. throw new Error('process.chdir is not supported');
  44. };