Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80708 views
1
// shim for using process in browser
2
3
var process = module.exports = {};
4
5
process.nextTick = (function () {
6
var canSetImmediate = typeof window !== 'undefined'
7
&& window.setImmediate;
8
var canMutationObserver = typeof window !== 'undefined'
9
&& window.MutationObserver;
10
var canPost = typeof window !== 'undefined'
11
&& window.postMessage && window.addEventListener
12
;
13
14
if (canSetImmediate) {
15
return function (f) { return window.setImmediate(f) };
16
}
17
18
var queue = [];
19
20
if (canMutationObserver) {
21
var hiddenDiv = document.createElement("div");
22
var observer = new MutationObserver(function () {
23
var queueList = queue.slice();
24
queue.length = 0;
25
queueList.forEach(function (fn) {
26
fn();
27
});
28
});
29
30
observer.observe(hiddenDiv, { attributes: true });
31
32
return function nextTick(fn) {
33
if (!queue.length) {
34
hiddenDiv.setAttribute('yes', 'no');
35
}
36
queue.push(fn);
37
};
38
}
39
40
if (canPost) {
41
window.addEventListener('message', function (ev) {
42
var source = ev.source;
43
if ((source === window || source === null) && ev.data === 'process-tick') {
44
ev.stopPropagation();
45
if (queue.length > 0) {
46
var fn = queue.shift();
47
fn();
48
}
49
}
50
}, true);
51
52
return function nextTick(fn) {
53
queue.push(fn);
54
window.postMessage('process-tick', '*');
55
};
56
}
57
58
return function nextTick(fn) {
59
setTimeout(fn, 0);
60
};
61
})();
62
63
process.title = 'browser';
64
process.browser = true;
65
process.env = {};
66
process.argv = [];
67
68
function noop() {}
69
70
process.on = noop;
71
process.addListener = noop;
72
process.once = noop;
73
process.off = noop;
74
process.removeListener = noop;
75
process.removeAllListeners = noop;
76
process.emit = noop;
77
78
process.binding = function (name) {
79
throw new Error('process.binding is not supported');
80
};
81
82
// TODO(shtylman)
83
process.cwd = function () { return '/' };
84
process.chdir = function (dir) {
85
throw new Error('process.chdir is not supported');
86
};
87
88