Search in sources :

Example 26 with Observable

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();
}
Also used : Callable(java.util.concurrent.Callable) Observable(io.reactivex.Observable)

Example 27 with Observable

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;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Type(com.sun.tools.javac.code.Type) List(java.util.List) ArrayList(java.util.ArrayList)

Example 28 with Observable

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;
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ConfigProvider(io.rx_cache2.ConfigProvider) EvictProvider(io.rx_cache2.EvictProvider) Observable(io.reactivex.Observable) ObservableEmitter(io.reactivex.ObservableEmitter) TestObserver(io.reactivex.observers.TestObserver)

Example 29 with Observable

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);
}
Also used : Person(io.requery.test.model.Person) Observable(io.reactivex.Observable) Test(org.junit.Test)

Example 30 with Observable

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);
}
Also used : List(java.util.List) MviBasePresenter(com.hannesdorfmann.mosby3.mvi.MviBasePresenter) Function(io.reactivex.functions.Function) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) ProductBackendApiDecorator(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.http.ProductBackendApiDecorator) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Timber(timber.log.Timber) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Function(io.reactivex.functions.Function) List(java.util.List) ArrayList(java.util.ArrayList) MainMenuItem(com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.MainMenuItem)

Aggregations

Observable (io.reactivex.Observable)70 InOrder (org.mockito.InOrder)27 Test (org.junit.Test)25 TestException (io.reactivex.exceptions.TestException)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Observer (io.reactivex.Observer)10 Function (io.reactivex.functions.Function)10 TestObserver (io.reactivex.observers.TestObserver)10 ConnectableObservable (io.reactivex.observables.ConnectableObservable)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Flowable (io.reactivex.Flowable)4 Maybe (io.reactivex.Maybe)4 Single (io.reactivex.Single)4 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)3 Schedulers (io.reactivex.schedulers.Schedulers)3 Callable (java.util.concurrent.Callable)3 Bundle (android.os.Bundle)2 Handler (android.os.Handler)2