/** * Creates an {@link ObservableTransformer} that will flatten the provided {@link Action} into the * stream as a {@link Completable} every time it receives an effect from the upstream effects * observable. This will result in calling the provided Action every time an effect is dispatched * to the created effect transformer. * * @param doEffect the {@link Action} to be run every time the effect is requested * @param <F> the type of Effect this transformer handles * @param <E> these transformers are for effects that do not result in any events; however, they * still need to share the same Event type * @return an {@link ObservableTransformer} that can be used with a {@link * SubtypeEffectHandlerBuilder}. */ static <F, E> ObservableTransformer<F, E> fromAction(final Action doEffect) { return fromAction(doEffect, null); }
/** * Add an {@link Action} for handling effects of a given type. The action will be invoked once * for every received effect object that extends the given class. * * <p>Adding handlers for two effect classes where one is a super-class of the other is * considered a collision and is not allowed. Registering the same class twice is also * considered a collision. * * @param effectClass the class to handle * @param action the action that should be invoked for the effect * @param <G> the effect class as a type parameter * @return this builder * @throws IllegalArgumentException if there is a handler collision */ public <G extends F> SubtypeEffectHandlerBuilder<F, E> addAction( final Class<G> effectClass, final Action action) { //noinspection ResultOfMethodCallIgnored checkNotNull(effectClass); //noinspection ResultOfMethodCallIgnored checkNotNull(action); return addTransformer(effectClass, Transformers.<G, E>fromAction(action)); }
/** * Add an {@link Action} for handling effects of a given type. The action will be invoked once * for every received effect object that extends the given class. * * <p>Adding handlers for two effect classes where one is a super-class of the other is * considered a collision and is not allowed. Registering the same class twice is also * considered a collision. * * @param effectClass the class to handle * @param action the action that should be invoked for the effect * @param scheduler the scheduler that should be used to invoke the action * @param <G> the effect class as a type parameter * @return this builder * @throws IllegalArgumentException if there is a handler collision */ public <G extends F> SubtypeEffectHandlerBuilder<F, E> addAction( final Class<G> effectClass, final Action action, Scheduler scheduler) { //noinspection ResultOfMethodCallIgnored checkNotNull(effectClass); //noinspection ResultOfMethodCallIgnored checkNotNull(action); return addTransformer(effectClass, Transformers.<G, E>fromAction(action, scheduler)); }
@Test public void effectPerformerRunsActionWheneverEffectIsRequested() throws Exception { PublishSubject<String> upstream = PublishSubject.create(); TestAction action = new TestAction(); upstream.compose(Transformers.fromAction(action)).subscribe(); upstream.onNext("First Time"); assertThat(action.getRunCount(), is(1)); upstream.onNext("One more!"); assertThat(action.getRunCount(), is(2)); }
@Test public void effectPerformerRunsActionOnSchedulerWheneverEffectIsRequested() throws Exception { PublishSubject<String> upstream = PublishSubject.create(); TestAction action = new TestAction(); TestScheduler scheduler = new TestScheduler(); upstream.compose(Transformers.fromAction(action, scheduler)).subscribe(); upstream.onNext("First Time"); assertThat(action.getRunCount(), is(0)); scheduler.triggerActions(); assertThat(action.getRunCount(), is(1)); }