Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2012 15:32
Show Gist options
  • Save anonymous/2519842 to your computer and use it in GitHub Desktop.
Save anonymous/2519842 to your computer and use it in GitHub Desktop.
use v6;
role Signal {
has @.slots; # a nice list of callbacks (that are methods)
multi method connect(Any:D $rcpt, Method $method){
@.slots.push([self, $rcpt, $method]);
}
}
multi trait_mod:<is>(Routine $_signal, :$signal!){
$_signal does Signal;
$_signal.slots; # WORKAROUND RT112666
multi handle_call($args, [ Signal $s, Any:D $receiver, Method $slot ]){
$slot($receiver, |$args);
}
$_signal.wrap(sub (|$args) {
handle_call($args, $_) for $_signal.slots;
# ^^^^^ HERE BE DRAGONS, big ugly sefault
# |$args does not segfault
});
}
multi sub connect(Signal $signal, Any:D $rcpt, Method $slot){
$signal.connect($rcpt, $slot);
}
class App {
our method exit { say "App::exit called" }
}
my App $app .= new;
sub bare_signal_exit is signal {}
connect(&bare_signal_exit, $app, &App::exit);
bare_signal_exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment