forked from trustmaster/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
46 lines (39 loc) · 990 Bytes
/
component.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
package goflow
// Component is a unit that can start a process
type Component interface {
Process()
}
// Done notifies that the process is finished
type Done struct{}
// Wait is a channel signalling of a completion
type Wait chan struct{}
// Run the component process
func Run(c Component) Wait {
wait := make(Wait)
go func() {
c.Process()
wait <- Done{}
}()
return wait
}
// InputGuard counts number of closed inputs
type InputGuard struct {
ports map[string]bool
complete int
}
// NewInputGuard returns a guard for a given number of inputs
func NewInputGuard(ports ...string) *InputGuard {
portMap := make(map[string]bool, len(ports))
for _, p := range ports {
portMap[p] = false
}
return &InputGuard{portMap, 0}
}
// Complete is called when a port is closed and returns true when all the ports have been closed
func (g *InputGuard) Complete(port string) bool {
if !g.ports[port] {
g.ports[port] = true
g.complete++
}
return g.complete >= len(g.ports)
}