-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
59 lines (43 loc) · 1.33 KB
/
source.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
49
50
51
52
53
54
55
56
57
58
59
package pipes
const RepeatForever = -1
func Source[T any](repeat, size int, source func() T) ChanPull[T] {
out := make(chan T, size)
go sourceWorker(repeat, source, out)
return out
}
func sourceWorker[T any](repeat int, source func() T, out chan<- T) {
defer close(out)
for i := 0; repeat == RepeatForever || repeat > 0; i++ {
out <- source()
}
}
func SourceWithError[T any](repeat, size int, source func() (T, error)) (ChanPull[T], ChanPull[error]) {
out, err := make(chan T, size), make(chan error, size)
go sourceWithErrorWorker(repeat, source, out, err)
return out, err
}
func sourceWithErrorWorker[T any](repeat int, source func() (T, error), out chan<- T, err chan<- error) {
defer func() { close(err); close(out) }()
for i := 0; repeat == RepeatForever || repeat > 0; i++ {
if v, er := source(); er != nil {
err <- er
} else {
out <- v
}
}
}
func SourceWithErrorSink[T any](repeat, size int, source func() (T, error), sink func(error)) ChanPull[T] {
out := make(chan T, size)
go sourceWithErrorSinkWorker(repeat, source, sink, out)
return out
}
func sourceWithErrorSinkWorker[T any](repeat int, source func() (T, error), sink func(error), out chan<- T) {
defer close(out)
for i := 0; repeat == RepeatForever || repeat > 0; i++ {
if v, err := source(); err != nil {
sink(err)
} else {
out <- v
}
}
}