Skip to content

Add stateful helpers for p5.strands addons - #9309

Open
bhabishnu wants to merge 5 commits into
processing:mainfrom
bhabishnu:feat/strands-registered-state-9259-docs
Open

bhabishnu wants to merge 5 commits into
processing:mainfrom
bhabishnu:feat/strands-registered-state-9259-docs

Conversation

@bhabishnu

Copy link
Copy Markdown
Contributor

Resolves #9259

Changes:

This lets addons create Strands helpers that keep track of their shader values through if/else branches and for loops, rather than having to manage that state themselves.

p5.registerStrandsState() returns a factory for independent helpers with read and update methods. Helpers can keep scoped values that pushStrandsState() and popStrandsState() restore, alongside global values that stay updated.

The optional globalInstance option lets an addon expose one helper’s methods for the duration of a build. This reuses the temporary-property cleanup, which now restores the original properties after both successful and failed builds.

As discussed in the issue, caught errors from hook callbacks or begin()/end() stop the build before code generation and preserve the original thrown value. Errors caught between manual begin() and end() calls are outside that protection.

Addon documentation and two runnable examples are included in contributor_docs/p5.strands.md.

Tested locally:

  • WebGL shader tests: 224 passed.
  • WebGPU shader tests: 82 passed, 3 todo, with no fallback.
  • Lint passes with the same 44 existing warnings.
  • The documentation build and both examples pass.

Screenshots of the change:

No UI changes. The tests check rendered pixels on WebGL and WebGPU.

PR Checklist

  • npm run lint passes
  • [Unit tests] are included / updated

AI disclosure: AI tools were used for tests and documentation. I reviewed and approved the decisions, and tested the changes locally.

Journal each temporary install on window, p5.prototype and
p5.Graphics.prototype with its previous property descriptor, and restore
the journal newest first after the build. Absent properties are removed
again and setters never run. A property that can be neither redefined
nor assigned is rejected before it is written, and a failed restore never
replaces a build's own error.

This also restores options.hook properties onto p5.Graphics.prototype
rather than p5.Graphics, and a modify() called from inside another
build's callback hands the enclosing journal back.

Part of processing#9259.
Add p5.registerStrandsState(name, { init, update, read }), which returns
a factory for helpers used inside hooks. Helper state is merged across
if/else branches and carried through for loops, and
pushStrandsState()/popStrandsState() save and restore every helper's
scoped fields.

Uses that cannot work throw clear errors: a helper used outside the hook
call that created it, created inside a branch or loop, or used in a loop
header; break or early return in a loop body while helpers exist;
updates inside ?:, &&, ||, &&= or ||= operands; changing a state's
shape; and unbalanced push/pop.

The addon API is documented in contributor_docs/p5.strands.md.

Part of processing#9259.
If a hook throws and sketch code catches the error inside modify(), the
first failure is rethrown as soon as the callback returns, before code
generation. The helper scope is cleared so the next build starts clean,
and finishHook's "requires a value" check now runs inside the same
cleanup.

The contributor doc notes this behaviour.

Part of processing#9259.
A base shader can pass globalInstance next to hook in modify()'s
options. It is called after that hook's begin() with the sketch as
`this`, and the helper it returns has its methods installed with
_installGlobal() on window, p5.prototype and p5.Graphics.prototype for
the rest of the build, through the temporary install journal.

The contributor doc covers options.hook, globalInstance, _installGlobal()
and the temporary replacement and restoration of names.

Part of processing#9259.
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

Successfully merging this pull request may close these issues.

Implicit state handling for p5.strands addons

1 participant