...
1 package queue
2
3 import "sync"
4
5
6 type Queue struct {
7 sync.Mutex
8 fns map[string]chan struct{}
9 }
10
11
12 func (q *Queue) Append(id string, f func()) {
13 q.Lock()
14 defer q.Unlock()
15
16 if q.fns == nil {
17 q.fns = make(map[string]chan struct{})
18 }
19
20 done := make(chan struct{})
21
22 fn, ok := q.fns[id]
23 q.fns[id] = done
24 go func() {
25 if ok {
26 <-fn
27 }
28 f()
29 close(done)
30
31 q.Lock()
32 if q.fns[id] == done {
33 delete(q.fns, id)
34 }
35 q.Unlock()
36 }()
37 }
38
View as plain text