Search in sources :

Example 11 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method sub.

public void sub(final TaskCompletionSource<DDPSubscription.Ready> task, String name, JSONArray params, String id) {
    final boolean requested = sendMessage("sub", json -> json.put("id", id).put("name", name).put("params", params));
    if (requested) {
        CompositeDisposable subscriptions = new CompositeDisposable();
        subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).subscribe(response -> {
            String msg = extractMsg(response);
            if ("ready".equals(msg) && !response.isNull("subs")) {
                JSONArray ids = response.optJSONArray("subs");
                for (int i = 0; i < ids.length(); i++) {
                    String _id = ids.optString(i);
                    if (id.equals(_id)) {
                        task.setResult(new DDPSubscription.Ready(client, id));
                        subscriptions.dispose();
                        break;
                    }
                }
            } else if ("nosub".equals(msg) && !response.isNull("id") && !response.isNull("error")) {
                String _id = response.optString("id");
                if (id.equals(_id)) {
                    task.setError(new DDPSubscription.NoSub.Error(client, id, response.optJSONObject("error")));
                    subscriptions.dispose();
                }
            }
        }, err -> {
        }));
        addErrorCallback(subscriptions, task);
    } else {
        task.trySetError(new DDPClientCallback.Closed(client));
    }
}
Also used : RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) TimeoutException(java.util.concurrent.TimeoutException) TextUtils(android.text.TextUtils) NonNull(android.support.annotation.NonNull) RCLog(chat.rocket.android.log.RCLog) TaskCompletionSource(bolts.TaskCompletionSource) TimeUnit(java.util.concurrent.TimeUnit) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) JSONException(org.json.JSONException) RxWebSocket(chat.rocket.android_ddp.rx.RxWebSocket) JSONObject(org.json.JSONObject) OkHttpClient(okhttp3.OkHttpClient) Flowable(io.reactivex.Flowable) Nullable(android.support.annotation.Nullable) Task(bolts.Task) JSONArray(org.json.JSONArray) JSONArray(org.json.JSONArray) RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 12 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project RxJava-Android-Samples by kaushikgopal.

the class RxBusDemo_Bottom1Fragment method onStart.

@Override
public void onStart() {
    super.onStart();
    _disposables = new CompositeDisposable();
    _disposables.add(_rxBus.asFlowable().subscribe(event -> {
        if (event instanceof RxBusDemoFragment.TapEvent) {
            _showTapText();
        }
    }));
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) LayoutInflater(android.view.LayoutInflater) MainActivity(com.morihacky.android.rxjava.MainActivity) ViewGroup(android.view.ViewGroup) BindView(butterknife.BindView) R(com.morihacky.android.rxjava.R) BaseFragment(com.morihacky.android.rxjava.fragments.BaseFragment) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) TextView(android.widget.TextView) View(android.view.View) ViewCompat(android.support.v4.view.ViewCompat) Nullable(android.support.annotation.Nullable) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 13 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project RxJava-Android-Samples by kaushikgopal.

the class RxBusDemo_Bottom2Fragment method onStart.

@Override
public void onStart() {
    super.onStart();
    _disposables = new CompositeDisposable();
    Flowable<Object> tapEventEmitter = _rxBus.asFlowable().share();
    _disposables.add(tapEventEmitter.subscribe(event -> {
        if (event instanceof RxBusDemoFragment.TapEvent) {
            _showTapText();
        }
    }));
    Flowable<Object> debouncedEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);
    Flowable<List<Object>> debouncedBufferEmitter = tapEventEmitter.buffer(debouncedEmitter);
    _disposables.add(debouncedBufferEmitter.observeOn(AndroidSchedulers.mainThread()).subscribe(taps -> {
        _showTapCount(taps.size());
    }));
}
Also used : Bundle(android.os.Bundle) ButterKnife(butterknife.ButterKnife) LayoutInflater(android.view.LayoutInflater) MainActivity(com.morihacky.android.rxjava.MainActivity) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ViewGroup(android.view.ViewGroup) BindView(butterknife.BindView) R(com.morihacky.android.rxjava.R) TimeUnit(java.util.concurrent.TimeUnit) BaseFragment(com.morihacky.android.rxjava.fragments.BaseFragment) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) List(java.util.List) TextView(android.widget.TextView) Flowable(io.reactivex.Flowable) View(android.view.View) ViewCompat(android.support.v4.view.ViewCompat) Nullable(android.support.annotation.Nullable) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 14 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project RxJava-Android-Samples by kaushikgopal.

the class PaginationAutoFragment method onStart.

