Conversation
… tag The details tag held the explanation of why arrow functions are wrong here, but the anti-example itself sat outside it. With the details collapsed a reader saw two code blocks and nothing marking the first as the one not to copy.
Covers both kinds: per-sketch state defaulted on fn and written through this, and renderer state written with states.setValue() so that push() and pop() can restore it. Notes why a variable in the addon function's scope is shared across sketches, and why assigning to states directly leaks past the pop() that should undo it.
|
🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors! Thank You! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #9171
I have used "addresses" rather than "resolves" deliberately, because one of the three points in the issue cannot be fixed in this repo. Details below.
Changes:
Moving the anti-example inside the
detailstag. Thedetailstag held the explanation of why arrow functions do not work here, but the anti-example itself sat outside it. With the section collapsed, which is how a reader first sees it, you got two code blocks in sequence with nothing marking the first as the one not to copy, and the wrong one came first. Moving the closing tag down puts the example with its explanation.A new "Adding state to your addon" section. This covers both kinds of state. For the kind
push()andpop()leave alone, it shows a default onfnwith writes throughthis, and explains why a variable in the addon function's own scope is not the same thing: that function runs once at registration, so two sketches on a page would share it. For the kindpush()andpop()restore, it showsthis._renderer.states.setValue()and explains why assigning directly leaks past thepop()that should undo it, which is thatsetValueis what records the previous value into the diff thatpop()reads back.I put the section with the other reference material after "Accessing custom actions" rather than inside the numbered steps, so the build-along tutorial stays unbroken.
On the first point in the issue, the jsx comments. I think the source here is already correct and the problem is one layer out. In
contributor_docs/creating_libraries.mdthose are written as real HTML comments at lines 86, 91 and 94, inside a fencedhtmlblock. What turns them into{/* Other tags */}on the live site is the website build:p5.js-website/src/scripts/builders/contribute.tshasconvertMarkdownCommentsToMDX, which rewrites every match of/<!--([\S\s]*?)-->/gso MDX does not choke on HTML comments. It runs over the whole document with no awareness of fenced code blocks, so an example that legitimately contains an HTML comment gets rewritten along with the real ones. So there is nothing to change here for that point, and the fix wants to go to p5.js-website. Happy to open that separately.One thing I could not work out, and would like your steer on. For the push/pop half I have documented reading and writing an existing key with
setValue(), since that is what the diff relies on. What I could not establish is how an addon should register a new key with a default.Renderer.statesis a static that each renderer copies at construction (p5.Renderer.js:93), so extending it would have to happen before any renderer is created, and I could not find an addon doing that anywhere. Is that supported, or should addon drawing state live somewhere else? Happy to add that to the section once I know.Opening as a draft per your suggestion, mostly for that question.
AI disclosure
I used Claude Code assistively on this one. It helped me trace how
States,push()andpop()fit together in the source, and it drafted wording for the new section which I then read through and edited before submitting. The behaviour the section describes is checked againstsrc/core/States.jsandsrc/core/p5.Renderer.jsrather than assumed, and I take responsibility for what is here. Happy to rework any of the wording.PR Checklist
npm run lintpassesContributor docs only, so no reference entry or unit test applies.
npm testpasses locally as well (2387 passed).