From 2ba0bcd594b8428aa1264025ec789f13c39e58be Mon Sep 17 00:00:00 2001 From: Quinton Ashley Date: Fri, 25 Apr 2025 09:00:09 -0500 Subject: [PATCH 1/2] Add "init" lifecycle hook Added two lines of code that implement the "init" lifecycle hook in p5 v2. This is needed for preload compatibility. --- src/core/main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/main.js b/src/core/main.js index 75d10c7398..694e26619d 100644 --- a/src/core/main.js +++ b/src/core/main.js @@ -36,6 +36,7 @@ class p5 { // global mode. static instance = null; static lifecycleHooks = { + init: [], presetup: [], postsetup: [], predraw: [], @@ -138,6 +139,8 @@ class p5 { bindGlobal(p); } + this._runLifecycleHook('init'); + const protectedProperties = ['constructor', 'length']; // Attach its properties to the window for (const p in this) { From 693f682d91e94802ca98ede9220eb40100d92967 Mon Sep 17 00:00:00 2001 From: Quinton Ashley Date: Sun, 27 Apr 2025 06:38:19 -0500 Subject: [PATCH 2/2] Add _runLifecycleHookSync and use it to run "init" hooks --- src/core/main.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/main.js b/src/core/main.js index 694e26619d..93d634198c 100644 --- a/src/core/main.js +++ b/src/core/main.js @@ -139,7 +139,7 @@ class p5 { bindGlobal(p); } - this._runLifecycleHook('init'); + this._runLifecycleHookSync('init'); const protectedProperties = ['constructor', 'length']; // Attach its properties to the window @@ -425,11 +425,17 @@ class p5 { } async _runLifecycleHook(hookName) { - for(const hook of p5.lifecycleHooks[hookName]){ + for (const hook of p5.lifecycleHooks[hookName]) { await hook.call(this); } } + _runLifecycleHookSync(hookName) { + for (const hook of p5.lifecycleHooks[hookName]) { + hook.call(this); + } + } + _initializeInstanceVariables() { this._accessibleOutputs = { text: false,