use of io.reactivex.disposables.CompositeDisposable in project RxJava by ReactiveX.
the class TrampolineSchedulerTest method testNestedTrampolineWithUnsubscribe.
@Test
public void testNestedTrampolineWithUnsubscribe() {
final ArrayList<String> workDone = new ArrayList<String>();
final CompositeDisposable workers = new CompositeDisposable();
Worker worker = Schedulers.trampoline().createWorker();
try {
workers.add(worker);
worker.schedule(new Runnable() {
@Override
public void run() {
workers.add(doWorkOnNewTrampoline("A", workDone));
}
});
final Worker worker2 = Schedulers.trampoline().createWorker();
workers.add(worker2);
worker2.schedule(new Runnable() {
@Override
public void run() {
workers.add(doWorkOnNewTrampoline("B", workDone));
// we unsubscribe worker2 ... it should not affect work scheduled on a separate Trampline.Worker
worker2.dispose();
}
});
assertEquals(6, workDone.size());
assertEquals(Arrays.asList("A.1", "A.B.1", "A.B.2", "B.1", "B.B.1", "B.B.2"), workDone);
} finally {
workers.dispose();
}
}
use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.
the class DDPClientImpl method unsub.
public void unsub(final TaskCompletionSource<DDPSubscription.NoSub> task, @Nullable final String id) {
final boolean requested = sendMessage("unsub", json -> json.put("id", id));
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 ("nosub".equals(msg) && response.isNull("error") && !response.isNull("id")) {
String _id = response.optString("id");
if (id.equals(_id)) {
task.setResult(new DDPSubscription.NoSub(client, id));
subscriptions.dispose();
}
}
}, err -> {
}));
addErrorCallback(subscriptions, task);
} else {
task.trySetError(new DDPClientCallback.Closed(client));
}
}
use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.
the class DDPClientImpl method connect.
public void connect(final TaskCompletionSource<DDPClientCallback.Connect> task, final String url, String session) {
try {
flowable = websocket.connect(url).autoConnect();
CompositeDisposable subscriptions = new CompositeDisposable();
subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Open).subscribe(callback -> {
sendMessage("connect", json -> (TextUtils.isEmpty(session) ? json : json.put("session", session)).put("version", "pre2").put("support", new JSONArray().put("pre2").put("pre1")), task);
}, err -> {
}));
subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).timeout(7, TimeUnit.SECONDS).subscribe(response -> {
String msg = extractMsg(response);
if ("connected".equals(msg) && !response.isNull("session")) {
task.trySetResult(new DDPClientCallback.Connect(client, response.optString("session")));
subscriptions.dispose();
} else if ("error".equals(msg) && "Already connected".equals(response.optString("reason"))) {
task.trySetResult(new DDPClientCallback.Connect(client, null));
subscriptions.dispose();
} else if ("failed".equals(msg)) {
task.trySetError(new DDPClientCallback.Connect.Failed(client, response.optString("version")));
subscriptions.dispose();
}
}, err -> task.trySetError(new DDPClientCallback.Connect.Timeout(client))));
addErrorCallback(subscriptions, task);
subscribeBaseListeners();
} catch (Exception e) {
RCLog.e(e);
}
}
use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.
the class DDPClientImpl method rpc.
public void rpc(final TaskCompletionSource<DDPClientCallback.RPC> task, String method, JSONArray params, String id, long timeoutMs) {
final boolean requested = sendMessage("method", json -> json.put("method", method).put("params", params).put("id", id));
if (requested) {
CompositeDisposable subscriptions = new CompositeDisposable();
subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).timeout(timeoutMs, TimeUnit.MILLISECONDS).subscribe(response -> {
String msg = extractMsg(response);
if ("result".equals(msg)) {
String _id = response.optString("id");
if (id.equals(_id)) {
if (!response.isNull("error")) {
task.setError(new DDPClientCallback.RPC.Error(client, id, response.optJSONObject("error")));
} else {
String result = response.optString("result");
task.setResult(new DDPClientCallback.RPC(client, id, result));
}
subscriptions.dispose();
}
}
}, err -> {
if (err instanceof TimeoutException) {
task.setError(new DDPClientCallback.RPC.Timeout(client));
}
}));
addErrorCallback(subscriptions, task);
} else {
task.trySetError(new DDPClientCallback.Closed(client));
}
}
use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.
the class DDPClientImpl method subscribeBaseListeners.
private void subscribeBaseListeners() {
if (subscriptions != null && subscriptions.size() > 0 && !subscriptions.isDisposed()) {
return;
}
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 ("ping".equals(msg)) {
if (response.isNull("id")) {
sendMessage("pong", null);
} else {
sendMessage("pong", json -> json.put("id", response.getString("id")));
}
}
}, err -> {
}));
}
Aggregations