@Override public void request(long n) { if (once) { return; } if (n < 0L) { throw new IllegalStateException("n >= required but it was " + n); } if (n != 0L) { once = true; Subscriber<? super T> a = actual; if (a.isUnsubscribed()) { return; } T v = value; try { a.onNext(v); } catch (Throwable e) { Exceptions.throwOrReport(e, a, v); return; } if (a.isUnsubscribed()) { return; } a.onCompleted(); } }
@Override public void onError(Throwable e) { long p = produced; if (p != 0L) { arbiter.produced(p); } Observable<T> o; try { o = onError.call(e); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); actual.onError(new CompositeException(e, ex)); return; } if (o == null) { actual.onError( new NullPointerException("The onError function returned a null Observable.")); } else { o.unsafeSubscribe(new ResumeSubscriber<T>(actual, arbiter)); } }
@Override public Subscriber<? super T> call(final Subscriber<? super Map<K, V>> subscriber) { Map<K, V> localMap; try { localMap = mapFactory.call(); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); subscriber.onError(ex); Subscriber<? super T> parent = Subscribers.empty(); parent.unsubscribe(); return parent; } final Map<K, V> fLocalMap = localMap; return new Subscriber<T>(subscriber) { private Map<K, V> map = fLocalMap; @Override public void onStart() { request(Long.MAX_VALUE); } @Override public void onNext(T v) { K key; V value; try { key = keySelector.call(v); value = valueSelector.call(v); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); subscriber.onError(ex); return; } map.put(key, value); } @Override public void onError(Throwable e) { map = null; subscriber.onError(e); } @Override public void onCompleted() { Map<K, V> map0 = map; map = null; subscriber.onNext(map0); subscriber.onCompleted(); } }; }
@Override public void call(final Subscriber<? super T> subscriber) { try { // create the resource final Resource resource = resourceFactory.call(); // create an action/subscription that disposes only once final DisposeAction<Resource> disposeOnceOnly = new DisposeAction<Resource>(dispose, resource); // dispose on unsubscription subscriber.add(disposeOnceOnly); // create the observable final Observable<? extends T> source = observableFactory // create the observable .call(resource); final Observable<? extends T> observable; // supplement with on termination disposal if requested if (disposeEagerly) observable = source // dispose on completion or error .doOnTerminate(disposeOnceOnly); else observable = source; try { // start observable.unsafeSubscribe(Subscribers.wrap(subscriber)); } catch (Throwable e) { Throwable disposeError = disposeEagerlyIfRequested(disposeOnceOnly); Exceptions.throwIfFatal(e); Exceptions.throwIfFatal(disposeError); if (disposeError != null) subscriber.onError(new CompositeException(Arrays.asList(e, disposeError))); else // propagate error subscriber.onError(e); } } catch (Throwable e) { // then propagate error Exceptions.throwOrReport(e, subscriber); } }
@Override public void call() { Subscriber<? super T> a = actual; if (a.isUnsubscribed()) { return; } T v = value; try { a.onNext(v); } catch (Throwable e) { Exceptions.throwOrReport(e, a, v); return; } if (a.isUnsubscribed()) { return; } a.onCompleted(); }
void emitLoop() { final Subscriber<? super T> c = child; outer: for (; ; ) { long localRequested; Producer localProducer; Object localTerminal; List<T> q; synchronized (this) { localRequested = missedRequested; localProducer = missedProducer; localTerminal = missedTerminal; q = queue; if (localRequested == 0L && localProducer == null && q == null && localTerminal == null) { emitting = false; return; } missedRequested = 0L; missedProducer = null; queue = null; missedTerminal = null; } boolean empty = q == null || q.isEmpty(); if (localTerminal != null) { if (localTerminal != Boolean.TRUE) { c.onError((Throwable) localTerminal); return; } else if (empty) { c.onCompleted(); return; } } long e = 0; if (q != null) { for (T v : q) { if (c.isUnsubscribed()) { return; } else if (hasError) { continue outer; // if an error has been set, shortcut the loop and act on it } try { c.onNext(v); } catch (Throwable ex) { Exceptions.throwOrReport(ex, c, v); return; } } e += q.size(); } long r = requested; // if requested is max, we don't do any accounting if (r != Long.MAX_VALUE) { // if there were missing requested, add it up if (localRequested != 0L) { long u = r + localRequested; if (u < 0) { u = Long.MAX_VALUE; } r = u; } // if there were emissions and we don't run on max since the last check, subtract if (e != 0L && r != Long.MAX_VALUE) { long u = r - e; if (u < 0) { throw new IllegalStateException("More produced than requested"); } r = u; } requested = r; } if (localProducer != null) { if (localProducer == NULL_PRODUCER) { currentProducer = null; } else { currentProducer = localProducer; if (r != 0L) { localProducer.request(r); } } } else { Producer p = currentProducer; if (p != null && localRequested != 0L) { p.request(localRequested); } } } }