-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionQueue.go
56 lines (47 loc) · 1.14 KB
/
actionQueue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package internal
import (
"container/list"
)
type ActionQueue struct {
g *Game // sads
list *list.List
}
// FUTURE: do we need to pass Game here? could we have a future queue ( and promises ) as a completely separate system?
type Future interface {
Run(g *Game) error
}
//
func NewActionQueue(g *Game) *ActionQueue {
return &ActionQueue{g, list.New()}
}
//
func (q ActionQueue) Empty() bool {
return q.list.Front() == nil
}
func (q ActionQueue) Enqueue(f Future) {
q.list.PushBack(f)
// fmt.Println(fmt.Sprintf("queuing %T future, %d", f, q.list.Len()))
}
//
func (q ActionQueue) QueueFuture(f Future) {
//q.list.PushBack(f)
// fmt.Println(fmt.Sprintf("queuing %T future, %d", f, q.list.Len()))
f.Run(q.g)
q.g.Frame.FlushFrame()
}
//
func (q ActionQueue) Pop() (ret Future) {
if el := q.list.Front(); el != nil {
ret = q.list.Remove(el).(Future)
}
// fmt.Println(fmt.Sprintf("popped %T future, %d", ret, q.list.Len()))
return ret
}
// this craziness exists to help unwind the very deep callstacks the events create
func (q *ActionQueue) ProcessActions(g *Game) (err error) {
for !q.Empty() {
r := q.Pop()
r.Run(g)
}
return err
}