本文整理汇总了Java中io.reactivex.disposables.Disposables类的典型用法代码示例。如果您正苦于以下问题:Java Disposables类的具体用法?Java Disposables怎么用?Java Disposables使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Disposables类属于io.reactivex.disposables包,在下文中一共展示了Disposables类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: subscribe
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override public void subscribe(final ObservableEmitter<FirebaseAuth> emitter) {
final FirebaseAuth.AuthStateListener listener = new FirebaseAuth.AuthStateListener() {
@Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (!emitter.isDisposed()) {
emitter.onNext(firebaseAuth);
}
}
};
instance.addAuthStateListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override public void run() throws Exception {
instance.removeAuthStateListener(listener);
}
}));
}
示例2: schedule
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
if (run == null) throw new NullPointerException("run == null");
if (unit == null) throw new NullPointerException("unit == null");
if (disposed) {
return Disposables.disposed();
}
run = RxJavaPlugins.onSchedule(run);
ScheduledRunnable scheduled = new ScheduledRunnable(handler, run);
Message message = Message.obtain(handler, scheduled);
message.obj = this; // Used as token for batch disposal of this worker's runnables.
handler.sendMessageDelayed(message, Math.max(0L, unit.toMillis(delay)));
// Re-check disposed state for removing in case we were racing a call to dispose().
if (disposed) {
handler.removeCallbacks(scheduled);
return Disposables.disposed();
}
return scheduled;
}
示例3: disposeInUiThread
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
private Disposable disposeInUiThread(final Action action) {
return Disposables.fromAction(new Action() {
@Override public void run() throws Exception {
if (Looper.getMainLooper() == Looper.myLooper()) {
action.run();
} else {
final Scheduler.Worker inner = AndroidSchedulers.mainThread().createWorker();
inner.schedule(new Runnable() {
@Override public void run() {
try {
action.run();
} catch (Exception e) {
onError("Could not unregister receiver in UI Thread", e);
}
inner.dispose();
}
});
}
}
});
}
示例4: schedule
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
if (run == null)
throw new NullPointerException("run == null");
if (unit == null)
throw new NullPointerException("unit == null");
if (disposed) {
return Disposables.disposed();
}
run = RxJavaPlugins.onSchedule(run);
ScheduledRunnable scheduled = new ScheduledRunnable(run);
executeRunnable(title, delay, unit, scheduled);
// Re-check disposed state for removing in case we were racing a
// call to dispose().
if (disposed) {
return Disposables.disposed();
}
return scheduled;
}
示例5: schedule
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
if (run == null)
throw new NullPointerException("run == null");
if (unit == null)
throw new NullPointerException("unit == null");
if (disposed) {
return Disposables.disposed();
}
run = RxJavaPlugins.onSchedule(run);
ScheduledRunnable scheduled = new ScheduledRunnable(run);
executeRunnable(display, delay, unit, scheduled);
// Re-check disposed state for removing in case we were racing a
// call to dispose().
if (disposed) {
return Disposables.disposed();
}
return scheduled;
}
示例6: subscribe
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public void subscribe(final ObservableEmitter<DocumentSnapshot> emitter) throws Exception {
final EventListener<DocumentSnapshot> listener = new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
if (!emitter.isDisposed()) {
if (e == null) {
emitter.onNext(documentSnapshot);
} else {
emitter.onError(e);
}
}
}
};
registration = documentReference.addSnapshotListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
registration.remove();
}
}));
}
示例7: openDatabase
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
public void openDatabase() {
disposable = Observable.create((ObservableOnSubscribe<Realm>) emitter -> {
final Realm observableRealm = Realm.getDefaultInstance();
final RealmChangeListener<Realm> listener = realm -> {
if(!emitter.isDisposed()) {
emitter.onNext(observableRealm);
}
};
observableRealm.addChangeListener(listener);
emitter.setDisposable(Disposables.fromAction(() -> {
observableRealm.removeChangeListener(listener);
observableRealm.close();
}));
emitter.onNext(observableRealm);
}).subscribeOn(looperScheduler.getScheduler()).unsubscribeOn(looperScheduler.getScheduler()).subscribe();
}
示例8: createResults
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
private Observable<List<Task>> createResults(QuerySelector<DbTask> querySelector) {
return Observable.create((ObservableOnSubscribe<List<Task>>) emitter -> {
Realm realm = Realm.getDefaultInstance();
final RealmResults<DbTask> dbTasks = querySelector.createQuery(realm);
final RealmChangeListener<RealmResults<DbTask>> realmChangeListener = element -> {
if(element.isLoaded() && !emitter.isDisposed()) {
List<Task> tasks = mapFrom(element);
if(!emitter.isDisposed()) {
emitter.onNext(tasks);
}
}
};
emitter.setDisposable(Disposables.fromAction(() -> {
if(dbTasks.isValid()) {
dbTasks.removeChangeListener(realmChangeListener);
}
realm.close();
}));
dbTasks.addChangeListener(realmChangeListener);
}).subscribeOn(looperScheduler.getScheduler()).unsubscribeOn(looperScheduler.getScheduler());
}
示例9: disposeInUiThread
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
/**
* Disposes an action in UI Thread
*
* @param dispose action to be executed
* @return Disposable object
*/
private Disposable disposeInUiThread(final Action dispose) {
return Disposables.fromAction(new Action() {
@Override public void run() throws Exception {
if (Looper.getMainLooper() == Looper.myLooper()) {
dispose.run();
} else {
final Scheduler.Worker inner = AndroidSchedulers.mainThread().createWorker();
inner.schedule(new Runnable() {
@Override public void run() {
try {
dispose.run();
} catch (Exception exception) {
onError("Could not unregister receiver in UI Thread", exception);
}
inner.dispose();
}
});
}
}
});
}
示例10: subscribe
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override public void subscribe(final ObservableEmitter<DataSnapshot> emitter) {
final ValueEventListener listener = new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
if (!emitter.isDisposed()) {
emitter.onNext(dataSnapshot);
}
}
@Override public void onCancelled(DatabaseError databaseError) {
if (!emitter.isDisposed()) {
emitter.onError(databaseError.toException());
}
}
};
ref.addValueEventListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override public void run() throws Exception {
ref.removeEventListener(listener);
}
}));
}
示例11: schedule
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public Disposable schedule(final Runnable action, long delayTime, TimeUnit unit) {
if (innerSubscription.isDisposed()) {
return Disposables.empty();
}
final ScheduledAction scheduledAction = new ScheduledAction(action, operationQueue);
final ScheduledExecutorService executor = IOSScheduledExecutorPool.getInstance();
Future<?> future;
if (delayTime <= 0) {
future = executor.submit(scheduledAction);
} else {
future = executor.schedule(scheduledAction, delayTime, unit);
}
scheduledAction.add(Disposables.fromFuture(future));
scheduledAction.addParent(innerSubscription);
return scheduledAction;
}
示例12: foundWithUnconditionalOnCompleteAfter
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Test
public void foundWithUnconditionalOnCompleteAfter() {
new Observable<Integer>() {
@Override
protected void subscribeActual(Observer<? super Integer> s) {
s.onSubscribe(Disposables.empty());
s.onNext(10);
s.onComplete();
}
}
.compose(ObservableTransformers.indexOf(new Predicate<Integer>() {
@Override
public boolean test(Integer v) throws Exception {
return v == 10;
}
}))
.test()
.assertResult(0L);
}
示例13: SingleResponseReceiver
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
public SingleResponseReceiver(ClientCall<?, RespT> call) {
this.call = call;
this.source = new SingleSource<RespT>() {
@Override
public void subscribe(SingleObserver<? super RespT> observer) {
responseObserver = observer;
// todo which disposable should be used here
observer.onSubscribe(Disposables.disposed());
// start call until response gets subscribed
startCall();
if (error != null) {
responseObserver.onError(error);
error = null;
}
}
};
}
示例14: subscribe
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
/**
* @param emitter
*/
@Override
public void subscribe(final ObservableEmitter<FirebaseAuth> emitter) {
final FirebaseAuth.AuthStateListener listener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (!emitter.isDisposed()) {
emitter.onNext(firebaseAuth);
}
}
};
instance.addAuthStateListener(listener);
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
instance.removeAuthStateListener(listener);
}
}));
}
示例15: subscribe
import io.reactivex.disposables.Disposables; //导入依赖的package包/类
@Override
public void subscribe(ObservableEmitter<Intent> emitter) throws Exception {
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
emitter.onNext(intent);
}
};
emitter.setDisposable(Disposables.fromRunnable(() -> { // thank you Jake W.
try {
if (contextWeakReference != null && contextWeakReference.get() != null) {
contextWeakReference.get().unregisterReceiver(broadcastReceiver);
}
} catch (IllegalArgumentException ignored) {}
}));
if (contextWeakReference != null && contextWeakReference.get() != null) {
contextWeakReference.get().registerReceiver(broadcastReceiver, intentFilter);
}
}