Skip to content

[2.0] Document workarounds for inter-addon dependencies #7798

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
davepagurek opened this issue May 7, 2025 · 9 comments
Open

[2.0] Document workarounds for inter-addon dependencies #7798

davepagurek opened this issue May 7, 2025 · 9 comments

Comments

@davepagurek
Copy link
Contributor

davepagurek commented May 7, 2025

Topic

There are some scenarios right now where you might want to have multiple addons in the same sketch. Sometimes, if those addons depend on each other, then the ordering of your addon script tags can affect whether or not the sketch runs correctly.

I proposed a potential API change in #7797 to help resolve these issues, but in the mean time, there are some workarounds that we can document.

  • For end users: You can rearrange your script tags to put them in the correct order. There are slightly more involved instructions for doing this in OpenProcessing since you'd have to switch your sketch to HTML mode.
  • For addon developers: @quinton-ashley had a good idea, and that is, bundling all of an addon's dependencies into one file. e.g. if your addon depends on the preload addon existing and running after your addon, then it could make sense to just include the preload code after your code in your bundled js file.

This documentation could live in https://github.com/processing/p5.js/blob/dev-2.0/contributor_docs/creating_libraries.md but also that document needs to be updated for 2.0 as well.

@ksen0
Copy link
Member

ksen0 commented May 7, 2025

This issue is actively looking for a contributor!
If you're interested but not familiar with the add-on library system in 2.0, you can also check out this article

cc @perminder-17

@VANSH3104
Copy link
Contributor

@ksen0 can i give it a try?

@ksen0
Copy link
Member

ksen0 commented May 8, 2025

@VANSH3104 please do! This is specific to dev-2.0 branch only. Feel free to ping with any questions

@VANSH3104
Copy link
Contributor

VANSH3104 commented May 8, 2025

@ksen0 i added this is this correct or need to change it or make a pr for this

Handling Addon Dependencies

When creating addons that depend on other addons or are used alongside them, you need to consider execution order and dependency management. Here are solutions for both addon developers and users:

For Addon Developers

Solution 1: Bundle Dependencies
For small, critical dependencies, include them directly in your addon:

// p5.combinedAddon.js
const combinedAddon = function(p5, fn, lifecycles) {
  // Bundled dependency code
  fn._preloadHelper = function() {
    // ...dependency logic...
  };

  // Main addon code
  fn.myFeature = function() {
    this._preloadHelper();
    // ...main feature logic...
  };
};

p5.registerAddon(combinedAddon);

Solution 2: Runtime Checks
For larger dependencies, verify their presence

const myAddon = function(p5, fn, lifecycles) {
  fn.initMyAddon = function() {
    if (typeof this.requiredFeature !== 'function') {
      console.error('Error: Please load required-addon.js before my-addon.js');
      return null;
    }
    // Safe to proceed
    return this.requiredFeature();
  };
};

p5.registerAddon(myAddon);

For End Users

Loading Order Solution
Always load dependency addons first:

<head>
  <script src="p5.js"></script>
  <script src="dependency-addon.js"></script> <!-- Load first -->
  <script src="main-addon.js"></script>     <!-- Then dependent addon -->
</head>

In Web Editors (OpenProcessing, p5 Web Editor):

  • Switch to "HTML mode"
  • Manually reorder script tags
  • Ensure dependencies load before addons that need them

@limzykenneth
Copy link
Member

I am in the middle of updating https://github.com/processing/p5.js/blob/dev-2.0/contributor_docs/creating_libraries.md for 2.0 and has not finished yet, shall I open a WIP PR for the progress instead?

@VANSH3104 It seems the example you shared above are still using the old addon syntax instead of the 2.x one?

@VANSH3104
Copy link
Contributor

@limzykenneth can you check it now ?

@limzykenneth
Copy link
Member

@VANSH3104 I think the proposed bundling solution code is likely not the practical way addon authors would want to do this as it involve manually copying the code from a different addon. This might be more practical to achieve with a build tool based solution but that involves its own set of complexity as well. It is also redistributing a third party addon which can have its own problems separate from the technical considerations. @davepagurek Do you have some idea around here? I think the runtime check is a relatively good solution so far and the error message logging can also led nicely to consideration around exposing FES API to addon authors which we were thinking of before already.

I'm almost done with the edits to the creating libraries contributor docs and will open a PR later today or more likely tomorrow. We can also work out details on this there.

@VANSH3104
Copy link
Contributor

@limzykenneth Understood! I’ll be waiting for your PR. Please let me know if there’s anything else you need from my side.

@quinton-ashley
Copy link
Contributor

quinton-ashley commented May 8, 2025

I think the proposed bundling solution code is likely not the practical way addon authors would want to do this as it involve manually copying the code from a different addon.

@limzykenneth Yeah I concur.

If let's say p5play and ml5 both implemented this workaround hack of including the preload.js compatibility addon, and users wanted to use both libraries, ml5's inclusion of preload support after p5play's would override p5play's edited version that initializes instance props to the global scope and p5play would fail to load.

So I agree with Limzy that my workaround shouldn't be documented or encouraged. Effort would be better spent developing a more robust solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Ready for Work
Status: Ready for Work
Development

No branches or pull requests

5 participants