Skip to content

Instantly share code, notes, and snippets.

View adam-arold's full-sized avatar
💭
Exploring the n'th dimension.

Adam Arold adam-arold

💭
Exploring the n'th dimension.
View GitHub Profile
interface EventBus {
fun <T : Event> subscribe(klass: KClass<T>,
callback: (T) -> Unit)
}
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)
fun usage() {
val subscriptions = mutableListOf<Subscription>()
subscriptions.cancelAll()
}
fun <T: Subscription> MutableList<T>.cancelAll() {
forEach {
it.cancel()
}
clear()
}
fun cancelAllSubscriptions() {
subscriptions.forEach {
it.cancel()
}
subscriptions.clear()
}
val subscriptions: MutableList<Subscription> =
mutableListOf()
fun Subscription.Companion.create(): Subscription {
TODO("Create new Subscription")
}
fun useIt() {
Subscription.create()
}
interface Subscription {
fun cancel()
companion object
}
// safe to use
package api
interface Component
interface DrawSurface
interface Container
// use them at your own risk!
object ComponentFactory {
fun createComponent(
children: Iterable<Component>,
drawSurface: DrawSurface): Component {
return MyComponent(
children = children,
drawSurface = drawSurface)
}
}