Skip to content

[2.0] Add a way to resolve addon ordering dependencies #7797

Open
@davepagurek

Description

@davepagurek

Increasing access

Sometimes addons build off of other addons, or want to be compatible with other addons. Currently, every addon lifecycle stage will run addons' callbacks in the order that the callbacks were registered. This means that users might run into problems if they accidentally include the script tags for those addons in the wrong order. If addons have a way of describing their dependencies as they are registered, then there are less potential gotchas for users of those addons.

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

Feature request details

An example of a situation where this might occur: addon A creates a new object in presetup, and addon B wants to do something with that object also in presetup. Addon B would need a way to always have its presetup run after addon A's presetup.

To make this work, ideally, addons have a way to name themselves so that other developers can describe dependencies relative to them. For example, imagine you have a physics engine that updates the positions of all objects predraw. It could register itself with a name:

p5.registerAddon(preloadAddon, { name: 'physicsEngine' });

Then, let's say you make an addon that adds some extra gravity forces to the physics system. You also want it to run before the user's code in predraw, but it needs to run before the physics engine applies the updates. You could then register this ordering dependency with before or after:

p5.registerAddon(gravityAddon, { name: 'gravity', before: 'physicsEngine' });

So the new API for registerAddon would be something like:

function registerAddon(
  callback: Function
  options?: {
    name?: string
    // These could be a single string or an array if there are multiple dependencies
    before?: string | string[]
    after?: string | string[]
  }
)

Internally, when initializing p5, we would then:

  • build a graph of the dependencies (each before/after dependency adds an edge)
  • come up with an ordering for all addons (via e.g. depth-first traversal of the graph)
  • sort all lifecycle hook callbacks based on that ordering

This before/after syntax isn't the only approach to getting an ordering, but I like that you only need to know the name of the other addon. I'm open to other ideas though!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Open for Discussion

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions