Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit cf644e0

Browse files
committed
fix(core): prevent calling onStateChange recurively when changing state from within onStateChange (#1209)
1 parent c48cb4b commit cf644e0

File tree

7 files changed

+12174
-1
lines changed

7 files changed

+12174
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { actor } from "rivetkit";
2+
3+
export const onStateChangeActor = actor({
4+
onAuth: () => {},
5+
state: {
6+
value: 0,
7+
changeCount: 0,
8+
},
9+
actions: {
10+
// Action that modifies state - should trigger onStateChange
11+
setValue: (c, newValue: number) => {
12+
c.state.value = newValue;
13+
return c.state.value;
14+
},
15+
// Action that modifies state multiple times - should trigger onStateChange for each change
16+
incrementMultiple: (c, times: number) => {
17+
for (let i = 0; i < times; i++) {
18+
c.state.value++;
19+
}
20+
return c.state.value;
21+
},
22+
// Action that doesn't modify state - should NOT trigger onStateChange
23+
getValue: (c) => {
24+
return c.state.value;
25+
},
26+
// Action that reads and returns without modifying - should NOT trigger onStateChange
27+
getDoubled: (c) => {
28+
const doubled = c.state.value * 2;
29+
return doubled;
30+
},
31+
// Get the count of how many times onStateChange was called
32+
getChangeCount: (c) => {
33+
return c.state.changeCount;
34+
},
35+
// Reset change counter for testing
36+
resetChangeCount: (c) => {
37+
c.state.changeCount = 0;
38+
},
39+
},
40+
// Track onStateChange calls
41+
onStateChange: (c) => {
42+
c.state.changeCount++;
43+
},
44+
});

0 commit comments

Comments
 (0)