Skip to content

Latest commit

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

RxBus

An event bus in 27 LOC.

  1. Add Kotlin, RxJava 1 and RxAndroid to you project.

  2. Copy and past the RxBus code into project

object RxBus {
privateval mBusSubject =SerializedSubject(BehaviorSubject.create<Any>())
lateinitvar uiScheduler:Scheduleroperatorfuninvoke(sch:Scheduler) { uiScheduler = sch }
fun <T> register(eventClass:Class<T>): Observable<T> = mBusSubject.filter({ event -> eventClass.isInstance(event) })
.observeOn(uiScheduler)
.onBackpressureBuffer()
.map<T>({ eventClass.cast(it) })
funpost(event:Any) = mBusSubject.onNext(event)
}
  1. Create your event class eg
classEvent
  1. Instantiate RxBus in onCreate of your application class with RxBus(AndroidSchedulers.mainThread())

  2. Register the event and wait to consume them.

RxBus.register(Event::class.java)
.subscribe{ event ->// trigger something
}
  1. Post an event with
RxBus.post(Event())
  1. Be happy!!!

About

An event bus in 27 LOC.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors