Skip to content

Commit 7bd2754

Browse files
lbogdanCompuIves
authored andcommitted
Update process with some missing APIs (mosy notably nextTick()) (codesandbox#340)
1 parent ca43994 commit 7bd2754

File tree

1 file changed

+159
-4
lines changed
  • packages/app/src/sandbox/eval/loaders/utils

1 file changed

+159
-4
lines changed

packages/app/src/sandbox/eval/loaders/utils/process.js

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,162 @@
1+
// from https://unpkg.com/process@0.11.10/browser.js
2+
13
const process = {};
4+
5+
// cached from whatever global is present so that test runners that stub it
6+
// don't break things. But we need to wrap it in a try catch in case it is
7+
// wrapped in strict mode code which doesn't define any globals. It's inside a
8+
// function because try/catches deoptimize in certain engines.
9+
10+
let cachedSetTimeout;
11+
let cachedClearTimeout;
12+
let queue = [];
13+
let draining = false;
14+
let currentQueue;
15+
let queueIndex = -1;
16+
17+
function defaultSetTimout() {
18+
throw new Error('setTimeout has not been defined');
19+
}
20+
function defaultClearTimeout() {
21+
throw new Error('clearTimeout has not been defined');
22+
}
23+
(function() {
24+
try {
25+
if (typeof setTimeout === 'function') {
26+
cachedSetTimeout = setTimeout;
27+
} else {
28+
cachedSetTimeout = defaultSetTimout;
29+
}
30+
} catch (e) {
31+
cachedSetTimeout = defaultSetTimout;
32+
}
33+
try {
34+
if (typeof clearTimeout === 'function') {
35+
cachedClearTimeout = clearTimeout;
36+
} else {
37+
cachedClearTimeout = defaultClearTimeout;
38+
}
39+
} catch (e) {
40+
cachedClearTimeout = defaultClearTimeout;
41+
}
42+
})();
43+
function runTimeout(fun) {
44+
if (cachedSetTimeout === setTimeout) {
45+
// normal enviroments in sane situations
46+
return setTimeout(fun, 0);
47+
}
48+
// if setTimeout wasn't available but was latter defined
49+
if (
50+
(cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) &&
51+
setTimeout
52+
) {
53+
cachedSetTimeout = setTimeout;
54+
return setTimeout(fun, 0);
55+
}
56+
try {
57+
// when when somebody has screwed with setTimeout but no I.E. maddness
58+
return cachedSetTimeout(fun, 0);
59+
} catch (e) {
60+
try {
61+
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
62+
return cachedSetTimeout.call(null, fun, 0);
63+
} catch (e) {
64+
// eslint-disable-line no-shadow
65+
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
66+
return cachedSetTimeout.call(this, fun, 0);
67+
}
68+
}
69+
}
70+
function runClearTimeout(marker) {
71+
if (cachedClearTimeout === clearTimeout) {
72+
// normal enviroments in sane situations
73+
return clearTimeout(marker);
74+
}
75+
// if clearTimeout wasn't available but was latter defined
76+
if (
77+
(cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) &&
78+
clearTimeout
79+
) {
80+
cachedClearTimeout = clearTimeout;
81+
return clearTimeout(marker);
82+
}
83+
try {
84+
// when when somebody has screwed with setTimeout but no I.E. maddness
85+
return cachedClearTimeout(marker);
86+
} catch (e) {
87+
try {
88+
// When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
89+
return cachedClearTimeout.call(null, marker);
90+
} catch (e) {
91+
// eslint-disable-line no-shadow
92+
// same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
93+
// Some versions of I.E. have different rules for clearTimeout vs setTimeout
94+
return cachedClearTimeout.call(this, marker);
95+
}
96+
}
97+
}
98+
function drainQueue() {
99+
if (draining) {
100+
return;
101+
}
102+
// eslint-disable-next-line no-use-before-define
103+
const timeout = runTimeout(cleanUpNextTick);
104+
draining = true;
105+
106+
let len = queue.length;
107+
while (len) {
108+
currentQueue = queue;
109+
queue = [];
110+
while (++queueIndex < len) {
111+
if (currentQueue) {
112+
currentQueue[queueIndex].run();
113+
}
114+
}
115+
queueIndex = -1;
116+
len = queue.length;
117+
}
118+
currentQueue = null;
119+
draining = false;
120+
runClearTimeout(timeout);
121+
}
122+
function cleanUpNextTick() {
123+
if (!draining || !currentQueue) {
124+
return;
125+
}
126+
draining = false;
127+
if (currentQueue.length) {
128+
queue = currentQueue.concat(queue);
129+
} else {
130+
queueIndex = -1;
131+
}
132+
if (queue.length) {
133+
drainQueue();
134+
}
135+
}
136+
// v8 likes predictible objects
137+
function Item(fun, array) {
138+
this.fun = fun;
139+
this.array = array;
140+
}
141+
Item.prototype.run = function() {
142+
this.fun.apply(null, this.array);
143+
};
144+
process.nextTick = function(fun) {
145+
const args = new Array(arguments.length - 1);
146+
if (arguments.length > 1) {
147+
for (let i = 1; i < arguments.length; i++) {
148+
// eslint-disable-next-line prefer-rest-params
149+
args[i - 1] = arguments[i];
150+
}
151+
}
152+
queue.push(new Item(fun, args));
153+
if (queue.length === 1 && !draining) {
154+
runTimeout(drainQueue);
155+
}
156+
};
2157
process.title = 'browser';
3158
process.browser = true;
4-
process.env = { NODE_ENV: 'development' };
159+
process.env = {};
5160
process.argv = [];
6161
process.version = ''; // empty string to avoid regexp issues
7162
process.versions = {};
@@ -18,18 +173,18 @@ process.emit = noop;
18173
process.prependListener = noop;
19174
process.prependOnceListener = noop;
20175

21-
process.listeners = function(name) {
176+
process.listeners = function() {
22177
return [];
23178
};
24179

25-
process.binding = function(name) {
180+
process.binding = function() {
26181
throw new Error('process.binding is not supported');
27182
};
28183

29184
process.cwd = function() {
30185
return '/';
31186
};
32-
process.chdir = function(dir) {
187+
process.chdir = function() {
33188
throw new Error('process.chdir is not supported');
34189
};
35190
process.umask = function() {

0 commit comments

Comments
 (0)