-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin.go
More file actions
52 lines (46 loc) · 1.03 KB
/
join.go
File metadata and controls
52 lines (46 loc) · 1.03 KB
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
package flame
type JoinNode[X, Y, Z any] struct {
LeftInput chan X
RightInput chan Y
Outputs []chan Z
Proc func(chan X, chan Y, chan Z)
}
func AddJoin[X, Y, Z any](w *Workflow, f func(chan X, chan Y, chan Z)) *JoinNode[X, Y, Z] {
n := &JoinNode[X, Y, Z]{Proc: f, Outputs: []chan Z{}}
w.Nodes = append(w.Nodes, n)
return n
}
func (n *JoinNode[X, Y, Z]) GetOutput() chan Z {
m := make(chan Z)
n.Outputs = append(n.Outputs, m)
return m
}
func (n *JoinNode[X, Y, Z]) ConnectLeft(e Emitter[X]) {
o := e.GetOutput()
n.LeftInput = o
}
func (n *JoinNode[X, Y, Z]) ConnectRight(e Emitter[Y]) {
o := e.GetOutput()
n.RightInput = o
}
func (n *JoinNode[X, Y, Z]) start(wf *Workflow) {
wf.WaitGroup.Add(1)
out := make(chan Z, 10)
go func() {
if n.LeftInput != nil && n.RightInput != nil {
n.Proc(n.LeftInput, n.RightInput, out)
}
close(out)
}()
go func() {
for o := range out {
for i := range n.Outputs {
n.Outputs[i] <- o
}
}
for i := range n.Outputs {
close(n.Outputs[i])
}
wf.WaitGroup.Done()
}()
}