-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiSubscriber.java
More file actions
123 lines (110 loc) · 3.76 KB
/
BiSubscriber.java
File metadata and controls
123 lines (110 loc) · 3.76 KB
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
package rx;
import rx.internal.util.SubscriptionList;
public abstract class BiSubscriber<T0, T1> implements BiObserver<T0, T1>, Subscription {
protected final SubscriptionList cs;
private final BiSubscriber<?, ?> op;
/* protected by `this` */
private Producer p;
/* protected by `this` */
private long requested = Long.MIN_VALUE; // default to not set
protected BiSubscriber() {
this.op = null;
this.cs = new SubscriptionList();
}
protected BiSubscriber(BiSubscriber<?, ?> op) {
this.op = op;
this.cs = op.cs;
}
protected BiSubscriber(Subscriber<?> op) {
this();
this.cs.add(op);
}
/**
* Adds a {@link Subscription} to this Subscriber's list of subscriptions if this list is not marked as
* unsubscribed. If the list <em>is</em> marked as unsubscribed, {@code add} will indicate this by
* explicitly unsubscribing the new {@code Subscription} as well.
*
* @param s
* the {@code Subscription} to add
*/
public void add(Subscription s) {
cs.add(s);
}
@Override
public void unsubscribe() {
cs.unsubscribe();
}
/**
* Indicates whether this Subscriber has unsubscribed from its list of subscriptions.
*
* @return {@code true} if this Subscriber has unsubscribed from its subscriptions, {@code false} otherwise
*/
public final boolean isUnsubscribed() {
return cs.isUnsubscribed();
}
/**
* This method is invoked when the Subscriber and Observable have been connected but the Observable has
* not yet begun to emit items or send notifications to the Subscriber. Override this method to add any
* useful initialization to your subscription, for instance to initiate backpressure.
*
* @since 0.20
*/
public void onStart() {
// do nothing by default
}
/**
* Request a certain maximum number of emitted items from the Observable this Subscriber is subscribed to.
* This is a way of requesting backpressure. To disable backpressure, pass {@code Long.MAX_VALUE} to this
* method.
*
* @param n the maximum number of items you want the Observable to emit to the Subscriber at this time, or
* {@code Long.MAX_VALUE} if you want the Observable to emit items at its own pace
* @since 0.20
*/
protected final void request(long n) {
Producer shouldRequest = null;
synchronized (this) {
if (p != null) {
shouldRequest = p;
} else {
requested = n;
}
}
// after releasing lock
if (shouldRequest != null) {
shouldRequest.request(n);
}
}
/**
* @warn javadoc description missing
* @warn param producer not described
* @param producer
* @since 0.20
*/
public void setProducer(Producer producer) {
long toRequest;
boolean setProducer = false;
synchronized (this) {
toRequest = requested;
p = producer;
if (op != null) {
// middle operator ... we pass thru unless a request has been made
if (toRequest == Long.MIN_VALUE) {
// we pass-thru to the next producer as nothing has been requested
setProducer = true;
}
}
}
// do after releasing lock
if (setProducer) {
op.setProducer(p);
} else {
// we execute the request with whatever has been requested (or Long.MAX_VALUE)
if (toRequest == Long.MIN_VALUE) {
p.request(Long.MAX_VALUE);
} else {
p.request(toRequest);
}
}
}
}