This file contains hidden or 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 EventBus { | |
fun <T : Event> subscribe(klass: KClass<T>, | |
callback: (T) -> Unit) | |
} |
This file contains hidden or 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
class EventBus { | |
private val subscriptions = | |
mutableMapOf<KClass<out Any>, (Any) -> Unit>() | |
inline fun <reified T : Event> subscribe( | |
noinline callback: (T) -> Unit) { | |
return subscribe( | |
klass = T::class, | |
callback = callback) |
This file contains hidden or 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
fun usage() { | |
val subscriptions = mutableListOf<Subscription>() | |
subscriptions.cancelAll() | |
} |
This file contains hidden or 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
fun <T: Subscription> MutableList<T>.cancelAll() { | |
forEach { | |
it.cancel() | |
} | |
clear() | |
} |
This file contains hidden or 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
fun cancelAllSubscriptions() { | |
subscriptions.forEach { | |
it.cancel() | |
} | |
subscriptions.clear() | |
} |
This file contains hidden or 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
val subscriptions: MutableList<Subscription> = | |
mutableListOf() |
This file contains hidden or 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
fun Subscription.Companion.create(): Subscription { | |
TODO("Create new Subscription") | |
} | |
fun useIt() { | |
Subscription.create() | |
} |
This file contains hidden or 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 Subscription { | |
fun cancel() | |
companion object | |
} |
This file contains hidden or 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
// safe to use | |
package api | |
interface Component | |
interface DrawSurface | |
interface Container | |
// use them at your own risk! |
This file contains hidden or 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
object ComponentFactory { | |
fun createComponent( | |
children: Iterable<Component>, | |
drawSurface: DrawSurface): Component { | |
return MyComponent( | |
children = children, | |
drawSurface = drawSurface) | |
} | |
} |