Skip to content

Instantly share code, notes, and snippets.

@manzanit0
Forked from orangy/event.kt
Last active September 30, 2019 09:46

Revisions

  1. manzanit0 revised this gist Sep 30, 2019. 2 changed files with 51 additions and 12 deletions.
    12 changes: 0 additions & 12 deletions event.kt
    Original file line number Diff line number Diff line change
    @@ -1,12 +0,0 @@
    class Event<T> {
    private val handlers = arrayListOf<(Event<T>.(T) -> Unit)>()
    fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) }
    fun invoke(value: T) { for (handler in handlers) handler(value) }
    }

    val e = Event<String>() // define event

    fun main(args : Array<String>) {
    e += { println(it) } // subscribe
    e("sdfsdf") // invoke
    }
    51 changes: 51 additions & 0 deletions main.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    /**
    * This is a generic event class. It simply allows subscription and invocation.
    */
    class Event<T> {
    private val handlers = arrayListOf<(Event<T>.(T) -> Unit)>()
    operator fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) }
    operator fun invoke(value: T) { for (handler in handlers) handler(value) }
    }

    /**
    * Each controller would have a companion object which would be the event emitted by
    * each endpoint. The event would be a tuple of the function invoked and its payload.
    */
    class SomeController {
    companion object {
    var eventEmitted = Event<Pair<String, String>>()
    }

    fun post(body: Any): String {
    // Ideally this "eventEmitted" invocation could be abstracted away in a parent class?
    eventEmitted(Pair("SomeController::post", "post method invoked"))
    return "Return POST value"
    }

    fun get(): String {
    eventEmitted(Pair("SomeController::get", "get method invoked"))
    return "Return GET value"
    }
    }

    /**
    * The Notifications is the one which handles which events trigger which logic. Potentially there could
    * be some sugar syntax applied here to not have to do the whole if/then ceremony.
    */
    class NotificationsManager {
    fun setupEvents() {
    SomeController.eventEmitted += { if (it.first == "SomeController::get") println("get's event payload >>> ${it.second}") }
    SomeController.eventEmitted += { if (it.first == "SomeController::post") println("post's event payload >>> ${it.second}") }
    }
    }

    /**
    * Runnable example :)
    */
    fun main(args : Array<String>) {
    val controller = SomeController()
    NotificationsManager().setupEvents()
    controller.get()
    controller.get()
    controller.post("")
    }
  2. @orangy orangy renamed this gist Apr 22, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @orangy orangy revised this gist Apr 22, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.kt
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    class Event<T> {
    private val handlers = arrayListOf<(T.(Event<T>) -> Unit)>()
    fun plusAssign(handler: T.(Event<T>) -> Unit) { handlers.add(handler) }
    fun invoke(value: T) { for (handler in handlers) value.handler(this) }
    private val handlers = arrayListOf<(Event<T>.(T) -> Unit)>()
    fun plusAssign(handler: Event<T>.(T) -> Unit) { handlers.add(handler) }
    fun invoke(value: T) { for (handler in handlers) handler(value) }
    }

    val e = Event<String>() // define event

    fun main(args : Array<String>) {
    e += { println(this) } // subscribe
    e += { println(it) } // subscribe
    e("sdfsdf") // invoke
    }
  4. @orangy orangy revised this gist Apr 22, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.kt
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@ class Event<T> {
    fun plusAssign(handler: T.(Event<T>) -> Unit) { handlers.add(handler) }
    fun invoke(value: T) { for (handler in handlers) value.handler(this) }
    }

    val e = Event<String>() // define event

    fun fn() {
    e += { println(it) } // subscribe
    fun main(args : Array<String>) {
    e += { println(this) } // subscribe
    e("sdfsdf") // invoke
    }
  5. @orangy orangy created this gist Apr 22, 2014.
    12 changes: 12 additions & 0 deletions gistfile1.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    class Event<T> {
    private val handlers = arrayListOf<(T.(Event<T>) -> Unit)>()
    fun plusAssign(handler: T.(Event<T>) -> Unit) { handlers.add(handler) }
    fun invoke(value: T) { for (handler in handlers) value.handler(this) }
    }

    val e = Event<String>() // define event

    fun fn() {
    e += { println(it) } // subscribe
    e("sdfsdf") // invoke
    }