-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionQueue.go
68 lines (54 loc) · 1.13 KB
/
FunctionQueue.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
57
58
59
60
61
62
63
64
65
66
67
68
package FunctionQueue
type FunctionQueue struct {
head *FunctionNode
isRunning bool
completion func()
}
func NewFunctionQueue() FunctionQueue {
return FunctionQueue{nil, false, nil}
}
func (self *FunctionQueue) Add(function func ()) {
node := newFunctionNode(function)
if self.head != nil {
self.head.add(node)
} else {
self.head = node
self.run()
}
}
func (self *FunctionQueue) run() {
self.head.run(func() {
self.head = self.head.next
if self.head != nil {
self.run()
} else if self.completion != nil {
self.completion()
}
})
}
func (self *FunctionQueue) SetCompletion(completion func()) {
self.completion = completion
}
func (self *FunctionQueue) Size() int {
if self.head != nil {
return self.head.size()
}
return 0
}
func (self *FunctionQueue) Remove(index int) *FunctionNode {
if index == 0 {
panic("Cannot remove Queue at index 0")
}
traverse := self.head
for i:=1; i < index; i++ {
traverse = traverse.next
}
node := traverse.next
traverse.next = traverse.next.next
return node
}
func (self *FunctionQueue) RemoveAllButCurrent() {
if self.head != nil {
self.head.next = nil
}
}