Created
May 12, 2017 04:06
-
-
Save CesarValiente/21ee7648adefc8d98a402e0985a41606 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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