Skip to content

Hook just before global mode instantiation #4204

Closed
@limzykenneth

Description

@limzykenneth

Nature of issue?

  • Found a bug
  • Existing feature enhancement
  • New feature request

Most appropriate sub-area of p5.js?

  • Color
  • 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().

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions