use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorTimeoutFirst.
@Test
public void testTimeoutSelectorTimeoutFirst() throws InterruptedException {
Observable<Integer> source = Observable.<Integer>never();
final PublishSubject<Integer> timeout = PublishSubject.create();
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return timeout;
}
};
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.timeout(timeout, timeoutFunc, other).subscribe(o);
timeout.onNext(1);
inOrder.verify(o).onNext(100);
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
use of io.reactivex.functions.Function in project RxJava by ReactiveX.
the class ReplayProcessorTest method testUnsubscriptionCase.
@Test(timeout = 1000)
public void testUnsubscriptionCase() {
ReplayProcessor<String> src = ReplayProcessor.create();
for (int i = 0; i < 10; i++) {
final Subscriber<Object> o = TestHelper.mockSubscriber();
InOrder inOrder = inOrder(o);
String v = "" + i;
src.onNext(v);
System.out.printf("Turn: %d%n", i);
src.firstElement().toFlowable().flatMap(new Function<String, Flowable<String>>() {
@Override
public Flowable<String> apply(String t1) {
return Flowable.just(t1 + ", " + t1);
}
}).subscribe(new DefaultSubscriber<String>() {
@Override
public void onNext(String t) {
System.out.println(t);
o.onNext(t);
}
@Override
public void onError(Throwable e) {
o.onError(e);
}
@Override
public void onComplete() {
o.onComplete();
}
});
inOrder.verify(o).onNext("0, 0");
inOrder.verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
}
use of io.reactivex.functions.Function in project RxCache by VictorAlbertos.
the class ProcessorProvidersBehaviour method getDataFromLoader.
private Observable<Reply> getDataFromLoader(final io.rx_cache2.ConfigProvider configProvider, final Record record) {
return configProvider.getLoaderObservable().map(new Function<Object, Reply>() {
@Override
public Reply apply(Object data) throws Exception {
boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
if (data == null && useExpiredData && record != null) {
return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
}
clearKeyIfNeeded(configProvider);
if (data == null) {
throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey());
}
twoLayersCache.save(configProvider.getProviderKey(), configProvider.getDynamicKey(), configProvider.getDynamicKeyGroup(), data, configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
return new Reply(data, Source.CLOUD, configProvider.isEncrypted());
}
}).onErrorReturn(new Function<Object, Object>() {
@Override
public Object apply(Object o) throws Exception {
clearKeyIfNeeded(configProvider);
boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
if (useExpiredData && record != null) {
return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
}
throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey(), (Throwable) o);
}
});
}
use of io.reactivex.functions.Function in project requery by requery.
the class ReactiveTest method testQueryObservableFromEntity.
@Test
public void testQueryObservableFromEntity() throws Exception {
final Person person = randomPerson();
data.insert(person).map(new Function<Person, Phone>() {
@Override
public Phone apply(Person person) {
Phone phone1 = randomPhone();
phone1.setOwner(person);
return phone1;
}
}).flatMap(new Function<Phone, Single<?>>() {
@Override
public Single<?> apply(Phone phone) {
return data.insert(phone);
}
}).blockingGet();
int count = person.getPhoneNumbers().toList().size();
assertEquals(1, count);
}
use of io.reactivex.functions.Function in project mosby by sockeqwe.
the class MainMenuPresenter method bindIntents.
@Override
protected void bindIntents() {
Observable<List<String>> loadCategories = intent(MainMenuView::loadCategoriesIntent).doOnNext(categoryName -> Timber.d("intent: load category %s", categoryName)).flatMap(ignored -> backendApi.getAllCategories().subscribeOn(Schedulers.io()));
Observable<String> selectCategory = intent(MainMenuView::selectCategoryIntent).doOnNext(categoryName -> Timber.d("intent: select category %s", categoryName)).startWith(MainMenuItem.HOME);
List<Observable<?>> allIntents = new ArrayList<>(2);
allIntents.add(loadCategories);
allIntents.add(selectCategory);
Observable<MenuViewState> menuViewStateObservable = Observable.combineLatest(allIntents, (Function<Object[], MenuViewState>) objects -> {
List<String> categories = (List<String>) objects[0];
String selectedCategory = (String) objects[1];
List<MainMenuItem> categoriesItems = new ArrayList<MainMenuItem>(categories.size() + 1);
categoriesItems.add(new MainMenuItem(MainMenuItem.HOME, selectedCategory.equals(MainMenuItem.HOME)));
for (int i = 0; i < categories.size(); i++) {
String category = categories.get(i);
categoriesItems.add(new MainMenuItem(category, category.equals(selectedCategory)));
}
return new MenuViewState.DataState(categoriesItems);
}).startWith(new MenuViewState.LoadingState()).onErrorReturn(MenuViewState.ErrorState::new).observeOn(AndroidSchedulers.mainThread());
subscribeViewState(menuViewStateObservable, MainMenuView::render);
}
Aggregations