MCPcopy Create free account
hub / github.com/wavetermdev/waveterm / UseAtom

Function UseAtom

pkg/vdom/vdom.go:360–383  ·  view source on GitHub ↗
(ctx context.Context, atomName string)

Source from the content-addressed store, hash-verified

358}
359
360func UseAtom[T any](ctx context.Context, atomName string) (T, func(T)) {
361 vc, hookVal := getHookFromCtx(ctx)
362 if !hookVal.Init {
363 hookVal.Init = true
364 closedWaveId := vc.Comp.WaveId
365 hookVal.UnmountFn = func() {
366 atom := vc.Root.GetAtom(atomName)
367 delete(atom.UsedBy, closedWaveId)
368 }
369 }
370 atom := vc.Root.GetAtom(atomName)
371 atom.UsedBy[vc.Comp.WaveId] = true
372 atomVal, ok := atom.Val.(T)
373 if !ok {
374 panic(fmt.Sprintf("UseAtom %q value type mismatch (expected %T, got %T)", atomName, atomVal, atom.Val))
375 }
376 setVal := func(newVal T) {
377 atom.Val = newVal
378 for waveId := range atom.UsedBy {
379 vc.Root.AddRenderWork(waveId)
380 }
381 }
382 return atomVal, setVal
383}
384
385func UseVDomRef(ctx context.Context) *VDomRef {
386 vc, hookVal := getHookFromCtx(ctx)

Callers

nothing calls this directly

Calls 3

getHookFromCtxFunction · 0.85
GetAtomMethod · 0.80
AddRenderWorkMethod · 0.80

Tested by

no test coverage detected