use of io.reactivex.Observable in project RxCache by VictorAlbertos.
the class ProxyProviders method invoke.
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
return Observable.defer(new Callable<ObservableSource<?>>() {
@Override
public ObservableSource<?> call() throws Exception {
Observable observable = processorProviders.process(proxyTranslator.processMethod(method, args));
Class<?> methodType = method.getReturnType();
if (methodType == Observable.class)
return Observable.just(observable);
if (methodType == Single.class)
return Observable.just(Single.fromObservable(observable));
if (methodType == Maybe.class) {
return Observable.just(Maybe.fromSingle(Single.fromObservable(observable)));
}
if (method.getReturnType() == io.reactivex.Flowable.class) {
return Observable.just(observable.toFlowable(BackpressureStrategy.MISSING));
}
String errorMessage = method.getName() + io.rx_cache2.internal.Locale.INVALID_RETURN_TYPE;
throw new RuntimeException(errorMessage);
}
}).blockingFirst();
}
use of io.reactivex.Observable in project RxCache by VictorAlbertos.
the class GetProvidersClass method getMethods.
private List<ProvidersClass.Method> getMethods(Element classElement) throws ValidationException {
List<? extends Element> enclosedElements = classElement.getEnclosedElements();
List<ProvidersClass.Method> methods = new ArrayList<>();
for (Element methodElement : enclosedElements) {
if (!isAnnotatedWithActionable(methodElement))
continue;
if (methodElement.getKind() != ElementKind.METHOD)
continue;
Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) methodElement;
String nameMethod = methodSymbol.getSimpleName().toString();
Type returnType = methodSymbol.getReturnType();
if (!returnType.tsym.toString().equals(TypeName.get(Observable.class).toString())) {
throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
}
Type enclosingTypeObservable = returnType.getTypeArguments().get(0);
if (!enclosingTypeObservable.tsym.toString().equals(TypeName.get(List.class).toString())) {
throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
}
List<Symbol.VarSymbol> params = methodSymbol.getParameters();
boolean hasEvictProvider = hasEvictProvider(params);
boolean hasEvictDynamicKey = hasEvictDynamicKey(params);
boolean hasEvictDynamicKeyGroup = hasEvictDynamicKeyGroup(params);
if (!hasEvictProvider && !hasEvictDynamicKey && !hasEvictDynamicKeyGroup) {
throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
}
if (hasEvictProvider && hasEvictDynamicKey) {
throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
}
if (hasEvictProvider && hasEvictDynamicKeyGroup) {
throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
}
if (hasEvictDynamicKey && hasEvictDynamicKeyGroup) {
throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
}
boolean hasDynamicKey = hasDynamicKey(params);
boolean hasDynamicKeyGroup = hasDynamicKeyGroup(params);
methods.add(new ProvidersClass.Method(nameMethod, methodElement, enclosingTypeObservable, hasDynamicKey, hasDynamicKeyGroup));
}
return methods;
}
use of io.reactivex.Observable in project RxCache by VictorAlbertos.
the class ProcessorProvidersTest method getSubscriberCompleted.
private TestObserver getSubscriberCompleted(boolean hasCache, final boolean evictCache, boolean detailResponse, Loader loader, boolean useExpiredDataIfLoaderNotAvailable) {
Observable observable;
switch(loader) {
case VALID:
observable = Observable.just(new Mock("message"));
break;
case NULL:
observable = Observable.error(new RuntimeException("No data"));
break;
default:
observable = Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(ObservableEmitter<Object> e) throws Exception {
throw new RuntimeException("error");
}
});
break;
}
ConfigProvider configProvider = new ConfigProvider("mockKey", null, null, detailResponse, true, false, "", "", observable, new EvictProvider(evictCache));
if (hasCache)
twoLayersCacheMock.save("mockKey", "", "", new Mock("message"), configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
processorProvidersUT = new ProcessorProvidersBehaviour(twoLayersCacheMock, useExpiredDataIfLoaderNotAvailable, evictExpiredRecordsPersistence, getDeepCopy, doMigrations);
TestObserver observerMock = processorProvidersUT.getData(configProvider).test();
observerMock.awaitTerminalEvent();
return observerMock;
}
use of io.reactivex.Observable in project requery by requery.
the class ReactiveTest method testInsertCount.
@Test
public void testInsertCount() throws Exception {
Person person = randomPerson();
Observable.just(person).concatMap(new Function<Person, Observable<Person>>() {
@Override
public Observable<Person> apply(Person person) {
return data.insert(person).toObservable();
}
});
Person p = data.insert(person).blockingGet();
assertTrue(p.getId() > 0);
int count = data.count(Person.class).get().single().blockingGet();
assertEquals(1, count);
}
use of io.reactivex.Observable 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