Skip to content

Lifecycle-aware pub/sub event system for Android.

License

Notifications You must be signed in to change notification settings

tsvetilian-ty/SimpleEventHub

Repository files navigation

SimpleEventHub

Lifecycle-aware pub/sub event system for Android.

Release Android Min SDK GitHub license Maven Central

Content

Add in your application

If you are building with Gradle, add the following line to the dependencies section of your build.gradle file:

implementation 'com.tsvetilian:simple-eventhub:(latest version)'

How to use

Emit an event

Params:

  • eventName - name of the event
  • data - the data that is send to the subscribers
SimpleEventHub.emit(eventName = "test-event", data = "message")

Subscribers

Lifecycle-aware subscriber

The subscriber will be automatically disposed when the lifecycle owner's state is Lifecycle.Event.ON_STOP

Params:

  • eventName - name of the event that will be used to determine when to call the subscriber's receiver
  • receiveCachedData - receive the cached data from the last event emission if such is available Default: false
  • observeOn - the thread that will be used to receive the data Default: main thread
  • bindToLifecycle - lifecycle owner that the listener is bind to
  • receiver - callback used when a new event that matches the eventName
SimpleEventHub.on<String>(eventName = "test-event", bindToLifecycle = viewLifecycleOwner) {
  Log.d("LOG_FROM_RECEIVER", "$it")
}

Disposable subscriber

The method will return DisposableSubscriber that can be used for disposing of the subscriber at any time.

Params:

  • eventName - name of the event that will be used to determine when to call the subscriber's receiver
  • receiveCachedData - receive the cached data from the last event emission if such is available Default: false
  • observeOn - the thread that will be used to receive the data by the receiver Default: main thread
  • receiver - callback used when a new event that matches the eventName
val eventSubscriber = SimpleEventHub.on<String>(eventName = "test-event") {
  Log.d("LOG_FROM_RECEIVER", "$it")
}

eventSubscriber.dispose()

Logs

To observe logs from the library, use the tag SimpleEventHub in logcat.