Skip to content

Commit 68291a7

Browse files
doc: update decorator example to show private state and controlled mutations
1 parent ef60edb commit 68291a7

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

packages/signal-polyfill/readme.md

+20-5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export function effect(callback) {
8787
8888
### Combining signals and decorators
8989

90+
A class accessor decorator can be combined with the `Signal.State()` API to enable improved DX.
91+
9092
```js
9193
import { Signal } from "signal-polyfill";
9294

@@ -107,13 +109,26 @@ export function signal(target) {
107109
},
108110
};
109111
}
112+
```
113+
114+
The above decorator can be used on public or **private** accessors, enabling reactivity while carefully controlling state mutations.
115+
116+
```js
117+
export class Counter {
118+
@signal accessor #value = 0;
119+
120+
get value() {
121+
return this.#value;
122+
}
110123

111-
export class Person {
112-
@signal accessor firstName = "";
113-
@signal accessor lastName = "";
124+
increment() {
125+
this.#value++;
126+
}
114127

115-
get fullName() {
116-
return `${this.firstName} ${this.lastName}`;
128+
decrement() {
129+
if (this.#value > 0) {
130+
this.#value--;
131+
}
117132
}
118133
}
119134
```

0 commit comments

Comments
 (0)