io.reactivex.FlowableTransformer Java Examples

The following examples show how to use io.reactivex.FlowableTransformer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testCancelFromTransition() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(new Transition2<String, Integer, Integer>() {

                @Override
                public String apply(String state, Integer value, Emitter<Integer> emitter) {
                   emitter.cancel_();
                   return state;
                }
            }) //
            .requestBatchSize(10) //
            .build();
    Burst.items(1, 2, 3).create() //
            .compose(sm) //
            .test() //
            .assertNoValues() //
            .assertNotTerminated();
}
 
Example #2
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void noActionTransition() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(new Transition2<String, Integer, Integer>() {
                @Override
                public String apply(String state, Integer value, Emitter<Integer> emitter) {
                    return state;
                }
            }) //
            .build();
    Flowable.just(1, 2) //
            .compose(sm) //
            .test() //
            .assertNoValues() //
            .assertComplete();
}
 
Example #3
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testPassThroughEmitterCompletesTwice() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .completion(new Completion2<String, Integer>() {
                @Override
                public void accept(String state, Emitter<Integer> emitter) {
                    emitter.onComplete_();
                    emitter.onComplete_();
                }
            }) //
            .requestBatchSize(1) //
            .build();
    Flowable.just(1, 2, 3, 4, 5, 6) //
            .compose(sm) //
            .test() //
            .assertValues(1, 2, 3, 4, 5, 6) //
            .assertComplete();
}
 
Example #4
Source File: StatusExceptionResumeNextTransformer.java    From RxGps with Apache License 2.0 6 votes vote down vote up
public static <R extends Result> FlowableTransformer<R, R> forFlowable() {
    return upstream -> upstream.onErrorResumeNext(throwable -> {
        if(throwable instanceof StatusException) {
            StatusException statusException = (StatusException) throwable;

            if(statusException.getStatus().hasResolution()) {
                return Flowable.just((R) statusException.getResult());
            } else {
                return Flowable.error(throwable);
            }

        } else {
            return Flowable.error(throwable);
        }
    });
}
 
Example #5
Source File: RxBusBuilder.java    From RxBus2 with Apache License 2.0 6 votes vote down vote up
public <R> Disposable subscribe(Consumer<R> onNext, Consumer<Throwable> onError, Action onCompleted, FlowableTransformer<T, R> transformer)
{
    Flowable flowable = build(false);
    if (transformer != null)
        flowable = flowable.compose(transformer);

    if (onNext == null)
        onNext = data -> {};
    if (onError == null)
        onError = error -> { throw new OnErrorNotImplementedException(error); };
    if (onCompleted == null)
        onCompleted = () -> {};

    Consumer<R> actualOnNext = onNext;
    if (mQueuer != null && mQueueSubscriptionSafetyCheckEnabled)
        actualOnNext = RxBusUtil.wrapQueueConsumer(onNext, mQueuer);

    flowable = applySchedular(flowable);
    Disposable disposable = flowable.subscribe(actualOnNext, onError, onCompleted);
    if (mBoundObject != null)
        RxDisposableManager.addDisposable(mBoundObject, disposable);
    return disposable;
}
 
Example #6
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public static <T, R extends Number> FlowableTransformer<T, Pair<T, Statistics>> collectStats(
        final Function<? super T, ? extends R> function) {
    return new FlowableTransformer<T, Pair<T, Statistics>>() {

        @Override
        public Flowable<Pair<T, Statistics>> apply(Flowable<T> source) {
            return source.scan(Pair.create((T) null, Statistics.create()),
                    new BiFunction<Pair<T, Statistics>, T, Pair<T, Statistics>>() {
                        @Override
                        public Pair<T, Statistics> apply(Pair<T, Statistics> pair, T t) throws Exception {
                            return Pair.create(t, pair.b().add(function.apply(t)));
                        }
                    }).skip(1);
        }
    };
}
 
Example #7
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnNextThrowsWithBurstSource() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(new Transition2<String, Integer, Integer>() {

                @Override
                public String apply(String state, Integer value, Emitter<Integer> emitter) {
                    throw new ThrowingException();
                }
            }) //
            .requestBatchSize(10) //
            .build();
    Burst.items(1, 2, 3).create() //
            .compose(sm) //
            .test() //
            .assertNoValues() //
            .assertError(ThrowingException.class);
}
 
