Created
May 30, 2012 00:37
-
-
Save diakopter/2831711 to your computer and use it in GitHub Desktop.
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
use v6; | |
role Signal { | |
has @.slots; # a nice list of callbacks (that are methods) | |
method connect(Any $sender, Any $rcpt, Method $method){ | |
@.slots.push([$sender, $rcpt, $method]); | |
} | |
} | |
multi trait_mod:<is>(Method $m, :$signal!){ | |
$m does Signal; | |
multi handle_call { | |
for $m.slots -> [$sender, $rcpt, $method] { | |
say $sender.WHAT, $rcpt.WHAT, $method.WHAT; | |
$method($rcpt); | |
} | |
} | |
multi handle_call(|$args){ | |
for $m.slots -> [$sender, $rcpt, $method] { | |
say $sender.WHAT, $rcpt.WHAT, $method.WHAT; | |
$method($rcpt, |$args); | |
} | |
} | |
$m.wrap(&handle_call); | |
} | |
multi sub connect($sender, Method $signal, $rcpt, Method $slot){ | |
$signal.connect($sender, $rcpt, $slot); | |
} | |
class Button { | |
our method pressed is signal { warn "signal fired" } | |
} | |
class App { | |
our method exit { die "gotta run!" } | |
} | |
my Button $btn_exit .= new; | |
my App $app .= new; | |
connect($btn_exit, &Button::pressed, $app, &App::exit); | |
$btn_exit.pressed; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment