-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
41 lines (36 loc) · 910 Bytes
/
link.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
package chanutil
// Never create this type yourself.
// Link or a Linker is what links multiple type of utility together. All utility in this pacakge are generators, this means that you can't provide your own channels and this is the only exception type.
// You can use this to filter + broadcast a value for example.
type Link struct {
done chan struct{}
}
// Create a new linker.
func NewLink(dst, src chan interface{}) *Link {
l := &Link{}
l.done = make(chan struct{})
go func() {
outter:
for {
select {
case <-l.done:
break outter
case v, ok := <-src:
if ok {
dst <- v
} else {
close(dst)
close(l.done)
break outter
}
}
}
}()
return l
}
// Closing of b will automatically Unlink for you.
// Never call Unlink if you intend to close b or call Unlink() twice.
// Closing a closed channel is an error.
func (l *Link) Unlink() {
close(l.done)
}