Create documentation for decorators in p5.js - #9118
Draft
ksen0 wants to merge 2 commits into
Draft
Conversation
Added guide on using decorators in p5.js
limzykenneth
left a comment
Member
There was a problem hiding this comment.
Just a few comments, this can be further expanded as we move it along so not all of them need to be accepted.
|
|
||
| ## When and Why to Use a Decorator | ||
|
|
||
| Decorators are a design pattern for having **one place** where repeated logic is maintained (in our example, the [vector parameter validation](https://github.com/processing/p5.js/blob/522b89ecc85e6ba4442ba40c69d96c6a5d00c839/src/math/patch-vector.js#L85)), but it is still applied in multiple files. The purpose is avoiding duplicate code, because: |
Member
There was a problem hiding this comment.
It is also a way to change or supplement existing code without needing to know how the existing code is implemented and also not needing to modify existing code.
Comment on lines
+48
to
+62
| const wasInternalCall = this._isUserCall; | ||
| this._isUserCall = true; | ||
| try { | ||
| if ( | ||
| !wasInternalCall && | ||
| !p5.disableFriendlyErrors && | ||
| !p5.disableParameterValidator | ||
| ) { | ||
| validate(name, args); | ||
| } | ||
| return target.apply(this, args); | ||
| } finally { | ||
| this._isUserCall = wasInternalCall; | ||
| } | ||
| }; |
Member
There was a problem hiding this comment.
This bit can potentially be simplified with the version before the internal call tracking is added.
|
|
||
| ### Step 1: Define behavior | ||
|
|
||
| First, create a new function, which will be out `decorator`: |
Member
There was a problem hiding this comment.
Suggested change
| First, create a new function, which will be out `decorator`: | |
| First, create a new function, which will be our `decorator`: |
|
|
||
| ### Step 2: Register decorator | ||
|
|
||
| Second, use `pattern` to register the decorator on various targets. You can use path comparison (as in the FES example above), or text: |
Member
There was a problem hiding this comment.
Suggested change
| Second, use `pattern` to register the decorator on various targets. You can use path comparison (as in the FES example above), or text: | |
| Second, use `pattern` to register the decorator on various targets. You can use a function that returns `true` if the decorator should apply or vice versa, or a string that matches the `p5` member path exactly: |
|
|
||
| ## Contributing | ||
|
|
||
| In p5.js, decorators are supported since [version 2.3.0](https://github.com/processing/p5.js/releases/tag/v2.3.0), and [partially implement the TC39 proposal](https://github.com/processing/p5.js/issues/8334). Unlike the TC39 proposal, the implementation in p5.js needs to be applied at runtime and after all addons are registered but before the p5 instance is created. Contribution to help maintain decorator usage in p5.js, its implementation, and documentation (especially documentation for addon authors) is welcome! |
Member
There was a problem hiding this comment.
This can also link to the TC39 proposal directly, mentioning that the decorator function follows the TC39 proposal as closely as possible.
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.
Added guide on using decorators in p5.js. This is a new API that has come up several times in different threads. This guide introduces a more centralized version. Please comment anything that's unclear, I would be happy to revise! Also open to comments on structure/etc.