Skip to content

Instantly share code, notes, and snippets.

@peschwa
Forked from anonymous/gist:2519842
Last active November 18, 2015 00:06
Show Gist options
  • Save peschwa/6adea7ae5fc09c4f0914 to your computer and use it in GitHub Desktop.
Save peschwa/6adea7ae5fc09c4f0914 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