Skip to content

Commit

Permalink
enqueue parent to updates
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed Apr 20, 2021
1 parent 261e381 commit d67b8cd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
70 changes: 48 additions & 22 deletions pkg/app/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,37 +275,63 @@ func (e *engine) start(ctx context.Context) {
}

func (e *engine) scheduleComponentUpdate(n UI) {
var compo Composer
var depth int
// var compo Composer
// var depth int

// for {
// if c, isCompo := n.(Composer); isCompo {
// if _, isScheduled := e.updates[c]; isScheduled {
// return
// }

// if compo == nil {
// compo = c
// } else {
// e.scheduleComponentUpdate(c)
// }
// }

// parent := n.parent()
// if parent == nil {
// break
// }

// if compo != nil {
// depth++
// }
// n = parent
// }

// if compo == nil {
// return
// }

// e.updates[compo] = struct{}{}
// e.updateQueue = append(e.updateQueue, updateDescriptor{
// compo: compo,
// priority: depth + 1,
// })

priority := 1

for {
if c, isCompo := n.(Composer); compo == nil && isCompo {
if _, isScheduled := e.updates[c]; isScheduled {
return
if c, isCompo := n.(Composer); isCompo {
if _, isScheduled := e.updates[c]; !isScheduled {
e.updates[c] = struct{}{}
e.updateQueue = append(e.updateQueue, updateDescriptor{
compo: c,
priority: priority,
})
}
compo = c
}

parent := n.parent()
if parent == nil {
break
}

if compo != nil {
depth++
return
}
n = parent
priority++
}

if compo == nil {
return
}

e.updates[compo] = struct{}{}
e.updateQueue = append(e.updateQueue, updateDescriptor{
compo: compo,
priority: depth + 1,
})
}

func (e *engine) updateComponents() {
Expand All @@ -314,7 +340,7 @@ func (e *engine) updateComponents() {
}

sort.Slice(e.updateQueue, func(a, b int) bool {
return e.updateQueue[a].priority < e.updateQueue[b].priority
return e.updateQueue[a].priority > e.updateQueue[b].priority
})

for _, ud := range e.updateQueue {
Expand Down
6 changes: 3 additions & 3 deletions pkg/app/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestEngineScheduleComponentUpdate(t *testing.T) {
require.Equal(t, struct{}{}, e.updates[h])
require.Equal(t, updateDescriptor{
compo: h,
priority: 2,
priority: 1,
}, e.updateQueue[0])

e.scheduleComponentUpdate(h)
Expand All @@ -67,11 +67,11 @@ func TestEngineScheduleNestedComponentUpdate(t *testing.T) {
require.Equal(t, struct{}{}, e.updates[h])
require.Equal(t, updateDescriptor{
compo: h,
priority: 3,
priority: 1,
}, e.updateQueue[0])
}

func TestEngineUpdateCoponents(t *testing.T) {
func TestEngineUpdateComponents(t *testing.T) {
e := engine{}
e.init()

Expand Down

0 comments on commit d67b8cd

Please sign in to comment.