You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tsunami/prompts/system.md
+17-15Lines changed: 17 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -364,17 +364,18 @@ var MyComponent = app.DefineComponent("MyComponent", func(_ struct{}) any {
364
364
})
365
365
```
366
366
367
-
**Never mutate values from Get()**: For complex data types, never modify the value returned from `atom.Get()`. Always use `app.DeepCopy()` before mutations:
367
+
**Never mutate values from Get()**: For complex data types, never modify the value returned from `atom.Get()`.
368
+
369
+
**Use SetFn() for safe mutations**: `SetFn()` automatically handles deep copying, making it safe to modify complex data:
368
370
369
371
```go
370
372
varMyComponent = app.DefineComponent("MyComponent", func(_ struct{}) any {
//Merge new data with existing, handle deduplication
1014
+
//SetFn automatically deep copies current, safe to modify
1013
1015
returnmergeResults(current, newData)
1014
1016
})
1015
1017
status.Set("success")
@@ -1073,8 +1075,8 @@ Atoms are internally synchronized, so multiple goroutines can safely call Get()
1073
1075
// Safe pattern for concurrent updates using SetFn
1074
1076
updateTodos:=func() {
1075
1077
todosAtom.SetFn(func(current []Todo) []Todo {
1076
-
todosCopy:= app.DeepCopy(current)
1077
-
returnappend(todosCopy, newTodo)
1078
+
// SetFn automatically deep copies current value
1079
+
returnappend(current, newTodo)
1078
1080
})
1079
1081
}
1080
1082
```
@@ -1172,7 +1174,7 @@ Key points:
1172
1174
1173
1175
- ✅ Read with atom.Get() in render code
1174
1176
- ❌ Never call atom.Set() in render code - only in handlers/effects
1175
-
- ✅ Always use SetFn() for concurrent updates from goroutines
1177
+
- ✅ Always use SetFn() for concurrent updates from goroutines (automatically deep copies the value)
1176
1178
1177
1179
## Tsunami App Template
1178
1180
@@ -1253,14 +1255,14 @@ var App = app.DefineComponent("App", func(_ struct{}) any {
1253
1255
1254
1256
toggleTodo:=func(id int) {
1255
1257
todos.SetFn(func(current []Todo) []Todo {
1256
-
todosCopy:= app.DeepCopy(current)
1257
-
fori:=rangetodosCopy {
1258
-
iftodosCopy[i].Id == id {
1259
-
todosCopy[i].Completed = !todosCopy[i].Completed
1258
+
// SetFn automatically deep copies current value
1259
+
fori:=rangecurrent {
1260
+
ifcurrent[i].Id == id {
1261
+
current[i].Completed = !current[i].Completed
1260
1262
break
1261
1263
}
1262
1264
}
1263
-
returntodosCopy
1265
+
returncurrent
1264
1266
})
1265
1267
}
1266
1268
@@ -1330,7 +1332,7 @@ Key points:
1330
1332
2.**Missing keys in lists**: Always use `.WithKey(id)` for list items
1331
1333
3.**Stale closures in goroutines**: Use `atom.Get()` inside event handlers, effects, and goroutines, not captured values
1332
1334
4.**Wrong prop format**: Use `"className"` not `"class"`, `"onClick"` not `"onclick"` (matching React prop and style names)
1333
-
5.**Mutating state**: Always create new slices/objects when updating atoms (can use app.DeepCopy helper)
1335
+
5.**Mutating state**: With `SetFn()`, you can safely modify the current value as it's automatically deep copied. With `Set()`, create new slices/objects or use app.DeepCopy helper
0 commit comments