Example #8
Source File: RxAutoCleanDelegate.java    From BaseProject with MIT License 6 votes vote down vote up
/**
 * 绑定一个 Activity、Fragment 的生命周期,自动释放资源
 * <br/>
 * 例如:网络请求时绑定{@link ActivityEvent#STOP} 或 {@link FragmentEvent#STOP},
 * onStop()时会自动取消网络请求.
 *
 * @param event 事件类型
 * @see ActivityEvent
 * @see FragmentEvent
 */
@SuppressWarnings("SpellCheckingInspection")
@Override
public <Type> FlowableTransformer<Type, Type> bindEventWithFlowable(final int event) {
    final Flowable<Integer> observable = mBehaviorSubject
            .toFlowable(BackpressureStrategy.LATEST)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer integer) throws Exception {
                    return integer == event;
                }
            })
            .take(1);

    return new FlowableTransformer<Type, Type>() {
        @Override
        public Publisher<Type> apply(@io.reactivex.annotations.NonNull Flowable<Type> upstream) {
            return upstream.takeUntil(observable);
        }
    };
}
 
Example #9
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testPassThroughWithCustomCompletion() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .completion(new Completion2<String, Integer>() {
                @Override
                public void accept(String state, Emitter<Integer> emitter) {
                    emitter.onComplete_();
                }
            }) //
            .requestBatchSize(1) //
            .build();
    Flowable.just(1, 2, 3, 4, 5, 6) //
            .compose(sm) //
            .test() //
            .assertValues(1, 2, 3, 4, 5, 6) //
            .assertComplete();
}
 
Example #10
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
public static <T> FlowableTransformer<T, T> rebatchRequests(final int minRequest, final long maxRequest,
        final boolean constrainFirstRequestMin) {
    Preconditions.checkArgument(minRequest <= maxRequest, "minRequest cannot be greater than maxRequest");
    return new FlowableTransformer<T, T>() {

        @Override
        public Publisher<T> apply(Flowable<T> source) {
            if (minRequest == maxRequest && constrainFirstRequestMin) {
                return source.rebatchRequests(minRequest);
            } else {
                return source
                        .compose(Transformers.<T>minRequest(constrainFirstRequestMin ? minRequest : 1, minRequest))
                        .compose(Transformers.<T>maxRequest(maxRequest));
            }
        }
    };
}
 
Example #11
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void errorActionThrows() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .errored(new Errored<String, Integer>() {
                @Override
                public void accept(String state, Throwable error, Emitter<Integer> emitter) {
                    throw new ThrowingException();
                }
            }) //
            .build();
    Flowable.<Integer> error(new RuntimeException()) //
            .compose(sm) //
            .test() //
            .assertNoValues() //
            .assertError(ThrowingException.class);
}
 
Example #12
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompletionThrows() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .completion(new Completion2<String, Integer>() {
                @Override
                public void accept(String state, Emitter<Integer> emitter) {
                    throw new ThrowingException();
                }
            }) //
            .requestBatchSize(1) //
            .build();
    Flowable.just(1) //
            .compose(sm) //
            .test() //
            .assertValues(1) //
            .assertError(ThrowingException.class);
}
 
Example #13
Source File: RxAutoCleanDelegate.java    From BaseProject with MIT License 6 votes vote down vote up
/**
 * 绑定一个 Activity、Fragment 的生命周期,自动释放资源
 * <br/>
 * 例如:网络请求时绑定{@link ActivityEvent#STOP} 或 {@link FragmentEvent#STOP},
 * onStop()时会自动取消网络请求.
 *
 * @param event 事件类型
 * @see ActivityEvent
 * @see FragmentEvent
 */
@SuppressWarnings("SpellCheckingInspection")
@Override
public <Type> FlowableTransformer<Type, Type> bindEventWithFlowable(final int event) {
    final Flowable<Integer> observable = mBehaviorSubject
            .toFlowable(BackpressureStrategy.LATEST)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer integer) throws Exception {
                    return integer == event;
                }
            })
            .take(1);

    return new FlowableTransformer<Type, Type>() {
        @Override
        public Publisher<Type> apply(@io.reactivex.annotations.NonNull Flowable<Type> upstream) {
            return upstream.takeUntil(observable);
        }
    };
}
 
