Created
April 11, 2019 00:00
-
-
Save kylepls/b8774b005cd12aa8f2b7c3cfc8bb5979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fuck.josh; | |
import java.util.HashMap; | |
import java.util.Map; | |
import rx.Observable; | |
import rx.subjects.PublishSubject; | |
public class NotificationLoop { | |
private final Map<Class<?>, PublishSubject<?>> containers = new HashMap<>(); | |
private <R> PublishSubject<R> getOrCreatePS(Class<R> tClass) { | |
PublishSubject<?> publishSubject = containers.get(tClass); | |
if (publishSubject == null) { | |
publishSubject = PublishSubject.create(); | |
containers.put(tClass, publishSubject); | |
} | |
return (PublishSubject<R>) publishSubject; | |
} | |
public <R> Observable<R> observeEvent(Class<R> clas) { | |
PublishSubject<R> publishSubject = getOrCreatePS(clas); | |
return publishSubject.asObservable(); | |
} | |
public <R> void callEvent(R r) { | |
containers.entrySet().forEach(entry -> { | |
if (entry.getKey().isAssignableFrom(r.getClass())) { | |
PublishSubject<R> tPublishSubject = (PublishSubject<R>) entry.getValue(); | |
tPublishSubject.onNext(r); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment