-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59a48f2
commit 19e4353
Showing
5 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package app | ||
|
||
// updateManager manages scheduled updates for UI components. It ensures that | ||
// components are updated in an order respecting their depth in the UI | ||
// hierarchy. | ||
type updateManager struct { | ||
pending []map[Composer]struct{} | ||
} | ||
|
||
// Add queues the given component for an update. | ||
func (m *updateManager) Add(v Composer) { | ||
depth := int(v.depth()) | ||
if len(m.pending) <= depth { | ||
size := max(depth+1, 100) | ||
pending := make([]map[Composer]struct{}, size) | ||
copy(pending, m.pending) | ||
m.pending = pending | ||
} | ||
|
||
updates := m.pending[depth] | ||
if updates == nil { | ||
updates = make(map[Composer]struct{}) | ||
m.pending[depth] = updates | ||
} | ||
updates[v] = struct{}{} | ||
} | ||
|
||
// Done removes the given Composer from the update queue, marking it as updated. | ||
func (m *updateManager) Done(v Composer) { | ||
depth := v.depth() | ||
if len(m.pending) <= int(depth) { | ||
return | ||
} | ||
delete(m.pending[depth], v) | ||
} | ||
|
||
// ForEach iterates over all queued components, invoking the provided function | ||
// on each. | ||
func (m *updateManager) ForEach(do func(Composer)) { | ||
for _, updates := range m.pending { | ||
for compo := range updates { | ||
do(compo) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package app | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUpdateManagerAdd(t *testing.T) { | ||
t.Run("component is queued", func(t *testing.T) { | ||
var m updateManager | ||
|
||
compo := &hello{} | ||
m.Add(compo) | ||
require.Len(t, m.pending, 100) | ||
_, ok := m.pending[0][compo] | ||
require.True(t, ok) | ||
}) | ||
|
||
t.Run("pending components size is increased", func(t *testing.T) { | ||
var m updateManager | ||
|
||
compo := &hello{} | ||
m.Add(compo) | ||
require.Len(t, m.pending, 100) | ||
|
||
compo2 := &bar{Compo: Compo{treeDepth: 200}} | ||
m.Add(compo2) | ||
require.Len(t, m.pending, 201) | ||
|
||
_, added := m.pending[0][compo] | ||
require.True(t, added) | ||
|
||
_, added2 := m.pending[200][compo2] | ||
require.True(t, added2) | ||
}) | ||
} | ||
|
||
func TestUpdateManagerDone(t *testing.T) { | ||
t.Run("component is removed from pending", func(t *testing.T) { | ||
var m updateManager | ||
|
||
compo := &hello{} | ||
m.Add(compo) | ||
_, ok := m.pending[0][compo] | ||
require.True(t, ok) | ||
|
||
m.Done(compo) | ||
_, ok = m.pending[0][compo] | ||
require.False(t, ok) | ||
}) | ||
|
||
t.Run("non added component is skipped", func(t *testing.T) { | ||
var m updateManager | ||
m.Done(&hello{}) | ||
}) | ||
} | ||
|
||
func TestUpdateManagerForEach(t *testing.T) { | ||
var m updateManager | ||
|
||
compo := &hello{} | ||
m.Add(compo) | ||
|
||
m.ForEach(func(c Composer) { | ||
m.Done(c) | ||
}) | ||
require.Empty(t, m.pending[0]) | ||
} |