Skip to content

Commit 8226395

Browse files
committed
make code more readable and add comments
1 parent 97e2c8a commit 8226395

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

src/core/error_helpers.js

+54-40
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ if (typeof IS_MINIFIED !== 'undefined') {
5757
const dataDoc = require('../../docs/parameterData.json');
5858
const arrDoc = JSON.parse(JSON.stringify(dataDoc));
5959

60-
const constantsReverseMap = {};
61-
for (let key in constants) {
62-
constantsReverseMap[constants[key]] = key;
63-
}
64-
6560
// -- Borrowed from jQuery 1.11.3 --
6661
const class2type = {};
6762
const toString = class2type.toString;
@@ -90,24 +85,6 @@ if (typeof IS_MINIFIED !== 'undefined') {
9085

9186
// -- End borrow --
9287

93-
// for when p5 classes are specified as arguments of functions
94-
// for e.g.: background(color) , where color is a p5.Color object
95-
const p5Types = [];
96-
const p5TypesNames = [];
97-
const funcSpecificp5Types = {};
98-
const funcSpecificp5Names = {};
99-
window.addEventListener('load', () => {
100-
// Make a list of all p5 classes to be used for argument validation
101-
// This must be done only when everything has loaded otherwise we get
102-
// an empty array
103-
for (let key of Object.keys(p5)) {
104-
if (typeof p5[key] === 'function' && key[0] !== key[0].toLowerCase()) {
105-
p5Types.push(p5[key]);
106-
p5TypesNames.push(key);
107-
}
108-
}
109-
});
110-
11188
const friendlyWelcome = () => {
11289
// p5.js brand - magenta: #ED225D
11390
//const astrixBgColor = 'transparent';
@@ -290,6 +267,36 @@ if (typeof IS_MINIFIED !== 'undefined') {
290267
undefined: true
291268
};
292269

270+
// reverse map of all constants
271+
const constantsReverseMap = {};
272+
for (let key in constants) {
273+
constantsReverseMap[constants[key]] = key;
274+
}
275+
276+
// mapping names of p5 types to their constructor function
277+
// p5Constructors:
278+
// - Color: f()
279+
// - Graphics: f()
280+
// - Vector: f()
281+
// and so on
282+
const p5Constructors = {};
283+
284+
// For speedup over many runs. funcSpecificConstructors[func] only has the
285+
// constructors for types which were seen earlier as args of "func"
286+
const funcSpecificConstructors = {};
287+
window.addEventListener('load', () => {
288+
// Make a list of all p5 classes to be used for argument validation
289+
// This must be done only when everything has loaded otherwise we get
290+
// an empty array
291+
for (let key of Object.keys(p5)) {
292+
// Get a list of all constructors in p5. They are functions whose names
293+
// start with a capital letter
294+
if (typeof p5[key] === 'function' && key[0] !== key[0].toLowerCase()) {
295+
p5Constructors[key] = p5[key];
296+
}
297+
}
298+
});
299+
293300
const argumentTree = {};
294301
// The following two functions are responsible for querying and inserting
295302
// into the argument tree. It stores the types of arguments that each
@@ -343,31 +350,38 @@ if (typeof IS_MINIFIED !== 'undefined') {
343350
return obj;
344351
}
345352

346-
let p5T = funcSpecificp5Types[func];
347-
let p5TN = funcSpecificp5Names[func];
348-
if (p5T === undefined) {
349-
p5T = funcSpecificp5Types[func] = [];
350-
p5TN = funcSpecificp5Names[func] = [];
353+
// constructors for types defined in p5 do not have a name property.
354+
// e.constructor.name gives "". Code in this segment is a workaround for it
355+
356+
// p5C will only have the name: constructor mapping for types
357+
// which were already seen as args of "func"
358+
let p5C = funcSpecificConstructors[func];
359+
// p5C would contain much fewer items than p5Constructors. if we find our
360+
// answer in p5C, we don't have to scan through p5Constructors
361+
362+
if (p5C === undefined) {
363+
// if there isn't an entry yet for func
364+
// make an entry of empty object
365+
p5C = funcSpecificConstructors[func] = {};
351366
}
352-
for (let i = 0, len = p5T.length; i < len; ++i) {
353-
// search on the classes we have already seen
354-
if (value instanceof p5T[i]) {
355-
obj = obj[p5TN[i]] || (obj[p5TN[i]] = {});
367+
368+
for (let key in p5C) {
369+
// search on the constructors we have already seen (smaller search space)
370+
if (value instanceof p5C[key]) {
371+
obj = obj[key] || (obj[key] = {});
356372
return obj;
357373
}
358374
}
359375

360-
for (let i = 0, len = p5Types.length; i < len; ++i) {
361-
// if the above search didn't work, search on all p5 classes
362-
if (value instanceof p5Types[i]) {
363-
obj = obj[p5TypesNames[i]] || (obj[p5TypesNames[i]] = {});
364-
// if found, add to known classes for this function
365-
p5T.push(p5Types[i]);
366-
p5TN.push(p5TypesNames[i]);
376+
for (let key in p5Constructors) {
377+
// if the above search didn't work, search on all p5 constructors
378+
if (value instanceof p5Constructors[key]) {
379+
obj = obj[key] || (obj[key] = {});
380+
// if found, add to known constructors for this function
381+
p5C[key] = p5Constructors[key];
367382
return obj;
368383
}
369384
}
370-
371385
// nothing worked, put the type as is
372386
obj = obj[type] || (obj[type] = {});
373387
}

0 commit comments

Comments
 (0)