Created
April 28, 2012 11:37
-
-
Save jnthn/2518235 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Method @.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; | |
proto handle_call(|$) { * } | |
multi handle_call { | |
for $m.slots -> [$sender, $rcpt, $method] { | |
say $sender.WHAT, $rcpt.WHAT, $method.WHAT; | |
$method($rcpt); | |
} | |
} | |
multi handle_call(|$args){ | |
say $m.WHAT; | |
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