Skip to content

Instantly share code, notes, and snippets.

@renatoathaydes
Created November 28, 2013 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoathaydes/7698625 to your computer and use it in GitHub Desktop.
Save renatoathaydes/7698625 to your computer and use it in GitHub Desktop.
Playing with Ceylon generics.
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