Skip to content

Add "init" hooks or similar functionality to preload.js #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
quinton-ashley opened this issue May 8, 2025 · 2 comments
Open

Add "init" hooks or similar functionality to preload.js #25

quinton-ashley opened this issue May 8, 2025 · 2 comments

Comments

@quinton-ashley
Copy link

quinton-ashley commented May 8, 2025

Some addons made for p5 v1, like p5play, need "init" hooks or equivalent functionality: getting access to the instance of p5 and being able to add to it before preload runs, as discussed in this feature request: processing/p5.js#7742.

In conclusion though, it's fair to say that "init" lifecycle hooks only have the potential to be useful if an addon relies on the preload.js compatibility addon, so its more appropriate that similar functionality be added to it instead of p5 v2.

@ksen0 My workaround of including and making custom modifications to the preload.js compat addon in p5play to achieve this is hacky and shouldn't be encouraged, since it could easily result in addons being incompatible with one another as pointed out by Limzy.
processing/p5.js#7798 (comment)

EDIT: You can see what I'm currently doing here:
https://github.com/quinton-ashley/p5play/blob/48342bae3594a0703f24d1bbc07346ee45b185d3/p5play.js#L10975

@davepagurek came up with a good related feature, but it doesn't fix this issue.
processing/p5.js#7797

I'm thinking preload.js could include a p5.registerInitMethod or p5.registerInitHook function.

And then init hooks would run before preload is run within the "presetup" hook in preload.js.

I can make a PR if desired!

@davepagurek
Copy link
Collaborator

To be clear on the difference between this and #7797, would this be primarily for backwards compatibility with addons designed for v1? If so, would it be helpful to bring back the p5.prototype.registerMethod() function and support for the old lifecycle states in the compatibility addon so that no code changes would be required in those community libraries?

@quinton-ashley
Copy link
Author

That could be helpful, as long as it's deemed fit to include in preload.js, if not then no.

But code changes probably would still be required for most p5 v1 addons, including ml5, because preload.js doesn't add _incrementPreload and _decrementPreload functions. (Note that p5.sound does a feature check to see if those functions exist or not.) You can see how I implemented them in q5-core though.

That's a separate issue though, I'm not requesting a higher degree of backwards compatibility other than "init" hook support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants