Skip to content

Commit 896d2e9

Browse files
authored
Merge pull request #7542 from aferriss/adam/preload-fes
Add warning when using preload function
2 parents 0b5b745 + d59f69b commit 896d2e9

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/core/friendly_errors/fes_core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ function fesCore(p5, fn){
8282
const entryPoints = [
8383
'setup',
8484
'draw',
85-
'preload',
8685
'deviceMoved',
8786
'deviceTurned',
8887
'deviceShaken',
@@ -295,6 +294,10 @@ function fesCore(p5, fn){
295294
context = instanceMode ? context : window;
296295
const fnNames = entryPoints;
297296

297+
if (context.preload) {
298+
p5._friendlyError(translator('fes.preloadDisabled'), 'preload');
299+
}
300+
298301
const fxns = {};
299302
// lowercasename -> actualName mapping
300303
fnNames.forEach(symbol => {

src/core/friendly_errors/param_validator.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function validateParams(p5, fn, lifecycles) {
352352
* @param {String} func - Name of the function. Expect global functions like `sin` and class methods like `p5.Vector.add`
353353
* @returns {String} The friendly error message.
354354
*/
355-
fn.friendlyParamError = function (zodErrorObj, func) {
355+
fn.friendlyParamError = function (zodErrorObj, func, args) {
356356
let message = '🌸 p5.js says: ';
357357
// The `zodErrorObj` might contain multiple errors of equal importance
358358
// (after scoring the schema closeness in `findClosestSchema`). Here, we
@@ -398,6 +398,11 @@ function validateParams(p5, fn, lifecycles) {
398398
});
399399

400400
if (expectedTypes.size > 0) {
401+
if (error.path?.length > 0 && args[error.path[0]] instanceof Promise) {
402+
message += 'Did you mean to put `await` before a loading function? ' +
403+
'An unexpected Promise was found. ';
404+
}
405+
401406
const expectedTypesStr = Array.from(expectedTypes).join(' or ');
402407
const position = error.path.join('.');
403408

@@ -502,7 +507,7 @@ function validateParams(p5, fn, lifecycles) {
502507
} catch (error) {
503508
const closestSchema = fn.findClosestSchema(funcSchemas, args);
504509
const zodError = closestSchema.safeParse(args).error;
505-
const errorMessage = fn.friendlyParamError(zodError, func);
510+
const errorMessage = fn.friendlyParamError(zodError, func, args);
506511

507512
return {
508513
success: false,

test/unit/core/param_errors.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ suite('Validate Params', function () {
77
Color: function () {
88
return 'mock p5.Color';
99
},
10+
Image: function() {
11+
return 'mock p5.Image';
12+
},
13+
Element: function() {
14+
return 'mock p5.Element';
15+
},
16+
Texture: function() {
17+
return 'mock p5.Texture';
18+
},
19+
Framebuffer: function() {
20+
return 'mock p5.Framebuffer';
21+
},
22+
FramebufferTexture: function() {
23+
return 'mock p5.FramebufferTexture';
24+
},
1025
};
1126
const mockP5Prototype = {};
1227

@@ -102,6 +117,17 @@ suite('Validate Params', function () {
102117
});
103118
});
104119

120+
suite('validateParams: promise where no promise is expected', function () {
121+
test('image(): promise for first argument', function () {
122+
const result = mockP5Prototype.validate('p5.image', [Promise.resolve(), 0, 0]);
123+
console.log(result);
124+
assert.equal(
125+
result.error,
126+
'🌸 p5.js says: Did you mean to put `await` before a loading function? An unexpected Promise was found. Expected Image or Element or Texture or Framebuffer or FramebufferTexture at the first parameter in p5.image().'
127+
);
128+
});
129+
});
130+
105131
suite('validateParams: class, multi-types + optional numbers', function () {
106132
test('ambientLight(): no firendly-err-msg', function () {
107133
const result = mockP5Prototype.validate('p5.ambientLight', [new mockP5.Color()]);

translations/en/translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"misspelling": "{{location}} It seems that you may have accidentally written \"{{name}}\" instead of \"{{actualName}}\". Please correct it to {{actualName}} if you wish to use the {{type}} from p5.js.",
4848
"misspelling_plural": "{{location}} It seems that you may have accidentally written \"{{name}}\".\nYou may have meant one of the following: \n{{suggestions}}",
4949
"misusedTopLevel": "Did you just try to use p5.js's {{symbolName}} {{symbolType}}? If so, you may want to move it into your sketch's setup() function.\n\n+ More info: {{url}}",
50+
"preloadDisabled": "The preload() function has been removed in p5.js 2.0. Please load assets in setup() using async / await keywords or callbacks instead. See https://dev.to/limzykenneth/asynchronous-p5js-20-458f for more information.",
5051
"positions": {
5152
"p_1": "first",
5253
"p_10": "tenth",

0 commit comments

Comments
 (0)