@Override
public void onStart() {
    super.onStart();
    _disposables = new CompositeDisposable();
    Disposable d2 = _paginator.onBackpressureDrop().doOnNext(i -> {
        _requestUnderWay = true;
        _progressBar.setVisibility(View.VISIBLE);
    }).concatMap(this::_itemsFromNetworkCall).observeOn(AndroidSchedulers.mainThread()).map(items -> {
        _adapter.addItems(items);
        _adapter.notifyDataSetChanged();
        return items;
    }).doOnNext(i -> {
        _requestUnderWay = false;
        _progressBar.setVisibility(View.INVISIBLE);
    }).subscribe();
    // I'm using an RxBus purely to hear from a nested button click
    // we don't really need Rx for this part. it's just easy ¯\_(ツ)_/¯
    Disposable d1 = _bus.asFlowable().filter(o -> !_requestUnderWay).subscribe(event -> {
        if (event instanceof PaginationAutoAdapter.PageEvent) {
            int nextPage = _adapter.getItemCount();
            _paginator.onNext(nextPage);
        }
    });
    _disposables.add(d1);
    _disposables.add(d2);
    _paginator.onNext(0);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) ButterKnife(butterknife.ButterKnife) RxBus(com.morihacky.android.rxjava.rxbus.RxBus) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) MainActivity(com.morihacky.android.rxjava.MainActivity) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ViewGroup(android.view.ViewGroup) BindView(butterknife.BindView) R(com.morihacky.android.rxjava.R) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) BaseFragment(com.morihacky.android.rxjava.fragments.BaseFragment) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) List(java.util.List) Flowable(io.reactivex.Flowable) View(android.view.View) Nullable(android.support.annotation.Nullable) PublishProcessor(io.reactivex.processors.PublishProcessor) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 15 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project RxJava by ReactiveX.

the class CompletableMergeDelayErrorIterable method subscribeActual.

@Override
public void subscribeActual(final CompletableObserver s) {
    final CompositeDisposable set = new CompositeDisposable();
    s.onSubscribe(set);
    Iterator<? extends CompletableSource> iterator;
    try {
        iterator = ObjectHelper.requireNonNull(sources.iterator(), "The source iterator returned is null");
    } catch (Throwable e) {
        Exceptions.throwIfFatal(e);
        s.onError(e);
        return;
    }
    final AtomicInteger wip = new AtomicInteger(1);
    final AtomicThrowable error = new AtomicThrowable();
    for (; ; ) {
        if (set.isDisposed()) {
            return;
        }
        boolean b;
        try {
            b = iterator.hasNext();
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            error.addThrowable(e);
            break;
        }
        if (!b) {
            break;
        }
        if (set.isDisposed()) {
            return;
        }
        CompletableSource c;
        try {
            c = ObjectHelper.requireNonNull(iterator.next(), "The iterator returned a null CompletableSource");
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            error.addThrowable(e);
            break;
        }
        if (set.isDisposed()) {
            return;
        }
        wip.getAndIncrement();
        c.subscribe(new MergeInnerCompletableObserver(s, set, error, wip));
    }
    if (wip.decrementAndGet() == 0) {
        Throwable ex = error.terminate();
        if (ex == null) {
            s.onComplete();
        } else {
            s.onError(ex);
        }
    }
}
Also used : AtomicThrowable(io.reactivex.internal.util.AtomicThrowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicThrowable(io.reactivex.internal.util.AtomicThrowable) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) MergeInnerCompletableObserver(io.reactivex.internal.operators.completable.CompletableMergeDelayErrorArray.MergeInnerCompletableObserver)

Aggregations

CompositeDisposable (io.reactivex.disposables.CompositeDisposable)25 Nullable (android.support.annotation.Nullable)11 TimeUnit (java.util.concurrent.TimeUnit)10 Flowable (io.reactivex.Flowable)9 Test (org.junit.Test)9 NonNull (android.support.annotation.NonNull)6 TextUtils (android.text.TextUtils)6 Task (bolts.Task)6 TaskCompletionSource (bolts.TaskCompletionSource)6 RCLog (chat.rocket.android.log.RCLog)6 RxWebSocket (chat.rocket.android_ddp.rx.RxWebSocket)6 RxWebSocketCallback (chat.rocket.android_ddp.rx.RxWebSocketCallback)6 TimeoutException (java.util.concurrent.TimeoutException)6 OkHttpClient (okhttp3.OkHttpClient)6 JSONArray (org.json.JSONArray)6 Bundle (android.os.Bundle)5 LayoutInflater (android.view.LayoutInflater)5 View (android.view.View)5 ViewGroup (android.view.ViewGroup)5 BindView (butterknife.BindView)5