Skip to content

Create documentation for decorators in p5.js - #9118

Open
ksen0 wants to merge 5 commits into
mainfrom
ksen0-patch-3
Open

Create documentation for decorators in p5.js#9118
ksen0 wants to merge 5 commits into
mainfrom
ksen0-patch-3

Conversation

@ksen0

@ksen0 ksen0 commented Aug 28, 2026

Copy link
Copy Markdown
Member

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.

Added guide on using decorators in p5.js
Added instructions for using @Private and @internal tags in docstrings.
@ksen0 ksen0 mentioned this pull request Aug 28, 2026
3 tasks
@ksen0
ksen0 requested a review from perminder-17 August 28, 2026 14:12

@limzykenneth limzykenneth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit can potentially be simplified with the version before the internal call tracking is added.

Comment thread contributor_docs/decorators.md Outdated
Comment thread contributor_docs/decorators.md Outdated

## 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!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also link to the TC39 proposal directly, mentioning that the decorator function follows the TC39 proposal as closely as possible.

ksen0 and others added 2 commits August 31, 2026 17:40
Co-authored-by: Kenneth Lim <limzy.kenneth@gmail.com>
Co-authored-by: Kenneth Lim <limzy.kenneth@gmail.com>
@ksen0 ksen0 added the Patch label Aug 31, 2026
@ksen0
ksen0 marked this pull request as ready for review August 31, 2026 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants