Skip to content

Instantly share code, notes, and snippets.

@therajanmaurya
Created July 22, 2018 17:45
Show Gist options
  • Save therajanmaurya/3117e79af9c5358a4373dec82b0d0326 to your computer and use it in GitHub Desktop.
Save therajanmaurya/3117e79af9c5358a4373dec82b0d0326 to your computer and use it in GitHub Desktop.
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
// Use object so we have a singleton instance
object RxBus {
private val publisher = PublishSubject.create<Any>()
fun publish(event: Any) {
publisher.onNext(event)
}
// Listen should return an Observable and not the publisher
// Using ofType we filter only events that match that class type
fun <T> listen(eventType: Class<T>): Observable<T> = publisher.ofType(eventType)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment