@@ -77,6 +77,25 @@ class p5 {
7777 // ensure correct reporting of window dimensions
7878 this . _updateWindowSize ( ) ;
7979
80+ // Apply addon defined decorations
81+ for ( const [ patternArray , decoration ] of p5 . decorations ) {
82+ for ( const member in p5 . prototype ) {
83+ // Member must be a function
84+ if ( typeof p5 . prototype [ member ] !== 'function' ) continue ;
85+
86+ if ( ! patternArray . some ( pattern => {
87+ if ( typeof pattern === 'string' ) {
88+ return pattern === member ;
89+ } else if ( pattern instanceof RegExp ) {
90+ return pattern . test ( member ) ;
91+ }
92+ } ) ) continue ;
93+
94+ const copy = p5 . prototype [ member ] . bind ( this ) ;
95+ p5 . prototype [ member ] = decoration . call ( this , copy , member ) ;
96+ }
97+ }
98+
8099 const bindGlobal = createBindGlobal ( this ) ;
81100 // If the user has created a global setup or draw function,
82101 // assume "global" mode and make everything global (i.e. on the window)
@@ -145,22 +164,7 @@ class p5 {
145164 static registerAddon ( addon ) {
146165 const lifecycles = { } ;
147166
148- // Create a proxy for p5.prototype that automatically re-binds globals when properties are added
149- const prototypeProxy = new Proxy ( p5 . prototype , {
150- set ( target , property , value ) {
151- const result = Reflect . set ( target , property , value ) ;
152-
153- // If we have a global instance and this is a new property, bind it globally
154- if ( p5 . instance && p5 . instance . _isGlobal && property [ 0 ] !== '_' ) {
155- const bindGlobal = createBindGlobal ( p5 . instance ) ;
156- bindGlobal ( property ) ;
157- }
158-
159- return result ;
160- }
161- } ) ;
162-
163- addon ( p5 , prototypeProxy , lifecycles ) ;
167+ addon ( p5 , p5 . prototype , lifecycles ) ;
164168
165169 const validLifecycles = Object . keys ( p5 . lifecycleHooks ) ;
166170 for ( const name of validLifecycles ) {
@@ -170,6 +174,13 @@ class p5 {
170174 }
171175 }
172176
177+ static decorations = new Map ( ) ;
178+ static decorateHelper ( pattern , decoration ) {
179+ let patternArray = pattern ;
180+ if ( ! Array . isArray ( pattern ) ) patternArray = [ pattern ] ;
181+ p5 . decorations . set ( patternArray , decoration ) ;
182+ }
183+
173184 #customActions = { } ;
174185 _customActions = new Proxy ( { } , {
175186 get : ( target , prop ) => {
@@ -393,28 +404,9 @@ class p5 {
393404 }
394405
395406 async _runLifecycleHook ( hookName ) {
396- // Create a proxy for the instance that automatically re-binds globals when
397- // properties are added over the course of the lifecycle.
398- // Afterward, we skip global binding for efficiency.
399- let inLifecycle = true ;
400- const instanceProxy = new Proxy ( this , {
401- set ( target , property , value ) {
402- const result = Reflect . set ( target , property , value ) ;
403-
404- // If this is a global instance and this is a new property, bind it globally
405- if ( inLifecycle && target . _isGlobal && property [ 0 ] !== '_' ) {
406- const bindGlobal = createBindGlobal ( target ) ;
407- bindGlobal ( property ) ;
408- }
409-
410- return result ;
411- }
412- } ) ;
413-
414407 await Promise . all ( p5 . lifecycleHooks [ hookName ] . map ( hook => {
415- return hook . call ( instanceProxy ) ;
408+ return hook . call ( this ) ;
416409 } ) ) ;
417- inLifecycle = false ;
418410 }
419411
420412 _initializeInstanceVariables ( ) {
@@ -480,7 +472,7 @@ function createBindGlobal(instance) {
480472 Object . defineProperty ( window , property , {
481473 configurable : true ,
482474 enumerable : true ,
483- value : boundFunction ,
475+ value : boundFunction
484476 } ) ;
485477 } else {
486478 Object . defineProperty ( window , property , {
@@ -498,7 +490,7 @@ function createBindGlobal(instance) {
498490 Object . defineProperty ( window , property , {
499491 configurable : true ,
500492 enumerable : true ,
501- value : constantValue ,
493+ value : constantValue
502494 } ) ;
503495 } else {
504496 Object . defineProperty ( window , property , {
@@ -785,8 +777,6 @@ for (const k in constants) {
785777 * </code>
786778 * </div>
787779 */
788- p5 . disableFriendlyErrors = false ;
789-
790780import transform from './transform' ;
791781import structure from './structure' ;
792782import environment from './environment' ;
0 commit comments