-
Notifications
You must be signed in to change notification settings - Fork 8
/
flatmap.go
142 lines (121 loc) · 3.06 KB
/
flatmap.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
130
131
132
133
134
135
136
137
138
139
140
141
142
package mono
import (
"context"
"sync/atomic"
"github.com/jjeffcaii/reactor-go"
"github.com/jjeffcaii/reactor-go/hooks"
"github.com/pkg/errors"
)
type flatMapStat int32
const (
_ flatMapStat = iota
flatMapStatInnerReady
flatMapStatInnerComplete
flatMapStatError
flatMapStatComplete
)
type innerFlatMapSubscriber struct {
parent *flatMapSubscriber
}
func (in *innerFlatMapSubscriber) OnError(err error) {
if atomic.CompareAndSwapInt32(&in.parent.stat, int32(flatMapStatInnerReady), int32(flatMapStatError)) {
in.parent.actual.OnError(err)
}
}
func (in *innerFlatMapSubscriber) OnNext(v Any) {
if atomic.LoadInt32(&in.parent.stat) != int32(flatMapStatInnerReady) {
hooks.Global().OnNextDrop(v)
return
}
in.parent.actual.OnNext(v)
in.OnComplete()
}
func (in *innerFlatMapSubscriber) OnSubscribe(_ context.Context, s reactor.Subscription) {
s.Request(reactor.RequestInfinite)
}
func (in *innerFlatMapSubscriber) OnComplete() {
if atomic.CompareAndSwapInt32(&in.parent.stat, int32(flatMapStatInnerReady), int32(flatMapStatInnerComplete)) {
in.parent.actual.OnComplete()
}
}
type flatMapSubscriber struct {
actual reactor.Subscriber
mapper FlatMapper
stat int32
ctx context.Context
}
func (p *flatMapSubscriber) Request(n int) {
}
func (p *flatMapSubscriber) Cancel() {
atomic.CompareAndSwapInt32(&p.stat, 0, statCancel)
}
func (p *flatMapSubscriber) OnComplete() {
if atomic.CompareAndSwapInt32(&p.stat, 0, int32(flatMapStatComplete)) {
p.actual.OnComplete()
}
}
func (p *flatMapSubscriber) OnError(err error) {
if !atomic.CompareAndSwapInt32(&p.stat, 0, int32(flatMapStatError)) {
hooks.Global().OnErrorDrop(err)
return
}
p.actual.OnError(err)
}
func (p *flatMapSubscriber) OnNext(v Any) {
if !atomic.CompareAndSwapInt32(&p.stat, 0, int32(flatMapStatInnerReady)) {
hooks.Global().OnNextDrop(v)
return
}
nextMono, err := p.computeNextMono(v)
if err != nil {
p.actual.OnError(err)
return
}
inner := &innerFlatMapSubscriber{
parent: p,
}
nextMono.SubscribeWith(p.ctx, inner)
}
func (p *flatMapSubscriber) computeNextMono(v Any) (next Mono, err error) {
defer func() {
rec := recover()
if rec == nil {
return
}
if e, ok := rec.(error); ok {
err = errors.WithStack(e)
} else {
err = errors.Errorf("%v", rec)
}
}()
next = p.mapper(v)
if next == nil {
err = errors.New("the FlatMap result is nil")
}
return
}
func (p *flatMapSubscriber) OnSubscribe(ctx context.Context, s reactor.Subscription) {
p.ctx = ctx
s.Request(reactor.RequestInfinite)
}
func newFlatMapSubscriber(actual reactor.Subscriber, mapper FlatMapper) *flatMapSubscriber {
return &flatMapSubscriber{
actual: actual,
mapper: mapper,
}
}
type monoFlatMap struct {
source reactor.RawPublisher
mapper FlatMapper
}
func (m *monoFlatMap) SubscribeWith(ctx context.Context, actual reactor.Subscriber) {
s := newFlatMapSubscriber(actual, m.mapper)
actual.OnSubscribe(ctx, s)
m.source.SubscribeWith(ctx, s)
}
func newMonoFlatMap(source reactor.RawPublisher, mapper FlatMapper) *monoFlatMap {
return &monoFlatMap{
source: source,
mapper: mapper,
}
}