Skip to content

Commit

Permalink
doc: update decorator example to show private state and controlled mu…
Browse files Browse the repository at this point in the history
…tations
  • Loading branch information
EisenbergEffect committed Mar 27, 2024
1 parent ef60edb commit 68291a7
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/signal-polyfill/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export function effect(callback) {
### Combining signals and decorators

A class accessor decorator can be combined with the `Signal.State()` API to enable improved DX.

```js
import { Signal } from "signal-polyfill";

Expand All @@ -107,13 +109,26 @@ export function signal(target) {
},
};
}
```

The above decorator can be used on public or **private** accessors, enabling reactivity while carefully controlling state mutations.

```js
export class Counter {
@signal accessor #value = 0;

get value() {
return this.#value;
}

export class Person {
@signal accessor firstName = "";
@signal accessor lastName = "";
increment() {
this.#value++;
}

get fullName() {
return `${this.firstName} ${this.lastName}`;
decrement() {
if (this.#value > 0) {
this.#value--;
}
}
}
```

0 comments on commit 68291a7

Please sign in to comment.