Created
November 28, 2013 21:55
-
-
Save renatoathaydes/7698625 to your computer and use it in GitHub Desktop.
Playing with Ceylon generics.
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
shared void run() { | |
object mouseDownBus extends EventBus<MouseDown>() {} | |
object mouseUpBus extends EventBus<MouseUp>() {} | |
mouseDownBus.listen(void (MouseDown event) => print("1 - " + event.string)); | |
mouseDownBus.listen(void (MouseDown event) => print("2 - " + event.string)); | |
mouseUpBus.listen(void (MouseUp event) => print("3 - " + event.string)); | |
mouseDownBus.fire(mouseDown); | |
mouseUpBus.fire(mouseUp); | |
} | |
interface Event of MouseDown|MouseUp {} | |
abstract class MouseDown() of mouseDown satisfies Event { shared actual String string => "mouseDown"; } | |
abstract class MouseUp() of mouseUp satisfies Event { shared actual String string => "mouseUp"; } | |
object mouseDown extends MouseDown() {} | |
object mouseUp extends MouseUp() {} | |
interface EventSource<in E> given E satisfies Event { | |
shared formal void fire(E event); | |
} | |
interface EventBroadcaster<out E> given E satisfies Event { | |
shared formal void listen(void onEvent(E event)); | |
} | |
abstract class EventBus<E>() satisfies EventSource<E> & EventBroadcaster<E> given E satisfies Event { | |
variable {Anything(E)*} listeners = {}; | |
shared actual void fire(E event) { | |
for (handle in listeners) { | |
handle(event); | |
} | |
} | |
shared actual void listen(void onEvent(E event)) { | |
listeners = listeners.chain({onEvent}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment