p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Hook just before global mode instantiation

Open limzykenneth opened this issue 5 years ago • 4 comments

Nature of issue?

  • [ ] Found a bug
  • [ ] Existing feature enhancement
  • [x] New feature request

Most appropriate sub-area of p5.js?

  • [ ] Color
  • [x] Core/Environment/Rendering
  • [ ] Data
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Other (specify if possible)

New feature details:

For addon libraries, is there a hook that will execute before global mode p5 is instantiated? If not can one be added?

The use case is that I'm fiddling around with some async code that when resolve will attach functions to the p5.prototype but since it is async the global mode p5 will have instantiated before it is completed so the global mode p5 sketch won't have that function attached yet.

I can use instance mode and delay the call to new p5(sketch) but for an addon library it should work out of the box, so I'm thinking can an optional hook be added just before global mode is instantiated that the async addon library can call when it is done loading to trigger global mode?

One idea comes from core/init.js

// if the page is ready, initialize p5 immediately
if (document.readyState === 'complete') {
  _globalInit();
  // if the page is still loading, add an event listener
  // and initialize p5 as soon as it finishes loading
} else {
  window.addEventListener('load', _globalInit, false);
}

Instead of checking for readyState, check for a static function on the p5 object and if it's null check readyState as usual, if not wait for it to resolve (assuming it should return a Promise) then run _globalInit().

limzykenneth avatar Dec 21 '19 21:12 limzykenneth

@limzykenneth @stalgiag , Can I work on this issue?Could you please tell how to approach to implement this new feature?

singhvisha avatar Dec 31 '19 02:12 singhvisha

@limzykenneth @stalgiag , Can I work on this issue?Could you please tell how to approach to implement this new feature?

singhvisha avatar Jan 02 '20 08:01 singhvisha

I'm gauging interest and potential problems of this feature with this issue so I'm not looking for someone in particular to implement this as I have a pretty good idea of how to implement this myself. Thanks for the interest though.

PS. It's the holiday period so it may take longer for us to reply to a message, there's no need to ping us again.

limzykenneth avatar Jan 02 '20 13:01 limzykenneth

small note that i think some work in #4233 will help set the stage for this infrastructure

outofambit avatar Feb 24 '20 23:02 outofambit

Closing, as with the current implementation of p5.js 2.0 this is no longer necessary.

PS: This message is part of cleaning up the p5.js 2.0 roadmap prior to release, to make outstanding more more approachable for new contributors. You can join the conversation about this new version on this Discourse thread or this GitHub thread!

ksen0 avatar Apr 16 '25 07:04 ksen0