-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
48 lines (39 loc) · 1019 Bytes
/
wait.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
package state
import (
"sync"
)
type waitState struct {
*group
sync.WaitGroup
}
// WaitTail detaches after waitable state initialization.
// The tail is supposed to stay in a background job associated with
// created State.
//
// WaitTail uses sync.WaitGroup and shares all its mechanics.
type WaitTail interface {
// Done calls sync.WaitGroup's Done method
Done()
// Add calls sync.WaitGroup's Add method
Add(i int)
}
// WithWait returns new waitable State with merged children.
//
// The returned WaitTail is used to increment and decrement State's WaitGroup counter.
func WithWait(children ...State) (State, WaitTail) {
s := withWait(children...)
return s, s
}
func withWait(children ...State) *waitState {
return &waitState{
group: merge(children...),
}
}
// Wait blocks until States's and States's children counters are zero.
func (w *waitState) Wait() {
w.WaitGroup.Wait()
w.group.Wait()
}
func (w *waitState) DependsOn(children ...State) State {
return withDependency(w, children...)
}