forked from ReactiveX/RxGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle.go
46 lines (39 loc) · 1.17 KB
/
single.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 rxgo
// Single is a observable with a single element.
type Single interface {
Iterable
Filter(apply Predicate, opts ...Option) OptionalSingle
Map(apply Func, opts ...Option) Single
}
type single struct {
iterable Iterable
}
func newSingleFromOperator(iterable Iterable, nextFunc, errFunc ItemHandler, endFunc EndHandler, opts ...Option) Single {
next := operator(iterable, nextFunc, errFunc, endFunc, opts...)
return &single{
iterable: newChannelIterable(next),
}
}
func (s *single) Observe(opts ...Option) <-chan Item {
return s.iterable.Observe()
}
func (s *single) Filter(apply Predicate, opts ...Option) OptionalSingle {
return newOptionalSingleFromOperator(s, func(item Item, dst chan<- Item, stop func()) {
if apply(item.Value) {
dst <- item
}
stop()
}, defaultErrorFuncOperator, defaultEndFuncOperator, opts...)
}
func (s *single) Map(apply Func, opts ...Option) Single {
return newSingleFromOperator(s, func(item Item, dst chan<- Item, stop func()) {
res, err := apply(item.Value)
if err != nil {
dst <- FromError(err)
stop()
} else {
dst <- FromValue(res)
stop()
}
}, defaultErrorFuncOperator, defaultEndFuncOperator, opts...)
}