Skip to content

Commit b468a6a

Browse files
committed
Fix ordering to make sure functions are overridden before global binding
1 parent a1df4c3 commit b468a6a

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/preload.js

+29-27
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,42 @@ function addPreload(p5, fn, lifecycles) {
66
'loadStrings': () => [],
77
'loadFont': (pInst) => new p5.Font(pInst, new FontFace('default', 'default.woff')),
88
};
9+
10+
p5.isPreloadSupported = function() {
11+
return true;
12+
};
913

1014
const promises = [];
15+
const prevMethods = {};
1116

12-
lifecycles.presetup = async function() {
13-
if (!window.preload) return;
14-
15-
const promises = [];
16-
const prevMethods = {};
17-
18-
// Override existing methods to return an object immediately,
19-
// and keep track of all things being loaded
20-
for (const method in methods) {
21-
const prevMethod = fn[method];
22-
prevMethods[method] = prevMethod;
17+
// Override existing methods to return an object immediately,
18+
// and keep track of all things being loaded
19+
for (const method in methods) {
20+
const prevMethod = fn[method];
21+
prevMethods[method] = prevMethod;
2322

24-
fn[method] = function(...args) {
25-
const obj = methods[method](this);
26-
const promise = prevMethod.apply(this, args).then((result) => {
27-
for (const key in result) {
28-
obj[key] = result[key];
29-
}
30-
});
31-
promises.push(promise);
32-
return obj;
23+
fn[method] = function(...args) {
24+
if (!this._isInPreload) {
25+
return prevMethod.apply(this, args);
3326
}
27+
const obj = methods[method](this);
28+
const promise = prevMethod.apply(this, args).then((result) => {
29+
for (const key in result) {
30+
obj[key] = result[key];
31+
}
32+
});
33+
promises.push(promise);
34+
return obj;
3435
}
35-
36+
}
37+
38+
lifecycles.presetup = async function() {
39+
if (!window.preload) return;
40+
41+
this._isInPreload = true;
3642
window.preload();
37-
38-
// Restore previous functionality
39-
for (const method in prevMethods) {
40-
fn[method] = prevMethods[method];
41-
}
42-
43+
this._isInPreload = false;
44+
4345
// Wait for everything to load before letting setup run
4446
await Promise.all(promises);
4547
}

0 commit comments

Comments
 (0)