|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package app |
| 5 | + |
| 6 | +import ( |
| 7 | + "log" |
| 8 | + "reflect" |
| 9 | + "runtime" |
| 10 | + |
| 11 | + "github.com/wavetermdev/waveterm/tsunami/engine" |
| 12 | + "github.com/wavetermdev/waveterm/tsunami/util" |
| 13 | +) |
| 14 | + |
| 15 | +// logInvalidAtomSet logs an error when an atom is being set during component render |
| 16 | +func logInvalidAtomSet(atomName string) { |
| 17 | + _, file, line, ok := runtime.Caller(2) |
| 18 | + if ok { |
| 19 | + log.Printf("invalid Set of atom '%s' in component render function at %s:%d", atomName, file, line) |
| 20 | + } else { |
| 21 | + log.Printf("invalid Set of atom '%s' in component render function", atomName) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// sameRef returns true if oldVal and newVal share the same underlying reference |
| 26 | +// (pointer, map, or slice). Nil values return false. |
| 27 | +func sameRef[T any](oldVal, newVal T) bool { |
| 28 | + vOld := reflect.ValueOf(oldVal) |
| 29 | + vNew := reflect.ValueOf(newVal) |
| 30 | + |
| 31 | + if !vOld.IsValid() || !vNew.IsValid() { |
| 32 | + return false |
| 33 | + } |
| 34 | + |
| 35 | + switch vNew.Kind() { |
| 36 | + case reflect.Ptr: |
| 37 | + // direct comparison works for *T |
| 38 | + return any(oldVal) == any(newVal) |
| 39 | + |
| 40 | + case reflect.Map, reflect.Slice: |
| 41 | + if vOld.Kind() != vNew.Kind() || vOld.IsZero() || vNew.IsZero() { |
| 42 | + return false |
| 43 | + } |
| 44 | + return vOld.Pointer() == vNew.Pointer() |
| 45 | + } |
| 46 | + |
| 47 | + // primitives, structs, etc. → not a reference type |
| 48 | + return false |
| 49 | +} |
| 50 | + |
| 51 | +// logMutationWarning logs a warning when mutation is detected |
| 52 | +func logMutationWarning(atomName string) { |
| 53 | + _, file, line, ok := runtime.Caller(2) |
| 54 | + if ok { |
| 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 | + |
| 61 | +// Atom[T] represents a typed atom implementation |
| 62 | +type Atom[T any] struct { |
| 63 | + name string |
| 64 | + client *engine.ClientImpl |
| 65 | +} |
| 66 | + |
| 67 | +// AtomName implements the vdom.Atom interface |
| 68 | +func (a Atom[T]) AtomName() string { |
| 69 | + return a.name |
| 70 | +} |
| 71 | + |
| 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. |
| 75 | +func (a Atom[T]) Get() T { |
| 76 | + vc := engine.GetGlobalRenderContext() |
| 77 | + if vc != nil { |
| 78 | + vc.UsedAtoms[a.name] = true |
| 79 | + } |
| 80 | + val := a.client.Root.GetAtomVal(a.name) |
| 81 | + typedVal := util.GetTypedAtomValue[T](val, a.name) |
| 82 | + return typedVal |
| 83 | +} |
| 84 | + |
| 85 | +// Set updates the atom's value to the provided new value and triggers re-rendering |
| 86 | +// of any components that depend on this atom. This method cannot be called during |
| 87 | +// render cycles - use effects or event handlers instead. |
| 88 | +func (a Atom[T]) Set(newVal T) { |
| 89 | + vc := engine.GetGlobalRenderContext() |
| 90 | + if vc != nil { |
| 91 | + logInvalidAtomSet(a.name) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + // Check for potential mutation bugs with reference types |
| 96 | + currentVal := a.client.Root.GetAtomVal(a.name) |
| 97 | + currentTyped := util.GetTypedAtomValue[T](currentVal, a.name) |
| 98 | + if sameRef(currentTyped, newVal) { |
| 99 | + logMutationWarning(a.name) |
| 100 | + } |
| 101 | + |
| 102 | + if err := a.client.Root.SetAtomVal(a.name, newVal); err != nil { |
| 103 | + log.Printf("Failed to set atom value for %s: %v", a.name, err) |
| 104 | + return |
| 105 | + } |
| 106 | + a.client.Root.AtomAddRenderWork(a.name) |
| 107 | +} |
| 108 | + |
| 109 | +// SetFn updates the atom's value by applying the provided function to the current value. |
| 110 | +// The function receives a copy of the current atom value, which can be safely mutated |
| 111 | +// without affecting the original data. The return value from the function becomes the |
| 112 | +// new atom value. This method cannot be called during render cycles. |
| 113 | +func (a Atom[T]) SetFn(fn func(T) T) { |
| 114 | + vc := engine.GetGlobalRenderContext() |
| 115 | + if vc != nil { |
| 116 | + logInvalidAtomSet(a.name) |
| 117 | + return |
| 118 | + } |
| 119 | + currentVal := a.Get() |
| 120 | + copiedVal := DeepCopy(currentVal) |
| 121 | + newVal := fn(copiedVal) |
| 122 | + a.Set(newVal) |
| 123 | +} |
0 commit comments