Example #14
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void errorActionPassThrough() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .errored(new Errored<String, Integer>() {
                @Override
                public void accept(String state, Throwable error, Emitter<Integer> emitter) {
                    emitter.onError_(error);
                }
            }) //
            .build();
    Flowable.<Integer> error(new ThrowingException()) //
            .compose(sm) //
            .test() //
            .assertNoValues() //
            .assertError(ThrowingException.class);
}
 
Example #15
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testPassThroughEmitterOnNextAfterCompletion() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .completion(new Completion2<String, Integer>() {
                @Override
                public void accept(String state, Emitter<Integer> emitter) {
                    emitter.onComplete_();
                    emitter.onNext_(8);
                }
            }) //
            .requestBatchSize(1) //
            .build();
    Flowable.just(1, 2, 3, 4, 5, 6) //
            .compose(sm) //
            .test() //
            .assertValues(1, 2, 3, 4, 5, 6) //
            .assertComplete();
}
 
Example #16
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnNextThrowsWithBurstSourceThatTerminatesWithError() {
    List<Throwable> list = new CopyOnWriteArrayList<Throwable>();
    try {
        RxJavaPlugins.setErrorHandler(Consumers.addTo(list));
        FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
                .initialState("") //
                .transition(new Transition2<String, Integer, Integer>() {

                    @Override
                    public String apply(String state, Integer value, Emitter<Integer> emitter) {
                        throw new ThrowingException();
                    }
                }) //
                .requestBatchSize(10) //
                .build();
        Burst.item(1).error(new RuntimeException()) //
                .compose(sm) //
                .test() //
                .assertNoValues() //
                .assertError(ThrowingException.class);
        assertEquals(1, list.size());
    } finally {
        RxJavaPlugins.reset();
    }
}
 
Example #17
Source File: ISHNewsModel.java    From AcgClub with MIT License 6 votes vote down vote up
@Override
public Flowable<SHPage> getAcgNews(int pageIndex) {
  return mRepositoryManager.obtainRetrofitService(AcgNewsService.class)
      .getISHNews(pageIndex, 15, 3)
      .compose(new FlowableTransformer<SHResponse<SHPage>, SHPage>() {
        @Override
        public Flowable<SHPage> apply(Flowable<SHResponse<SHPage>> httpResponseFlowable) {
          return httpResponseFlowable
              .flatMap(new Function<SHResponse<SHPage>, Flowable<SHPage>>() {
                @Override
                public Flowable<SHPage> apply(SHResponse<SHPage> response) {
                  LogUtil.d(response.toString());
                  if (!TextUtils.isEmpty(response.getErrMsg())) {
                    return Flowable.error(new ApiException(response.getErrMsg()));
                  } else if (response.getData() != null) {
                    return RxUtil.createData(response.getData());
                  } else {
                    return Flowable.error(new ApiException("数据加载失败"));
                  }
                }
              });
        }
      });
}
 
Example #18
Source File: Strings.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <T> FlowableTransformer<T, String> strings() {
    return new FlowableTransformer<T, String>() {

        @Override
        public Publisher<String> apply(Flowable<T> source) {
            return Strings.strings(source);
        }

    };
}
 
Example #19
Source File: XApi.java    From XDroid-Databinding with MIT License 5 votes vote down vote up
/**
 * 统一线程切换
 *
 * @return
 */
