-
Notifications
You must be signed in to change notification settings - Fork 8
/
op_peek.go
153 lines (133 loc) · 3.03 KB
/
op_peek.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
143
144
145
146
147
148
149
150
151
152
153
package mono
import (
"context"
"sync/atomic"
"github.com/jjeffcaii/reactor-go"
"github.com/jjeffcaii/reactor-go/internal"
"github.com/pkg/errors"
)
type monoPeek struct {
source reactor.RawPublisher
onSubscribeCall reactor.FnOnSubscribe
onNextCall reactor.FnOnNext
onErrorCall reactor.FnOnError
onCompleteCall reactor.FnOnComplete
onRequestCall reactor.FnOnRequest
onCancelCall reactor.FnOnCancel
}
func (p *monoPeek) Parent() reactor.RawPublisher {
return p.source
}
type peekSubscriber struct {
actual reactor.Subscriber
parent *monoPeek
s reactor.Subscription
stat int32
}
func newMonoPeek(source reactor.RawPublisher, first monoPeekOption, others ...monoPeekOption) *monoPeek {
m := &monoPeek{
source: source,
}
first(m)
for _, value := range others {
value(m)
}
return m
}
func newPeekSubscriber(parent *monoPeek, actual reactor.Subscriber) *peekSubscriber {
return &peekSubscriber{
parent: parent,
actual: actual,
}
}
func (p *peekSubscriber) Request(n int) {
if call := p.parent.onRequestCall; call != nil {
call(n)
}
p.s.Request(n)
}
func (p *peekSubscriber) Cancel() {
if call := p.parent.onCancelCall; call != nil {
call()
}
p.s.Cancel()
}
func (p *peekSubscriber) OnComplete() {
if !atomic.CompareAndSwapInt32(&p.stat, 0, statComplete) {
return
}
if call := p.parent.onCompleteCall; call != nil {
call()
}
p.actual.OnComplete()
}
func (p *peekSubscriber) OnError(err error) {
if !atomic.CompareAndSwapInt32(&p.stat, 0, statError) {
return
}
if call := p.parent.onErrorCall; call != nil {
call(err)
}
p.actual.OnError(err)
}
func (p *peekSubscriber) OnNext(v Any) {
if atomic.LoadInt32(&p.stat) != 0 {
return
}
if call := p.parent.onNextCall; call != nil {
defer func() {
rec := recover()
if rec == nil {
return
}
if e, ok := rec.(error); ok {
p.OnError(errors.WithStack(e))
} else {
p.OnError(errors.Errorf("%v", rec))
}
}()
if err := call(v); err != nil {
defer p.OnError(err)
}
}
p.actual.OnNext(v)
}
func (p *peekSubscriber) OnSubscribe(ctx context.Context, s reactor.Subscription) {
if p.s != nil {
panic(internal.ErrCallOnSubscribeDuplicated)
}
p.s = s
if call := p.parent.onSubscribeCall; call != nil {
call(ctx, p)
}
p.actual.OnSubscribe(ctx, p)
}
func (p *monoPeek) SubscribeWith(ctx context.Context, s reactor.Subscriber) {
p.source.SubscribeWith(ctx, newPeekSubscriber(p, s))
}
type monoPeekOption func(*monoPeek)
func peekNext(fn reactor.FnOnNext) monoPeekOption {
return func(peek *monoPeek) {
peek.onNextCall = fn
}
}
func peekComplete(fn reactor.FnOnComplete) monoPeekOption {
return func(peek *monoPeek) {
peek.onCompleteCall = fn
}
}
func peekCancel(fn reactor.FnOnCancel) monoPeekOption {
return func(peek *monoPeek) {
peek.onCancelCall = fn
}
}
func peekError(fn reactor.FnOnError) monoPeekOption {
return func(peek *monoPeek) {
peek.onErrorCall = fn
}
}
func peekSubscribe(fn reactor.FnOnSubscribe) monoPeekOption {
return func(peek *monoPeek) {
peek.onSubscribeCall = fn
}
}