GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — RxJava/src/main/java/rx/Subscriber.java at 1.x · javalover/RxJava Expand file tree/ Copy pathSubscriber.javaMore file actions 215 lines (200 loc) · 8.21 KB / Copy pathSubscriber.javaFile metadata and controls215 lines (200 loc) · 8.21 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 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 /** * Copyright 2014 Netflix, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package rx; import rx.internal.util.SubscriptionList; /** * Provides a mechanism for receiving push-based notifications from Observables, and permits manual * unsubscribing from these Observables. * <p> * After a Subscriber calls an {@link Observable}'s {@link Observable#subscribe subscribe} method, the * {@link Observable} calls the Subscriber's {@link #onNext} method to emit items. A well-behaved * {@link Observable} will call a Subscriber's {@link #onCompleted} method exactly once or the Subscriber's * {@link #onError} method exactly once. * * @see <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation: Observable</a> * @param <T> * the type of items the Subscriber expects to observe */ public abstract class Subscriber<T> implements Observer<T>, Subscription { // represents requested not set yet private static final Long NOT_SET = Long.MIN_VALUE; private final SubscriptionList subscriptions; private final Subscriber<?> subscriber; /* protected by `this` */ private Producer producer; /* protected by `this` */ private long requested = NOT_SET; // default to not set protected Subscriber() { this(null, false); } /** * Construct a Subscriber by using another Subscriber for backpressure and * for holding the subscription list (when <code>this.add(sub)</code> is * called this will in fact call <code>subscriber.add(sub)</code>). * * @param subscriber * the other Subscriber */ protected Subscriber(Subscriber<?> subscriber) { this(subscriber, true); } /** * Construct a Subscriber by using another Subscriber for backpressure and * optionally for holding the subscription list (if * <code>shareSubscriptions</code> is <code>true</code> then when * <code>this.add(sub)</code> is called this will in fact call * <code>subscriber.add(sub)</code>). * <p> * To retain the chaining of subscribers when setting * <code>shareSubscriptions</code> to <code>false</code>, add the created * instance to {@code subscriber} via {@link #add}. * * @param subscriber * the other Subscriber * @param shareSubscriptions * {@code true} to share the subscription list in {@code subscriber} with * this instance * @since 1.0.6 */ protected Subscriber(Subscriber<?> subscriber, boolean shareSubscriptions) { this.subscriber = subscriber; this.subscriptions = shareSubscriptions && subscriber != null ? subscriber.subscriptions : new SubscriptionList(); } /** * 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 final void add(Subscription s) { subscriptions.add(s); } @Override public final void unsubscribe() { subscriptions.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 */ @Override public final boolean isUnsubscribed() { return subscriptions.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. */ 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. * <p> * Requests are additive but if a sequence of requests totals more than {@code Long.MAX_VALUE} then * {@code Long.MAX_VALUE} requests will be actioned and the extras <i>may</i> be ignored. Arriving at * {@code Long.MAX_VALUE} by addition of requests cannot be assumed to disable backpressure. For example, * the code below may result in {@code Long.MAX_VALUE} requests being actioned only. * * <pre> * request(100); * request(Long.MAX_VALUE-1); * </pre> * * @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 * @throws IllegalArgumentException * if {@code n} is negative */ protected final void request(long n) { if (n < 0) { throw new IllegalArgumentException("number requested cannot be negative: " + n); } // if producer is set then we will request from it // otherwise we increase the requested count by n Producer producerToRequestFrom = null; synchronized (this) { if (producer != null) { producerToRequestFrom = producer; } else { addToRequested(n); return; } } // after releasing lock (we should not make requests holding a lock) producerToRequestFrom.request(n); } private void addToRequested(long n) { if (requested == NOT_SET) { requested = n; } else { final long total = requested + n; // check if overflow occurred if (total < 0) { requested = Long.MAX_VALUE; } else { requested = total; } } } /** * If other subscriber is set (by calling constructor * {@link #Subscriber(Subscriber)} or * {@link #Subscriber(Subscriber, boolean)}) then this method calls * <code>setProducer</code> on the other subscriber. If the other subscriber * is not set and no requests have been made to this subscriber then * <code>p.request(Long.MAX_VALUE)</code> is called. If the other subscriber * is not set and some requests have been made to this subscriber then * <code>p.request(n)</code> is called where n is the accumulated requests * to this subscriber. * * @param p * producer to be used by this subscriber or the other subscriber * (or recursively its other subscriber) to make requests from */ public void setProducer(Producer p) { long toRequest; boolean passToSubscriber = false; synchronized (this) { toRequest = requested; producer = p; if (subscriber != null) { // middle operator ... we pass thru unless a request has been made if (toRequest == NOT_SET) { // we pass-thru to the next producer as nothing has been requested passToSubscriber = true; } } } // do after releasing lock if (passToSubscriber) { subscriber.setProducer(producer); } else { // we execute the request with whatever has been requested (or Long.MAX_VALUE) if (toRequest == NOT_SET) { producer.request(Long.MAX_VALUE); } else { producer.request(toRequest); } } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |