getOrCreateState retrieves existing state by key, or creates it with the initial value.
(s *Scope, key string, initial T)
| 145 | |
| 146 | // getOrCreateState retrieves existing state by key, or creates it with the initial value. |
| 147 | func getOrCreateState[T any](s *Scope, key string, initial T) *StateValue[T] { |
| 148 | s.mu.Lock() |
| 149 | defer s.mu.Unlock() |
| 150 | |
| 151 | if existing, ok := s.states[key]; ok { |
| 152 | return existing.(*StateValue[T]) |
| 153 | } |
| 154 | |
| 155 | st := newState(initial, s.onStateChange) |
| 156 | s.states[key] = st |
| 157 | return st |
| 158 | } |
| 159 | |
| 160 | // getOrCreateRef retrieves existing ref by key, or creates it with the initial value. |
| 161 | func getOrCreateRef[T any](s *Scope, key string, initial T) *RefValue[T] { |