Search in sources :

Example 1 with BehaviorSubject

use of rx.subjects.BehaviorSubject in project android-client by GenesisVision.

the class RateManager method getRate.

public BehaviorSubject<Double> getRate(String from, String to) {
    BehaviorSubject<Double> rateSubject = getRateSubject(from, to);
    RequestRate model = new RequestRate();
    model.setFrom(RequestRate.FromEnum.fromValue(from));
    model.setTo(RequestRate.ToEnum.fromValue(to));
    rateSubscription = rateApi.apiRatePost(model).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).subscribe(response -> handleGetRateResponse(response, rateSubject), this::handleGetRateError);
    return rateSubject;
}
Also used : Pair(android.util.Pair) RateViewModel(io.swagger.client.model.RateViewModel) Schedulers(rx.schedulers.Schedulers) BehaviorSubject(rx.subjects.BehaviorSubject) HashMap(java.util.HashMap) RateApi(io.swagger.client.api.RateApi) Subscription(rx.Subscription) RequestRate(io.swagger.client.model.RequestRate) RequestRate(io.swagger.client.model.RequestRate)

Example 2 with BehaviorSubject

use of rx.subjects.BehaviorSubject in project spoofax by metaborg.

the class ParseResultProcessor method request.

@Override
public Observable<P> request(final I input) {
    final FileObject resource = input.source();
    return Observable.create(new OnSubscribe<P>() {

        @Override
        public void call(Subscriber<? super P> observer) {
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from parse result request for {}", input);
                return;
            }
            final BehaviorSubject<ParseChange<P>> updates = getUpdates(input);
            final ParseChange<P> update = updates.toBlocking().first(new Func1<ParseChange<P>, Boolean>() {

                @Override
                public Boolean call(ParseChange<P> updateToFilter) {
                    final UpdateKind kind = updateToFilter.kind;
                    return kind != UpdateKind.Invalidate;
                }
            });
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from parse result request for {}", resource);
                return;
            }
            switch(update.kind) {
                case Update:
                    logger.trace("Returning cached parse result for {}", resource);
                    observer.onNext(update.unit);
                    observer.onCompleted();
                    break;
                case Error:
                    logger.trace("Returning parse error for {}", resource);
                    observer.onError(update.exception);
                    break;
                case Remove:
                    {
                        final String message = logger.format("Parse result for {} was removed unexpectedly", resource);
                        logger.error(message);
                        observer.onError(new ParseException(input, message));
                        break;
                    }
                default:
                    {
                        final String message = logger.format("Unexpected parse update kind {} for {}", update.kind, resource);
                        logger.error(message);
                        observer.onError(new ParseException(input, message));
                        break;
                    }
            }
        }
    });
}
Also used : BehaviorSubject(rx.subjects.BehaviorSubject) UpdateKind(org.metaborg.core.build.UpdateKind) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) Func1(rx.functions.Func1)

Example 3 with BehaviorSubject

use of rx.subjects.BehaviorSubject in project spoofax by metaborg.

the class AnalysisResultProcessor method request.

@Override
public Observable<A> request(final I input, final IContext context) {
    if (input.detached()) {
        throw new MetaborgRuntimeException("Cannot request updates for detached (no source) units");
    }
    final FileObject resource = input.source();
    return Observable.create(new OnSubscribe<A>() {

        @Override
        public void call(Subscriber<? super A> observer) {
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from analysis result request for {}", resource);
                return;
            }
            final BehaviorSubject<AnalysisChange<A>> updates = getUpdates(input, context);
            final AnalysisChange<A> update = updates.toBlocking().first(new Func1<AnalysisChange<A>, Boolean>() {

                @Override
                public Boolean call(AnalysisChange<A> updateToFilter) {
                    final UpdateKind kind = updateToFilter.kind;
                    return kind != UpdateKind.Invalidate;
                }
            });
            if (observer.isUnsubscribed()) {
                logger.trace("Unsubscribed from analysis result request for {}", resource);
                return;
            }
            switch(update.kind) {
                case Update:
                    logger.trace("Returning cached analysis result for {}", resource);
                    observer.onNext(update.result);
                    observer.onCompleted();
                    break;
                case Error:
                    logger.trace("Returning analysis error for {}", resource);
                    observer.onError(update.exception);
                    break;
                case Remove:
                    {
                        final String message = String.format("Analysis result for %s was removed unexpectedly", resource);
                        logger.error(message);
                        observer.onError(new AnalysisException(context, message));
                        break;
                    }
                default:
                    {
                        final String message = String.format("Unexpected analysis update kind %s for %s", update.kind, resource);
                        logger.error(message);
                        observer.onError(new MetaborgRuntimeException(message));
                        break;
                    }
            }
        }
    });
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) BehaviorSubject(rx.subjects.BehaviorSubject) UpdateKind(org.metaborg.core.build.UpdateKind) AnalysisException(org.metaborg.core.analysis.AnalysisException) FileObject(org.apache.commons.vfs2.FileObject) Func1(rx.functions.Func1)

Aggregations

BehaviorSubject (rx.subjects.BehaviorSubject)3 FileObject (org.apache.commons.vfs2.FileObject)2 UpdateKind (org.metaborg.core.build.UpdateKind)2 Func1 (rx.functions.Func1)2 Pair (android.util.Pair)1 RateApi (io.swagger.client.api.RateApi)1 RateViewModel (io.swagger.client.model.RateViewModel)1 RequestRate (io.swagger.client.model.RequestRate)1 HashMap (java.util.HashMap)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 AnalysisException (org.metaborg.core.analysis.AnalysisException)1 ParseException (org.metaborg.core.syntax.ParseException)1 Subscription (rx.Subscription)1 Schedulers (rx.schedulers.Schedulers)1