本文整理汇总了Java中io.reactivex.functions.Cancellable类的典型用法代码示例。如果您正苦于以下问题:Java Cancellable类的具体用法?Java Cancellable怎么用?Java Cancellable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Cancellable类属于io.reactivex.functions包,在下文中一共展示了Cancellable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: observable
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* Using the returned Observable, you can be notified about data changes.
* Once a transaction is committed, you will get info on classes with changed Objects.
*/
public static <T> Observable<Class> observable(final BoxStore boxStore) {
return Observable.create(new ObservableOnSubscribe<Class>() {
@Override
public void subscribe(final ObservableEmitter<Class> emitter) throws Exception {
final DataSubscription dataSubscription = boxStore.subscribe().observer(new DataObserver<Class>() {
@Override
public void onData(Class data) {
if (!emitter.isDisposed()) {
emitter.onNext(data);
}
}
});
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
dataSubscription.cancel();
}
});
}
});
}
示例2: createListItemEmitter
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
static <T> void createListItemEmitter(final Query<T> query, final FlowableEmitter<T> emitter) {
final DataSubscription dataSubscription = query.subscribe().observer(new DataObserver<List<T>>() {
@Override
public void onData(List<T> data) {
for (T datum : data) {
if (emitter.isCancelled()) {
return;
} else {
emitter.onNext(datum);
}
}
if (!emitter.isCancelled()) {
emitter.onComplete();
}
}
});
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
dataSubscription.cancel();
}
});
}
示例3: observable
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* The returned Observable emits Query results as Lists.
* Never completes, so you will get updates when underlying data changes.
*/
public static <T> Observable<List<T>> observable(final Query<T> query) {
return Observable.create(new ObservableOnSubscribe<List<T>>() {
@Override
public void subscribe(final ObservableEmitter<List<T>> emitter) throws Exception {
final DataSubscription dataSubscription = query.subscribe().observer(new DataObserver<List<T>>() {
@Override
public void onData(List<T> data) {
if (!emitter.isDisposed()) {
emitter.onNext(data);
}
}
});
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
dataSubscription.cancel();
}
});
}
});
}
示例4: single
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* The returned Single emits one Query result as a List.
*/
public static <T> Single<List<T>> single(final Query<T> query) {
return Single.create(new SingleOnSubscribe<List<T>>() {
@Override
public void subscribe(final SingleEmitter<List<T>> emitter) throws Exception {
final DataSubscription dataSubscription = query.subscribe().single().observer(new DataObserver<List<T>>() {
@Override
public void onData(List<T> data) {
if (!emitter.isDisposed()) {
emitter.onSuccess(data);
}
}
});
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
dataSubscription.cancel();
}
});
}
});
}
示例5: subscribe
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Override
public final void subscribe(FlowableEmitter<T> emitter) throws Exception {
final GoogleApiClient apiClient = createApiClient(new ApiClientConnectionCallbacks(emitter));
try {
apiClient.connect();
} catch (Throwable ex) {
emitter.onError(ex);
}
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
if (apiClient.isConnected()) {
RxLocationFlowableOnSubscribe.this.onUnsubscribed(apiClient);
}
apiClient.disconnect();
}
});
}
示例6: subscribe
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Override
public final void subscribe(MaybeEmitter<T> emitter) throws Exception {
final GoogleApiClient apiClient = createApiClient(new ApiClientConnectionCallbacks(emitter));
try {
apiClient.connect();
} catch (Throwable ex) {
emitter.onError(ex);
}
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
if (apiClient.isConnected()) {
RxLocationMaybeOnSubscribe.this.onUnsubscribed(apiClient);
}
apiClient.disconnect();
}
});
}
示例7: subscribe
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Override
public final void subscribe(SingleEmitter<T> emitter) throws Exception {
final GoogleApiClient apiClient = createApiClient(new ApiClientConnectionCallbacks(emitter));
try {
apiClient.connect();
} catch (Throwable ex) {
emitter.onError(ex);
}
emitter.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
if (apiClient.isConnected()) {
RxLocationSingleOnSubscribe.this.onUnsubscribed(apiClient);
}
apiClient.disconnect();
}
});
}
示例8: locationUpdates
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* Yields periodical location updates.
*
* This observable will never call onComplete() thus manual unsubscribe() is necessary.
*
* When using setExpirationDuration() or setNumUpdates() or setExpirationTime() the observable
* will not terminate automatically and will just stop emitting new items without releasing any
* resources.
*
* @return an Observable that returns Location items.
*/
@SuppressWarnings("WeakerAccess") // It's an entry point.
public static Observable<Location> locationUpdates(final Context context,
final LocationRequest locationRequest) {
return Observable.create(new ObservableOnSubscribe<Location>() {
@Override
public void subscribe(ObservableEmitter<Location> e) throws Exception {
final LocationUpdatesHelper locationUpdatesHelper = new LocationUpdatesHelper(
context, new GoogleApiClientFactoryImpl(),
new FusedLocationProviderFactoryImpl(), e, locationRequest);
e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
locationUpdatesHelper.stop();
}
});
locationUpdatesHelper.start();
}
});
}
示例9: lastLocation
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* Yields the last location available to the system.
*
* This observable will emit only one element and then call onComplete.
*
* @return an Observable that returns one Location item.
*/
@SuppressWarnings("WeakerAccess") // It's an entry point.
public static Single<Location> lastLocation(final Context context) {
return Observable.create(new ObservableOnSubscribe<Location>() {
@Override
public void subscribe(ObservableEmitter<Location> e) throws Exception {
final LastLocationHelper lastLocationHelper = new LastLocationHelper(
context, new GoogleApiClientFactoryImpl(),
new FusedLocationProviderFactoryImpl(), e);
e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
lastLocationHelper.stop();
}
});
lastLocationHelper.start();
}
}).singleOrError();
}
示例10: observe
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
public <T> Observable<T> observe(final String key) {
return Observable.create(new ObservableOnSubscribe<T>() {
@Override
public void subscribe(final ObservableEmitter<T> e) throws Exception {
addOnWriteListener(key, new OnWriteListener<T>() {
@Override
public void onWrite(T object) {
if (!e.isDisposed()) {
e.onNext(object);
}
e.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
removeListener(key);
}
});
}
});
}
});
}
示例11: verifyCancellation
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Test public void verifyCancellation() {
final AtomicInteger i = new AtomicInteger();
//noinspection unchecked because Java
Maybe<Integer> source = Maybe.create(new MaybeOnSubscribe<Integer>() {
@Override public void subscribe(MaybeEmitter<Integer> e) {
e.setCancellable(new Cancellable() {
@Override public void cancel() {
i.incrementAndGet();
}
});
}
});
MaybeSubject<Integer> lifecycle = MaybeSubject.create();
source.as(AutoDispose.<Integer>autoDisposable(lifecycle))
.subscribe();
assertThat(i.get()).isEqualTo(0);
assertThat(lifecycle.hasObservers()).isTrue();
lifecycle.onSuccess(0);
// Verify cancellation was called
assertThat(i.get()).isEqualTo(1);
assertThat(lifecycle.hasObservers()).isFalse();
}
示例12: verifyCancellation
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Test public void verifyCancellation() {
final AtomicInteger i = new AtomicInteger();
//noinspection unchecked because Java
Completable source = Completable.create(new CompletableOnSubscribe() {
@Override public void subscribe(CompletableEmitter e) {
e.setCancellable(new Cancellable() {
@Override public void cancel() {
i.incrementAndGet();
}
});
}
});
MaybeSubject<Integer> lifecycle = MaybeSubject.create();
source.as(autoDisposable(lifecycle))
.subscribe();
assertThat(i.get()).isEqualTo(0);
assertThat(lifecycle.hasObservers()).isTrue();
lifecycle.onSuccess(0);
// Verify cancellation was called
assertThat(i.get()).isEqualTo(1);
assertThat(lifecycle.hasObservers()).isFalse();
}
示例13: verifyCancellation
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Test public void verifyCancellation() {
final AtomicInteger i = new AtomicInteger();
//noinspection unchecked because Java
Single<Integer> source = Single.create(new SingleOnSubscribe<Integer>() {
@Override public void subscribe(SingleEmitter<Integer> e) {
e.setCancellable(new Cancellable() {
@Override public void cancel() {
i.incrementAndGet();
}
});
}
});
MaybeSubject<Integer> lifecycle = MaybeSubject.create();
source.as(AutoDispose.<Integer>autoDisposable(lifecycle))
.subscribe();
assertThat(i.get()).isEqualTo(0);
assertThat(lifecycle.hasObservers()).isTrue();
lifecycle.onSuccess(0);
// Verify cancellation was called
assertThat(i.get()).isEqualTo(1);
assertThat(lifecycle.hasObservers()).isFalse();
}
示例14: subscribe
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
@Override
public void subscribe(@NonNull final ObservableEmitter<AppState> appStateEmitter) throws Exception {
final AppStateListener appStateListener = new AppStateListener() {
@Override
public void onAppDidEnterForeground() {
appStateEmitter.onNext(FOREGROUND);
}
@Override
public void onAppDidEnterBackground() {
appStateEmitter.onNext(BACKGROUND);
}
};
appStateEmitter.setCancellable(new Cancellable() {
@Override public void cancel() throws Exception {
recognizer.removeListener(appStateListener);
recognizer.stop();
}
});
recognizer.addListener(appStateListener);
recognizer.start();
}
示例15: single
import io.reactivex.functions.Cancellable; //导入依赖的package包/类
/**
* @param query
* @return
*/
@NonNull
@CheckReturnValue
public static Single<DataSnapshot> single(@NonNull final Query query) {
return Single.create(new SingleOnSubscribe<DataSnapshot>() {
@Override
public void subscribe(
@NonNull final SingleEmitter<DataSnapshot> emit) throws Exception {
final ValueEventListener listener = listener(emit);
emit.setCancellable(new Cancellable() {
@Override
public void cancel() throws Exception {
query.removeEventListener(listener);
}
});
query.addListenerForSingleValueEvent(listener);
}
});
}