UNPKG

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