Lifecycle-aware pub/sub event system for Android.
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)'
Params:
eventName
- name of the eventdata
- the data that is send to the subscribers
SimpleEventHub.emit(eventName = "test-event", data = "message")
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 receiverreceiveCachedData
- 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 toreceiver
- callback used when a new event that matches the eventName
SimpleEventHub.on<String>(eventName = "test-event", bindToLifecycle = viewLifecycleOwner) {
Log.d("LOG_FROM_RECEIVER", "$it")
}
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 receiverreceiveCachedData
- 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()
To observe logs from the library, use the tag SimpleEventHub
in logcat.