forked from ReactiveX/RxGo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
single.go
129 lines (108 loc) · 3.2 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package rxgo
import (
"context"
)
// Single is a observable with a single element.
type Single interface {
Iterable
Filter(apply Predicate, opts ...Option) OptionalSingle
Get(opts ...Option) (Item, error)
Map(apply Func, opts ...Option) Single
Run(opts ...Option) Disposed
}
// SingleImpl implements Single.
type SingleImpl struct {
parent context.Context
iterable Iterable
}
// Filter emits only those items from an Observable that pass a predicate test.
func (s *SingleImpl) Filter(apply Predicate, opts ...Option) OptionalSingle {
return optionalSingle(s.parent, s, func() operator {
return &filterOperatorSingle{apply: apply}
}, true, true, opts...)
}
// Get returns the item. The error returned is if the context has been cancelled.
// This method is blocking.
func (s *SingleImpl) Get(opts ...Option) (Item, error) {
option := parseOptions(opts...)
ctx := option.buildContext(s.parent)
observe := s.Observe(opts...)
for {
select {
case <-ctx.Done():
return Item{}, ctx.Err()
case v := <-observe:
return v, nil
}
}
}
// Map transforms the items emitted by a Single by applying a function to each item.
func (s *SingleImpl) Map(apply Func, opts ...Option) Single {
return single(s.parent, s, func() operator {
return &mapOperatorSingle{apply: apply}
}, false, true, opts...)
}
type mapOperatorSingle struct {
apply Func
}
func (op *mapOperatorSingle) next(ctx context.Context, item Item, dst chan<- Item, operatorOptions operatorOptions) {
res, err := op.apply(ctx, item.V)
if err != nil {
Error(err).SendContext(ctx, dst)
operatorOptions.stop()
return
}
Of(res).SendContext(ctx, dst)
}
func (op *mapOperatorSingle) err(ctx context.Context, item Item, dst chan<- Item, operatorOptions operatorOptions) {
defaultErrorFuncOperator(ctx, item, dst, operatorOptions)
}
func (op *mapOperatorSingle) end(_ context.Context, _ chan<- Item) {
}
func (op *mapOperatorSingle) gatherNext(ctx context.Context, item Item, dst chan<- Item, _ operatorOptions) {
switch item.V.(type) {
case *mapOperatorSingle:
return
}
item.SendContext(ctx, dst)
}
// Observe observes a Single by returning its channel.
func (s *SingleImpl) Observe(opts ...Option) <-chan Item {
return s.iterable.Observe(opts...)
}
type filterOperatorSingle struct {
apply Predicate
}
func (op *filterOperatorSingle) next(ctx context.Context, item Item, dst chan<- Item, _ operatorOptions) {
if op.apply(item.V) {
item.SendContext(ctx, dst)
}
}
func (op *filterOperatorSingle) err(ctx context.Context, item Item, dst chan<- Item, operatorOptions operatorOptions) {
defaultErrorFuncOperator(ctx, item, dst, operatorOptions)
}
func (op *filterOperatorSingle) end(_ context.Context, _ chan<- Item) {
}
func (op *filterOperatorSingle) gatherNext(_ context.Context, _ Item, _ chan<- Item, _ operatorOptions) {
}
// Run creates an observer without consuming the emitted items.
func (s *SingleImpl) Run(opts ...Option) Disposed {
dispose := make(chan struct{})
option := parseOptions(opts...)
ctx := option.buildContext(s.parent)
go func() {
defer close(dispose)
observe := s.Observe(opts...)
for {
select {
case <-ctx.Done():
return
case _, ok := <-observe:
if !ok {
return
}
}
}
}()
return dispose
}