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
// primitives, structs, etc. → not a reference type
48
+
returnfalse
49
+
}
50
+
51
+
// logMutationWarning logs a warning when mutation is detected
52
+
funclogMutationWarning(atomNamestring) {
53
+
_, file, line, ok:=runtime.Caller(2)
54
+
ifok {
55
+
log.Printf("WARNING: atom '%s' appears to be mutated instead of copied at %s:%d - use app.DeepCopy to create a copy before mutating", atomName, file, line)
56
+
} else {
57
+
log.Printf("WARNING: atom '%s' appears to be mutated instead of copied - use app.DeepCopy to create a copy before mutating", atomName)
58
+
}
59
+
}
60
+
24
61
// Atom[T] represents a typed atom implementation
25
62
typeAtom[Tany] struct {
26
63
namestring
@@ -32,7 +69,9 @@ func (a Atom[T]) AtomName() string {
32
69
returna.name
33
70
}
34
71
35
-
// Get returns the current value of the atom
72
+
// Get returns the current value of the atom. When called during component render,
73
+
// it automatically registers the component as a dependency for this atom, ensuring
74
+
// the component re-renders when the atom value changes.
36
75
func (aAtom[T]) Get() T {
37
76
vc:=engine.GetGlobalContext()
38
77
ifvc!=nil {
@@ -44,20 +83,34 @@ func (a Atom[T]) Get() T {
44
83
returntypedVal
45
84
}
46
85
47
-
// Set updates the atom's value
86
+
// Set updates the atom's value to the provided new value and triggers re-rendering
87
+
// of any components that depend on this atom. This method cannot be called during
88
+
// render cycles - use effects or event handlers instead.
48
89
func (aAtom[T]) Set(newValT) {
49
90
vc:=engine.GetGlobalContext()
50
91
ifvc!=nil {
51
92
logInvalidAtomSet(a.name)
52
93
return
53
94
}
95
+
96
+
// Check for potential mutation bugs with reference types
0 commit comments