
com.spotify.mobius.rx3.MobiusEffectRouter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mobius-rx3 Show documentation
Show all versions of mobius-rx3 Show documentation
RxJava3 utilities for use with Mobius
/*
* -\-\-
* Mobius
* --
* Copyright (c) 2017-2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.mobius.rx3;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.functions.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Transformer that routes each incoming Effect descriptor to a sub-transformer associated with the
* Effect descriptor class.
*/
class MobiusEffectRouter implements ObservableTransformer {
@NonNull private final MergedTransformer mergedTransformer;
MobiusEffectRouter(
@NonNull Set> handledEffectClasses,
@NonNull Collection> effectPerformers) {
final Set> effectClasses = new HashSet<>(handledEffectClasses);
final List> immutableEffectPerformers =
Collections.unmodifiableList(new ArrayList<>(effectPerformers));
final ObservableTransformer unhandledEffectHandler =
new ObservableTransformer() {
@Override
public @NonNull ObservableSource apply(@NonNull Observable effects) {
return effects
.filter(
new Predicate() {
@Override
public boolean test(F e) throws Throwable {
for (Class> effectClass : effectClasses) {
if (effectClass.isAssignableFrom(e.getClass())) {
return false;
}
}
return true;
}
})
.map(
new Function() {
@Override
public E apply(F e) throws Throwable {
throw new UnknownEffectException(e);
}
});
}
};
final List> allHandlers =
new ArrayList<>(immutableEffectPerformers);
allHandlers.add(unhandledEffectHandler);
mergedTransformer = new MergedTransformer<>(allHandlers);
}
@Override
public Observable apply(@NonNull Observable effects) {
return effects.compose(mergedTransformer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy