Skip to content

Instantly share code, notes, and snippets.

@CesarValiente
Created May 12, 2017 04:06
Show Gist options
  • Save CesarValiente/21ee7648adefc8d98a402e0985a41606 to your computer and use it in GitHub Desktop.
Save CesarValiente/21ee7648adefc8d98a402e0985a41606 to your computer and use it in GitHub Desktop.
interface Subscriber<in T> {
fun onNext(data: T)
}
abstract class Dispatcher<T> {
private val subscriptions = ArrayList<Subscriber<T>>()
fun subscribe(subscriber: Subscriber<T>): Boolean =
subscriptions.add(subscriber)
fun unsubscribe(subscriber: Subscriber<T>): Boolean =
subscriptions.remove(subscriber)
// Other functions you want to have like size, unsubscribeAll, etc.
open fun dispatch(data: T) =
subscriptions.forEach { it.onNext(data) }
fun isSubscribed(subscriber: Subscriber<T>): Boolean {
return subscriptions.any { it == subscriber }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment