Skip to content

Commit

Permalink
Fix for updating nested views multiple times (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
activey authored Sep 23, 2024
1 parent 324aa02 commit b4de05f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion view.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (v *View) Update() {
if v.isDirty {
v.startLayout()
}
v.processHandler()
if !v.hasParent {
v.processHandler()
}
for _, v := range v.children {
v.item.Update()
v.item.processHandler()
Expand Down
29 changes: 29 additions & 0 deletions view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,32 @@ func TestAddChild(t *testing.T) {
view.RemoveAll()
require.Equal(t, 0, len(view.children))
}

type CountingHandler struct {
Times int
}

func (t *CountingHandler) Update(v *View) {
t.Times++
}

func TestUpdateOnlyOnce(t *testing.T) {
rootHandler := &CountingHandler{}
nestedHandler := &CountingHandler{}

// given
rootView := &View{
Handler: rootHandler,
}
nestedView := &View{
Handler: nestedHandler,
}
rootView.addChild(nestedView)

// when
rootView.Update()

// then
require.True(t, rootHandler.Times == 1)
require.True(t, nestedHandler.Times == 1)
}

0 comments on commit b4de05f

Please sign in to comment.