public static <T> FlowableTransformer<T, T> getFlowableScheduler() {
    return new FlowableTransformer<T, T>() {
        @Override
        public Publisher<T> apply(Flowable<T> upstream) {
            return upstream
                    .onErrorResumeNext(new ServerResultErrorFunc2<T>())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
Example #20
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <T> FlowableTransformer<T, T> windowMin(final int windowSize,
        final Comparator<? super T> comparator) {
    return new FlowableTransformer<T, T>() {
        @Override
        public Flowable<T> apply(Flowable<T> source) {
            return new FlowableWindowMinMax<T>(source, windowSize, comparator, Metric.MIN);
        }
    };
}
 
Example #21
Source File: FlowableUseCase.java    From EasyMVP with Apache License 2.0 5 votes vote down vote up
public FlowableUseCase(final UseCaseExecutor useCaseExecutor,
                       final PostExecutionThread postExecutionThread) {
    super(useCaseExecutor, postExecutionThread);
    schedulersTransformer = new FlowableTransformer<R, R>() {
        @Override
        public Flowable<R> apply(Flowable<R> rObservable) {
            return rObservable.subscribeOn(useCaseExecutor.getScheduler())
                    .observeOn(postExecutionThread.getScheduler());
        }
    };
}
 
Example #22
Source File: RxSchedulers.java    From RxJava2-Android-Samples with Apache License 2.0 5 votes vote down vote up
public <T> FlowableTransformer<T, T> applyFlowableMainThread() {
    return new FlowableTransformer<T, T>() {
        @Override
        public Publisher<T> apply(Flowable<T> flowable) {
            return flowable.observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
Example #23
Source File: XApi.java    From XDroid-Databinding with MIT License 5 votes vote down vote up
public static <T> FlowableTransformer<T, T> getFlowableScheduler(final Function<? super Flowable<Throwable>, ? extends Publisher<?>> retryWhenHandler) {
    return new FlowableTransformer<T, T>() {
        @Override
        public Publisher<T> apply(Flowable<T> upstream) {
            return upstream
                    .retryWhen(retryWhenHandler)
                    .onErrorResumeNext(new ServerResultErrorFunc2<T>())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
Example #24
Source File: XApi.java    From XDroid-Databinding with MIT License 5 votes vote down vote up
public static <T> FlowableTransformer<T, T> getFlowableScheduler(final Function<? super Flowable<Throwable>, ? extends Publisher<?>> retryWhenHandler, final Function<? super Throwable, ? extends Publisher<? extends T>> resumeFunction) {
    return new FlowableTransformer<T, T>() {
        @Override
        public Publisher<T> apply(Flowable<T> upstream) {
            return upstream
                    .retryWhen(retryWhenHandler)
                    .onErrorResumeNext(resumeFunction)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
Example #25
Source File: RxUtils.java    From ViewPagerHelper with Apache License 2.0 5 votes vote down vote up
public static <T>FlowableTransformer<T,T> flScheduers(){
    return new FlowableTransformer<T, T>() {
        @Override
        public Publisher<T> apply(Flowable<T> upstream) {
            return upstream.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };
}
 
Example #26
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <A, B, C, K> FlowableTransformer<A, C> matchWith(final Flowable<B> b,
        final Function<? super A, K> aKey, final Function<? super B, K> bKey,
        final BiFunction<? super A, ? super B, C> combiner, int requestSize) {
    return new FlowableTransformer<A, C>() {

        @Override
        public Publisher<C> apply(Flowable<A> upstream) {
            return Flowables.match(upstream, b, aKey, bKey, combiner);
        }
    };
}
 
Example #27
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <T> FlowableTransformer<T, T> mapLast(final Function<? super T, ? extends T> function) {
    return new FlowableTransformer<T, T>() {

        @Override
        public Publisher<T> apply(Flowable<T> upstream) {
            return new FlowableMapLast<T>(upstream, function);
        }

    };

}
 
Example #28
Source File: FlowableStateMachineTest.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testInvalidRequestBatchSize() {
    FlowableTransformer<Integer, Integer> sm = StateMachine2.builder() //
            .initialState("") //
            .transition(PASS_THROUGH_TRANSITION) //
            .requestBatchSize(-10) //
            .build();
    Flowable.just(1).compose(sm).test();
}
 
Example #29
Source File: Transformers.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <State, In, Out> FlowableTransformer<In, Out> stateMachine(Callable<? extends State> initialState,
        Function3<? super State, ? super In, ? super FlowableEmitter<Out>, ? extends State> transition,
        BiPredicate<? super State, ? super FlowableEmitter<Out>> completion,
        BackpressureStrategy backpressureStrategy, int requestBatchSize) {
    return TransformerStateMachine.create(initialState, transition, completion, backpressureStrategy,
            requestBatchSize);
}
 
Example #30
Source File: TransformerStateMachine.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public static <State, In, Out> FlowableTransformer<In, Out> create(Callable<? extends State> initialState,
        Function3<? super State, ? super In, ? super FlowableEmitter<Out>, ? extends State> transition,
        BiPredicate<? super State, ? super FlowableEmitter<Out>> completion,
        BackpressureStrategy backpressureStrategy, int requestBatchSize) {
    return new TransformerStateMachine<State, In, Out>(initialState, transition, completion, backpressureStrategy,
            requestBatchSize);
}