UNPKG

1.37 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 var source = ev.source;
20 if ((source === window || source === null) && ev.data === 'process-tick') {
21 ev.stopPropagation();
22 if (queue.length > 0) {
23 var fn = queue.shift();
24 fn();
25 }
26 }
27 }, true);
28
29 return function nextTick(fn) {
30 queue.push(fn);
31 window.postMessage('process-tick', '*');
32 };
33 }
34
35 return function nextTick(fn) {
36 setTimeout(fn, 0);
37 };
38})();
39
40process.title = 'browser';
41process.browser = true;
42process.env = {};
43process.argv = [];
44
45process.binding = function (name) {
46 throw new Error('process.binding is not supported');
47}
48
49// TODO(shtylman)
50process.cwd = function () { return '/' };
51process.chdir = function (dir) {
52 throw new Error('process.chdir is not supported');
53};