Revisions
-
manzanit0 revised this gist
Sep 30, 2019 . 2 changed files with 51 additions and 12 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +0,0 @@ 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 charactersOriginal 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("") } -
orangy renamed this gist
Apr 22, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
orangy revised this gist
Apr 22, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ 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 } -
orangy revised this gist
Apr 22, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal 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 main(args : Array<String>) { e += { println(this) } // subscribe e("sdfsdf") // invoke } -
orangy created this gist
Apr 22, 2014 .There are no files selected for viewing
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 charactersOriginal 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 }