@Override public void run() { System.out.println("Doing something expensive"); reactiveXBoilerplate.sleep(3000); subscriber.onNext("Expensive result"); subscriber.onCompleted(); }
@Override public void onNext(T t) { synchronized (this) { if (emitting) { List<T> q = queue; if (q == null) { q = new ArrayList<T>(4); queue = q; } q.add(t); return; } } boolean skipFinal = false; try { child.onNext(t); long r = requested; if (r != Long.MAX_VALUE) { requested = r - 1; } emitLoop(); skipFinal = true; } finally { if (!skipFinal) { synchronized (this) { emitting = false; } } } }
@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 call(Subscriber<? super Long> op) { long l = 1; while (!op.isUnsubscribed()) { op.onNext(l++); } op.onCompleted(); }
@Override public void onViewDetachedFromWindow(View v) { if (!isUnsubscribed()) { Subscriber<? super View> originalSubscriber = subscriber; unsubscribe(); originalSubscriber.onNext(v); originalSubscriber.onCompleted(); } }
@Override public void call(Subscriber<? super Integer> subscriber) { subscriber.onNext(1); subscriber.onNext(2); if (throwAnErrorCounter > 4) { throwAnErrorCounter--; subscriber.onError(new FooException()); return; } if (throwAnErrorCounter > 0) { throwAnErrorCounter--; subscriber.onError(new BooException()); return; } subscriber.onNext(3); subscriber.onNext(4); subscriber.onCompleted(); }
@Override public void call(final Subscriber<? super T> subscriber) { final Realm realm = Realm.getInstance(context); thread = Thread.currentThread(); subscriber.add( Subscriptions.create( new Action0() { @Override public void call() { if (thread != null && !thread.isInterrupted()) { thread.interrupt(); } } })); boolean interrupted = false; boolean withError = false; T object = null; try { realm.beginTransaction(); object = get(realm); interrupted = thread.isInterrupted(); if (object != null && !interrupted) { realm.commitTransaction(); } else { realm.cancelTransaction(); } } catch (RuntimeException e) { realm.cancelTransaction(); subscriber.onError(new RealmException("Error during transaction.", e)); withError = true; } catch (Error e) { realm.cancelTransaction(); subscriber.onError(e); withError = true; } if (!interrupted && !withError) { subscriber.onNext(object); } try { realm.close(); } catch (RealmException ex) { subscriber.onError(ex); withError = true; } thread = null; if (!withError) { subscriber.onCompleted(); } if (interrupted) { Thread.currentThread().interrupt(); } }
@Override public void onPreviewFrame(byte[] data) { if (subscriber != null && !subscriber.isUnsubscribed() && rxCamera.isOpenCamera()) { if (data == null || data.length == 0) { subscriber.onError(new CameraDataNullException()); } RxCameraData rxCameraData = new RxCameraData(); rxCameraData.cameraData = data; rxCameraData.rotateMatrix = rxCamera.getRotateMatrix(); subscriber.onNext(rxCameraData); } }
private void triggerServiceActive() { try { Iterator localIterator = this.networkSubscribers.iterator(); while (localIterator.hasNext()) { Subscriber localSubscriber = (Subscriber)localIterator.next(); localSubscriber.onNext(this.mNetworkManager); localSubscriber.onCompleted(); localIterator.remove(); } } finally {} }
@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(); }
private void onScanPerformed() { Log.d("WifiService", "The scan has been performed."); unregisterReceiver(wifiScanReceiver); wifiScanReceiver = null; Sample sample = new Sample(wifiManager.getScanResults()); Subscriber<? super Sample> subscriber = wifiScanSubscribers.poll(); if (subscriber != null) { Log.d("WifiService", "The sample has been taken."); subscriber.onNext(sample); } if (!wifiScanSubscribers.isEmpty()) { Log.d("WifiService", "The are requests to take a sample in the queue."); performScan(); } }
@Override public void onNext(T args) { child.onNext(args); boolean stop = false; try { stop = stopPredicate.call(args); } catch (Throwable e) { done = true; child.onError(e); unsubscribe(); return; } if (stop) { done = true; child.onCompleted(); unsubscribe(); } }
private static void produceValuesAndAnError(Subscriber s) { Subscriber subscriber = (Subscriber) s; try { for (int ii = 0; ii < 50; ii++) { if (!subscriber.isUnsubscribed()) { subscriber.onNext("Pushed value " + ii); } if (ii == 5) { throw new Throwable("Something has gone wrong here"); } } if (!subscriber.isUnsubscribed()) { subscriber.onCompleted(); } } catch (Throwable throwable) { subscriber.onError(throwable); } }
private Observable<Navajo> processNavajoEvent( NavajoStreamEvent n, Subscriber<? super Navajo> subscriber) { switch (n.type()) { case NAVAJO_STARTED: createHeader((NavajoHead) n.body()); return Observable.<Navajo>empty(); case MESSAGE_STARTED: Message prMessage = null; if (!messageStack.isEmpty()) { prMessage = messageStack.peek(); } String mode = (String) n.attribute("mode"); Message msg = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_SIMPLE); msg.setMode(mode); if (prMessage == null) { assemble.addMessage(msg); } else { prMessage.addMessage(msg); } messageStack.push(msg); tagStack.push(n.path()); return Observable.<Navajo>empty(); case MESSAGE: Message msgParent = messageStack.pop(); tagStack.pop(); Msg mm = (Msg) n.body(); List<Prop> msgProps = mm.properties(); for (Prop e : msgProps) { msgParent.addProperty(createTmlProperty(e)); } for (Entry<String, Binary> e : pushBinaries.entrySet()) { msgParent.addProperty(createBinaryProperty(e.getKey(), e.getValue())); } pushBinaries.clear(); return Observable.<Navajo>empty(); case ARRAY_STARTED: tagStack.push(n.path()); String path = currentPath(); AtomicInteger cnt = arrayCounts.get(path); if (cnt == null) { cnt = new AtomicInteger(); arrayCounts.put(path, cnt); } // cnt.incrementAndGet(); Message parentMessage = null; if (!messageStack.isEmpty()) { parentMessage = messageStack.peek(); } Message arr = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_ARRAY); if (parentMessage == null) { assemble.addMessage(arr); } else { parentMessage.addMessage(arr); } messageStack.push(arr); return Observable.<Navajo>empty(); case ARRAY_DONE: String apath = currentPath(); arrayCounts.remove(apath); this.messageStack.pop(); return Observable.<Navajo>empty(); case ARRAY_ELEMENT_STARTED: String arrayElementName = tagStack.peek(); String arrayPath = currentPath(); AtomicInteger currentCount = arrayCounts.get(arrayPath); String ind = "@" + currentCount.getAndIncrement(); tagStack.push(ind); arrayPath = currentPath(); Message newElt = NavajoFactory.getInstance() .createMessage(assemble, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT); Message arrParent = messageStack.peek(); arrParent.addElement(newElt); messageStack.push(newElt); return Observable.<Navajo>empty(); case ARRAY_ELEMENT: tagStack.pop(); Message elementParent = messageStack.pop(); Msg msgElement = (Msg) n.body(); List<Prop> elementProps = msgElement.properties(); for (Prop e : elementProps) { elementParent.addProperty(createTmlProperty(e)); } for (Entry<String, Binary> e : pushBinaries.entrySet()) { elementParent.addProperty(createBinaryProperty(e.getKey(), e.getValue())); } pushBinaries.clear(); return Observable.<Navajo>empty(); case MESSAGE_DEFINITION_STARTED: // TODO return Observable.<Navajo>empty(); case MESSAGE_DEFINITION: // TODO // tagStack.push(n.path()); // deferredMessages.get(stripIndex(n.path())).setDefinitionMessage((Message) n.body()); return Observable.<Navajo>empty(); case NAVAJO_DONE: if (subscriber != null) { subscriber.onNext(assemble); } return Observable.<Navajo>just(assemble); case BINARY_STARTED: try { String name = n.path(); this.currentBinary = new Binary(); this.currentBinary.startPushRead(); this.pushBinaries.put(name, currentBinary); return Observable.<Navajo>empty(); } catch (IOException e1) { return Observable.error(e1); } case BINARY_CONTENT: try { if (this.currentBinary == null) { // whoops; } this.currentBinary.pushContent((String) n.body()); return Observable.<Navajo>empty(); } catch (IOException e1) { return Observable.error(e1); } case BINARY_DONE: try { this.currentBinary.finishPushContent(); return Observable.<Navajo>empty(); } catch (IOException e1) { return Observable.error(e1); } default: return Observable.<Navajo>empty(); } }
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); } } } }
@Override public void onNext(Void aVoid) { child.onNext(aVoid); }
@Override public void call(Subscriber<? super String> sub) { sub.onNext("Hello, world!"); sub.onCompleted(); }
@Override public void onNext(T t) { produced++; actual.onNext(t); }
@Override public void onNext(T t) { actual.onNext(t); }