Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
// shim for using process in browser
2
3
var process = module.exports = {};
4
var queue = [];
5
var draining = false;
6
var currentQueue;
7
var queueIndex = -1;
8
9
function cleanUpNextTick() {
10
draining = false;
11
if (currentQueue.length) {
12
queue = currentQueue.concat(queue);
13
} else {
14
queueIndex = -1;
15
}
16
if (queue.length) {
17
drainQueue();
18
}
19
}
20
21
function drainQueue() {
22
if (draining) {
23
return;
24
}
25
var timeout = setTimeout(cleanUpNextTick);
26
draining = true;
27
28
var len = queue.length;
29
while(len) {
30
currentQueue = queue;
31
queue = [];
32
while (++queueIndex < len) {
33
currentQueue[queueIndex].run();
34
}
35
queueIndex = -1;
36
len = queue.length;
37
}
38
currentQueue = null;
39
draining = false;
40
clearTimeout(timeout);
41
}
42
43
process.nextTick = function (fun) {
44
var args = new Array(arguments.length - 1);
45
if (arguments.length > 1) {
46
for (var i = 1; i < arguments.length; i++) {
47
args[i - 1] = arguments[i];
48
}
49
}
50
queue.push(new Item(fun, args));
51
if (queue.length === 1 && !draining) {
52
setTimeout(drainQueue, 0);
53
}
54
};
55
56
// v8 likes predictible objects
57
function Item(fun, array) {
58
this.fun = fun;
59
this.array = array;
60
}
61
Item.prototype.run = function () {
62
this.fun.apply(null, this.array);
63
};
64
process.title = 'browser';
65
process.browser = true;
66
process.env = {};
67
process.argv = [];
68
process.version = ''; // empty string to avoid regexp issues
69
process.versions = {};
70
71
function noop() {}
72
73
process.on = noop;
74
process.addListener = noop;
75
process.once = noop;
76
process.off = noop;
77
process.removeListener = noop;
78
process.removeAllListeners = noop;
79
process.emit = noop;
80
81
process.binding = function (name) {
82
throw new Error('process.binding is not supported');
83
};
84
85
// TODO(shtylman)
86
process.cwd = function () { return '/' };
87
process.chdir = function (dir) {
88
throw new Error('process.chdir is not supported');
89
};
90
process.umask = function() { return 0; };
91
92