Skip to content

Commit d4f8687

Browse files
committed
genatom interface in rootelem
1 parent 9dd37ed commit d4f8687

6 files changed

Lines changed: 39 additions & 19 deletions

File tree

tsunami/app/atom.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (a Atom[T]) Set(newVal T) {
5151
return
5252
}
5353
a.client.Root.AtomAddRenderWork(a.name)
54-
a.client.SetAtomVal(a.name, newVal)
54+
a.client.Root.SetAtomVal(a.name, newVal)
5555
}
5656

5757
func (a Atom[T]) SetFn(fn func(T) T) {
@@ -64,5 +64,5 @@ func (a Atom[T]) SetFn(fn func(T) T) {
6464
typedVal := util.GetTypedAtomValue[T](val, a.name)
6565
newVal := fn(typedVal)
6666
a.client.Root.AtomAddRenderWork(a.name)
67-
a.client.SetAtomVal(a.name, newVal)
67+
a.client.Root.SetAtomVal(a.name, newVal)
6868
}

tsunami/engine/atomimpl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ func (a *atomImpl) SetVal(val any) {
3535
defer a.lock.Unlock()
3636
a.val = val
3737
}
38+
39+
func (a *atomImpl) SetUsedBy(waveId string, used bool) {
40+
a.lock.Lock()
41+
defer a.lock.Unlock()
42+
if used {
43+
a.UsedBy[waveId] = true
44+
} else {
45+
delete(a.UsedBy, waveId)
46+
}
47+
}
48+
49+
func (a *atomImpl) GetUsedBy() []string {
50+
a.lock.Lock()
51+
defer a.lock.Unlock()
52+
53+
keys := make([]string, 0, len(a.UsedBy))
54+
for compId := range a.UsedBy {
55+
keys = append(keys, compId)
56+
}
57+
return keys
58+
}

tsunami/engine/clientimpl.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,6 @@ func (c *ClientImpl) SendAsyncInitiation() error {
218218
}
219219
}
220220

221-
func (c *ClientImpl) SetAtomVal(name string, val any) {
222-
c.Root.SetAtomVal(name, val, true)
223-
}
224-
225221
func (c *ClientImpl) GetAtomVal(name string) any {
226222
return c.Root.GetAtomVal(name)
227223
}

tsunami/engine/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ func UseAtom(vc *VDomContextImpl, atomName string) (any, func(any), func(func(an
9999
atomVal := vc.Root.GetAtomVal(atomName)
100100

101101
setVal := func(newVal any) {
102-
vc.Root.SetAtomVal(atomName, newVal, true)
102+
vc.Root.SetAtomVal(atomName, newVal)
103103
vc.Root.AtomAddRenderWork(atomName)
104104
}
105105

106106
setFuncVal := func(updateFunc func(any) any) {
107107
currentVal := vc.Root.GetAtomVal(atomName)
108-
vc.Root.SetAtomVal(atomName, updateFunc(currentVal), true)
108+
vc.Root.SetAtomVal(atomName, updateFunc(currentVal))
109109
vc.Root.AtomAddRenderWork(atomName)
110110
}
111111

tsunami/engine/rootelem.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ type EffectWorkElem struct {
4141
EffectIndex int
4242
}
4343

44+
type genAtom interface {
45+
GetVal() any
46+
SetVal(any)
47+
SetUsedBy(string, bool)
48+
GetUsedBy() []string
49+
}
50+
4451
type RootElem struct {
4552
OuterCtx context.Context
4653
Root *ComponentImpl
@@ -50,7 +57,7 @@ type RootElem struct {
5057
CompMap map[string]*ComponentImpl // component waveid -> component
5158
EffectWorkQueue []*EffectWorkElem
5259
NeedsRenderMap map[string]bool
53-
Atoms map[string]*atomImpl
60+
Atoms map[string]genAtom
5461
atomLock sync.Mutex
5562
RefOperations []vdom.VDomRefOperation
5663
}
@@ -99,7 +106,7 @@ func MakeRoot() *RootElem {
99106
Root: nil,
100107
CFuncs: make(map[string]any),
101108
CompMap: make(map[string]*ComponentImpl),
102-
Atoms: make(map[string]*atomImpl),
109+
Atoms: make(map[string]genAtom),
103110
}
104111
}
105112

@@ -120,7 +127,7 @@ func (r *RootElem) cleanupUsedByForUnmount(waveId string) {
120127
defer r.atomLock.Unlock()
121128

122129
for _, atom := range r.Atoms {
123-
delete(atom.UsedBy, waveId)
130+
atom.SetUsedBy(waveId, false)
124131
}
125132
}
126133

@@ -132,11 +139,7 @@ func (r *RootElem) AtomSetUsedBy(atomName string, waveId string, used bool) {
132139
if !ok {
133140
return
134141
}
135-
if used {
136-
atom.UsedBy[waveId] = true
137-
} else {
138-
delete(atom.UsedBy, waveId)
139-
}
142+
atom.SetUsedBy(waveId, used)
140143
}
141144

142145
func (r *RootElem) AtomAddRenderWork(atomName string) {
@@ -147,7 +150,7 @@ func (r *RootElem) AtomAddRenderWork(atomName string) {
147150
if !ok {
148151
return
149152
}
150-
for compId := range atom.UsedBy {
153+
for _, compId := range atom.GetUsedBy() {
151154
r.AddRenderWork(compId)
152155
}
153156
}
@@ -163,7 +166,7 @@ func (r *RootElem) GetAtomVal(name string) any {
163166
return atom.GetVal()
164167
}
165168

166-
func (r *RootElem) SetAtomVal(name string, val any, markDirty bool) {
169+
func (r *RootElem) SetAtomVal(name string, val any) {
167170
r.atomLock.Lock()
168171
defer r.atomLock.Unlock()
169172

tsunami/engine/serverhandlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (h *httpHandlers) handleConfigPost(w http.ResponseWriter, r *http.Request)
225225

226226
for key, value := range configData {
227227
atomName := "$config." + key
228-
h.Client.Root.SetAtomVal(atomName, value, true)
228+
h.Client.Root.SetAtomVal(atomName, value)
229229
}
230230

231231
w.WriteHeader(http.StatusOK)

0 commit comments

Comments
 (0)