@@ -57,11 +57,6 @@ if (typeof IS_MINIFIED !== 'undefined') {
57
57
const dataDoc = require ( '../../docs/parameterData.json' ) ;
58
58
const arrDoc = JSON . parse ( JSON . stringify ( dataDoc ) ) ;
59
59
60
- const constantsReverseMap = { } ;
61
- for ( let key in constants ) {
62
- constantsReverseMap [ constants [ key ] ] = key ;
63
- }
64
-
65
60
// -- Borrowed from jQuery 1.11.3 --
66
61
const class2type = { } ;
67
62
const toString = class2type . toString ;
@@ -90,24 +85,6 @@ if (typeof IS_MINIFIED !== 'undefined') {
90
85
91
86
// -- End borrow --
92
87
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
-
111
88
const friendlyWelcome = ( ) => {
112
89
// p5.js brand - magenta: #ED225D
113
90
//const astrixBgColor = 'transparent';
@@ -290,6 +267,36 @@ if (typeof IS_MINIFIED !== 'undefined') {
290
267
undefined : true
291
268
} ;
292
269
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
+
293
300
const argumentTree = { } ;
294
301
// The following two functions are responsible for querying and inserting
295
302
// into the argument tree. It stores the types of arguments that each
@@ -343,31 +350,38 @@ if (typeof IS_MINIFIED !== 'undefined') {
343
350
return obj ;
344
351
}
345
352
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 ] = { } ;
351
366
}
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 ] = { } ) ;
356
372
return obj ;
357
373
}
358
374
}
359
375
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 ] ;
367
382
return obj ;
368
383
}
369
384
}
370
-
371
385
// nothing worked, put the type as is
372
386
obj = obj [ type ] || ( obj [ type ] = { } ) ;
373
387
}
0 commit comments