当前位置: 首页>>代码示例>>Java>>正文


Java Cancellable类代码示例

本文整理汇总了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();
                }
            });
        }
    });
}
 
开发者ID:greenrobot,项目名称:ObjectBoxRxJava,代码行数:26,代码来源:RxBoxStore.java

示例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();
        }
    });
}
 
开发者ID:greenrobot,项目名称:ObjectBoxRxJava,代码行数:24,代码来源:RxQuery.java

示例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();
                }
            });
        }
    });
}
 
开发者ID:greenrobot,项目名称:ObjectBoxRxJava,代码行数:26,代码来源:RxQuery.java

示例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();
                }
            });
        }
    });
}
 
开发者ID:greenrobot,项目名称:ObjectBoxRxJava,代码行数:25,代码来源:RxQuery.java

示例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();
        }
    });
}
 
开发者ID:TechIsFun,项目名称:RxJava2-weather-example,代码行数:22,代码来源:RxLocationFlowableOnSubscribe.java

示例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();
        }
    });
}
 
开发者ID:TechIsFun,项目名称:RxJava2-weather-example,代码行数:22,代码来源:RxLocationMaybeOnSubscribe.java

示例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();
        }
    });
}
 
开发者ID:TechIsFun,项目名称:RxJava2-weather-example,代码行数:22,代码来源:RxLocationSingleOnSubscribe.java

示例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();
        }
    });
}
 
开发者ID:julioromano,项目名称:RxLocation,代码行数:34,代码来源:RxLocation.java

示例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();
}
 
开发者ID:julioromano,项目名称:RxLocation,代码行数:29,代码来源:RxLocation.java

示例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);
                        }
                    });
                }
            });
        }
    });
}
 
开发者ID:FabianTerhorst,项目名称:Floppy,代码行数:22,代码来源:RxArrayDisk.java

示例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();
}
 
开发者ID:uber,项目名称:AutoDispose,代码行数:26,代码来源:AutoDisposeMaybeObserverTest.java

示例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();
}
 
开发者ID:uber,项目名称:AutoDispose,代码行数:26,代码来源:AutoDisposeCompletableObserverTest.java

示例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();
}
 
开发者ID:uber,项目名称:AutoDispose,代码行数:26,代码来源:AutoDisposeSingleObserverTest.java

示例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();
}
 
开发者ID:jenzz,项目名称:RxAppState,代码行数:25,代码来源:AppStateObservableOnSubscribe.java

示例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);
        }
    });
}
 
开发者ID:yongjhih,项目名称:rxfirebase,代码行数:25,代码来源:RxValue.java


注:本文中的io.reactivex.functions.Cancellable类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。