Skip to content

Commit 3d0e99c

Browse files
docs: add trigger() function documentation to README
Co-authored-by: johnsoncodehk <[email protected]>
1 parent ddfd88f commit 3d0e99c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,47 @@ stopScope();
9696
count(3); // No console output
9797
```
9898

99+
#### Manual Triggering
100+
101+
The `trigger()` function allows you to manually trigger updates for downstream dependencies when you've directly mutated a signal's value without using the signal setter:
102+
103+
```ts
104+
import { signal, computed, trigger } from 'alien-signals';
105+
106+
const arr = signal<number[]>([]);
107+
const length = computed(() => arr().length);
108+
109+
console.log(length()); // 0
110+
111+
// Direct mutation doesn't automatically trigger updates
112+
arr().push(1);
113+
console.log(length()); // Still 0
114+
115+
// Manually trigger updates
116+
trigger(arr);
117+
console.log(length()); // 1
118+
```
119+
120+
You can also trigger multiple signals at once:
121+
122+
```ts
123+
import { signal, computed, trigger } from 'alien-signals';
124+
125+
const src1 = signal<number[]>([]);
126+
const src2 = signal<number[]>([]);
127+
const total = computed(() => src1().length + src2().length);
128+
129+
src1().push(1);
130+
src2().push(2);
131+
132+
trigger(() => {
133+
src1();
134+
src2();
135+
});
136+
137+
console.log(total()); // 2
138+
```
139+
99140
#### Creating Your Own Surface API
100141

101142
You can reuse alien-signals’ core algorithm via `createReactiveSystem()` to build your own signal API. For implementation examples, see:

0 commit comments

Comments
 (0)