Skip to content

Create documentation for decorators in p5.js - #9118

Draft
ksen0 wants to merge 2 commits into
mainfrom
ksen0-patch-3
Draft

Create documentation for decorators in p5.js#9118
ksen0 wants to merge 2 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
@p5-bot

p5-bot Bot commented Aug 28, 2026

Copy link
Copy Markdown

Continuous Release

CDN link

Published Packages

Commit hash: 26bf3bd

Previous deployments

70f745a


This is an automated message.

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.


### Step 1: Define behavior

First, create a new function, which will be out `decorator`:

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.

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:

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.

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!

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.

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.

2 participants