livestream-kt / dev.shreyaspatil.livestream / LiveStream

LiveStream

class LiveStream<T : Any> : ILiveStream<T>

LiveStream is a data holder class which can be created and used anywhere in application. By using it, You can emit values to any stream with generic data from anywhere in the application. Observers will receive data events when the value of subscribed stream is updated. This class is designed to share data between different modules in your application.

Parameters

T - The type of a data.

Types

OnChangeListener

A simple callback that can retrieve from LiveStream.

interface OnChangeListener<T>

Constructors

<init>

LiveStream is a data holder class which can be created and used anywhere in application. By using it, You can emit values to any stream with generic data from anywhere in the application. Observers will receive data events when the value of subscribed stream is updated. This class is designed to share data between different modules in your application.

LiveStream()

Functions

getValue

Returns the current value of a stream. Note that calling this method on a background thread does not guarantee that the latest value set will be received.

fun getValue(stream: String): T?

post

Posts a task to main thread to set the given value. This method should be called from background thread. Otherwise, you can use set If there are active subscribers, the value will be dispatched to them.

fun post(stream: String, value: T?): Unit

set

Sets the value. If there are active subscribers, the value will be dispatched to them. This method must be called from the main thread. If you need set a value from a background thread, you can use post

fun set(stream: String, value: T?): Unit

subscribe

Subscribes to the given stream. The events are dispatched on the main thread. If stream already has data set, it will be delivered to the listener. You should call unsubscribe to stop observing LiveStream.

fun subscribe(stream: String, onChangeListener: OnChangeListener<T>): StreamObserver<T>

unsubscribe

Removes/unsubscribe the given stream observer.

fun unsubscribe(observer: StreamObserver<T>): Unit

Extension Functions

subscribe

Subscribes to the given stream. The events are dispatched on the main thread. If stream already has data set, it will be delivered to the listener. You should call LiveStream.unsubscribe to stop observing LiveStream.

fun <T : Any> LiveStream<T>.subscribe(stream: String, onChangeCallback: (T?) -> Unit): StreamObserver<T>