use of io.reactivex.Flowable in project brave by openzipkin.
the class CurrentTraceContextAssemblyTrackingMatrixTest method connectableFlowable_assembleInScope_subscribeNoScope.
@Test
public void connectableFlowable_assembleInScope_subscribeNoScope() {
ConnectableFlowable<Integer> source, errorSource;
try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
source = Flowable.range(1, 3).doOnNext(e -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
errorSource = Flowable.<Integer>error(new IllegalStateException()).doOnError(t -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
}
subscribeInNoContext(source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()).assertResult(1, 2, 3);
}
use of io.reactivex.Flowable in project brave by openzipkin.
the class CurrentTraceContextAssemblyTrackingMatrixTest method connectableFlowable_assembleInScope_subscribeInScope.
@Test
public void connectableFlowable_assembleInScope_subscribeInScope() {
ConnectableFlowable<Integer> source, errorSource;
try (Scope scope = currentTraceContext.newScope(assemblyContext)) {
source = Flowable.range(1, 3).doOnNext(e -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
errorSource = Flowable.<Integer>error(new IllegalStateException()).doOnError(t -> assertInAssemblyContext()).doOnComplete(this::assertInAssemblyContext).publish();
}
subscribeInDifferentContext(source.autoConnect().toObservable(), errorSource.autoConnect().toObservable()).assertResult(1, 2, 3);
}
use of io.reactivex.Flowable in project BaseProject by wareine.
the class Transformers method transformers.
// public static <T> FlowableTransformer<ResponseBody,ApiResponse<T>> transformers(){
// return new FlowableTransformer<ResponseBody, ApiResponse<T>>() {
// @Override
// public Publisher<ApiResponse<T>> apply(Flowable<ResponseBody> upstream) {
// return upstream.map(new Function<ResponseBody, ApiResponse<T>>() {
// @Override
// public ApiResponse<T> apply(ResponseBody responseBody) throws Exception {
// String response = responseBody.string();
// Gson gson = new Gson();
// Type objectType = new TypeToken<ApiResponse<T>>() {}.getType();
// return gson.fromJson(response, objectType);
// }
// }).onErrorResumeNext(new Function<Throwable, Publisher<? extends ApiResponse<T>>>() {
// @Override
// public Publisher<? extends ApiResponse<T>> apply(Throwable throwable) throws Exception {
// return Flowable.error(ExceptionHandle.handleException(throwable));
// }
// });
// }
// };
// }
public static <T> FlowableTransformer<ResponseBody, T> transformers(final Class<T> cls) {
return new FlowableTransformer<ResponseBody, T>() {
@Override
public Publisher<T> apply(Flowable<ResponseBody> upstream) {
return upstream.map(new Function<ResponseBody, T>() {
@Override
public T apply(ResponseBody responseBody) throws Exception {
String response = responseBody.string();
Gson gson = new Gson();
return gson.fromJson(response, cls);
}
}).onErrorResumeNext(new Function<Throwable, Publisher<? extends T>>() {
@Override
public Publisher<? extends T> apply(Throwable throwable) throws Exception {
return Flowable.error(ExceptionHandle.handleException(throwable));
}
});
}
};
}
use of io.reactivex.Flowable in project NewPipe by TeamNewPipe.
the class SearchFragment method initSuggestionObserver.
private void initSuggestionObserver() {
if (DEBUG)
Log.d(TAG, "initSuggestionObserver() called");
if (suggestionDisposable != null)
suggestionDisposable.dispose();
final Observable<String> observable = suggestionPublisher.debounce(SUGGESTIONS_DEBOUNCE, TimeUnit.MILLISECONDS).startWith(searchQuery != null ? searchQuery : "").filter(query -> isSuggestionsEnabled);
suggestionDisposable = observable.switchMap(query -> {
final Flowable<List<SearchHistoryEntry>> flowable = historyRecordManager.getRelatedSearches(query, 3, 25);
final Observable<List<SuggestionItem>> local = flowable.toObservable().map(searchHistoryEntries -> {
List<SuggestionItem> result = new ArrayList<>();
for (SearchHistoryEntry entry : searchHistoryEntries) result.add(new SuggestionItem(true, entry.getSearch()));
return result;
});
if (query.length() < THRESHOLD_NETWORK_SUGGESTION) {
// Only pass through if the query length is equal or greater than THRESHOLD_NETWORK_SUGGESTION
return local.materialize();
}
final Observable<List<SuggestionItem>> network = ExtractorHelper.suggestionsFor(serviceId, query, contentCountry).toObservable().map(strings -> {
List<SuggestionItem> result = new ArrayList<>();
for (String entry : strings) {
result.add(new SuggestionItem(false, entry));
}
return result;
});
return Observable.zip(local, network, (localResult, networkResult) -> {
List<SuggestionItem> result = new ArrayList<>();
if (localResult.size() > 0)
result.addAll(localResult);
// Remove duplicates
final Iterator<SuggestionItem> iterator = networkResult.iterator();
while (iterator.hasNext() && localResult.size() > 0) {
final SuggestionItem next = iterator.next();
for (SuggestionItem item : localResult) {
if (item.query.equals(next.query)) {
iterator.remove();
break;
}
}
}
if (networkResult.size() > 0)
result.addAll(networkResult);
return result;
}).materialize();
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(listNotification -> {
if (listNotification.isOnNext()) {
handleSuggestions(listNotification.getValue());
} else if (listNotification.isOnError()) {
Throwable error = listNotification.getError();
if (!ExtractorHelper.hasAssignableCauseThrowable(error, IOException.class, SocketException.class, InterruptedException.class, InterruptedIOException.class)) {
onSuggestionError(error);
}
}
});
}
use of io.reactivex.Flowable in project retrofit by square.
the class RxJava2CallAdapterFactoryTest method responseTypes.
@Test
public void responseTypes() {
Type oBodyClass = new TypeToken<Observable<String>>() {
}.getType();
assertThat(factory.get(oBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sBodyClass = new TypeToken<Single<String>>() {
}.getType();
assertThat(factory.get(sBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mBodyClass = new TypeToken<Maybe<String>>() {
}.getType();
assertThat(factory.get(mBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fBodyClass = new TypeToken<Flowable<String>>() {
}.getType();
assertThat(factory.get(fBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type oBodyWildcard = new TypeToken<Observable<? extends String>>() {
}.getType();
assertThat(factory.get(oBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sBodyWildcard = new TypeToken<Single<? extends String>>() {
}.getType();
assertThat(factory.get(sBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mBodyWildcard = new TypeToken<Maybe<? extends String>>() {
}.getType();
assertThat(factory.get(mBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fBodyWildcard = new TypeToken<Flowable<? extends String>>() {
}.getType();
assertThat(factory.get(fBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type oBodyGeneric = new TypeToken<Observable<List<String>>>() {
}.getType();
assertThat(factory.get(oBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
}.getType());
Type sBodyGeneric = new TypeToken<Single<List<String>>>() {
}.getType();
assertThat(factory.get(sBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
}.getType());
Type mBodyGeneric = new TypeToken<Maybe<List<String>>>() {
}.getType();
assertThat(factory.get(mBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
}.getType());
Type fBodyGeneric = new TypeToken<Flowable<List<String>>>() {
}.getType();
assertThat(factory.get(fBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
}.getType());
Type oResponseClass = new TypeToken<Observable<Response<String>>>() {
}.getType();
assertThat(factory.get(oResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sResponseClass = new TypeToken<Single<Response<String>>>() {
}.getType();
assertThat(factory.get(sResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mResponseClass = new TypeToken<Maybe<Response<String>>>() {
}.getType();
assertThat(factory.get(mResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fResponseClass = new TypeToken<Flowable<Response<String>>>() {
}.getType();
assertThat(factory.get(fResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type oResponseWildcard = new TypeToken<Observable<Response<? extends String>>>() {
}.getType();
assertThat(factory.get(oResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sResponseWildcard = new TypeToken<Single<Response<? extends String>>>() {
}.getType();
assertThat(factory.get(sResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mResponseWildcard = new TypeToken<Maybe<Response<? extends String>>>() {
}.getType();
assertThat(factory.get(mResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fResponseWildcard = new TypeToken<Flowable<Response<? extends String>>>() {
}.getType();
assertThat(factory.get(fResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type oResultClass = new TypeToken<Observable<Result<String>>>() {
}.getType();
assertThat(factory.get(oResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sResultClass = new TypeToken<Single<Result<String>>>() {
}.getType();
assertThat(factory.get(sResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mResultClass = new TypeToken<Maybe<Result<String>>>() {
}.getType();
assertThat(factory.get(mResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fResultClass = new TypeToken<Flowable<Result<String>>>() {
}.getType();
assertThat(factory.get(fResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type oResultWildcard = new TypeToken<Observable<Result<? extends String>>>() {
}.getType();
assertThat(factory.get(oResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type sResultWildcard = new TypeToken<Single<Result<? extends String>>>() {
}.getType();
assertThat(factory.get(sResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type mResultWildcard = new TypeToken<Maybe<Result<? extends String>>>() {
}.getType();
assertThat(factory.get(mResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
Type fResultWildcard = new TypeToken<Flowable<Result<? extends String>>>() {
}.getType();
assertThat(factory.get(fResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
}
Aggregations