Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 789a130f73
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 547 lines (415 sloc) 13.411 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
package Reflex::Callbacks;
# vim: ts=2 sw=2 noexpandtab

# Reflex::Callbacks is a callback manager. It encapsulates the
# callbacks for an object. Via deliver(), it maps event names to the
# corresponding callbacks, then invokes them through the underlying
# callback system.
#
# On another level, it makes sure all the callback classes are loaded
# and relevant coercions are defined.
#
# TODO - Explore whether it's sensible for the underlying callback
# system to be pluggable.

use Moose;
use Moose::Util::TypeConstraints;

use Reflex::Callback;
use Reflex::Callback::CodeRef;
use Reflex::Callback::Method;
use Reflex::Callback::Promise;

use Moose::Exporter;
Moose::Exporter->setup_import_methods(
as_is => [
qw(
cb_class
cb_coderef
cb_method
cb_object
cb_promise
cb_role
gather_cb
)
],
with_caller => [
qw(
make_emitter
make_terminal_emitter
make_error_handler
make_null_handler
)
],
);

use Carp qw(croak);

has callback_map => (
is => 'rw',
isa => 'HashRef[Reflex::Callback]',
default => sub { {} },
);

coerce 'Reflex::Callback'
=> from 'CodeRef'
=> via { die; Reflex::Callback::CodeRef->new( code_ref => $_ ) };

coerce 'Reflex::Callback'
=> from 'Str'
=> via {
die;
Reflex::Callback::Method->new(
method_name => $_,
)
};

coerce 'Reflex::Callback'
=> from 'ArrayRef'
=> via {
die;
Reflex::Callback::Method->new(
object => $_->[0],
method_name => $_->[1],
)
};

sub cb_method {
my ($object, $method_name) = @_;
return Reflex::Callback::Method->new(
object => $object,
method_name => $method_name,
);
}

sub cb_object {
my ($object, $methods) = @_;

# They passed us a scalar. Emulate cb_methods().
return("on_$methods" => cb_method($object, $methods)) unless ref $methods;

# Events match method names.
return( map { ("on_$_" => cb_method($object, $_)) } @$methods ) if (
ref($methods) eq "ARRAY"
);

return (
map { ("on_$_" => cb_method($object, $methods->{$_})) }
keys %$methods
) if ref($methods) eq "HASH";

croak "cb_object with unknown methods type: $methods";
}

# A bit of a cheat. Goes with the Object|Str type constraint in
# Reflex::Callback::Method.
sub cb_class {
cb_object(@_);
}

# Role callbacks inspect the handler object or class methods and
# determine the events being handled by their names.
sub cb_role {
my ($invocant, $role, $prefix) = @_;
$prefix = "on" unless defined $prefix;

my $method_prefix = qr/^${prefix}_${role}_(\S+)/;

my @class_methods = (
grep /$method_prefix/,
map { $_->name() }
$invocant->meta()->get_all_methods()
);

my @events = (
map { /$method_prefix/; $1 }
@class_methods
);

my %methods;
@methods{@events} = @class_methods;

return cb_object($invocant, \%methods);
}

sub cb_promise {
my $promise_ref = shift;

$$promise_ref = Reflex::Callback::Promise->new();
return( on_promise => $$promise_ref );
}

sub cb_coderef (&) {
return Reflex::Callback::CodeRef->new(code_ref => shift);
}

sub gather_cb {
my ($owner, $arg, $match) = @_;
$match = qr/^on_/ unless defined $match;

my %return;

# TODO - Also analyze whether the value is a Reflex::Callack object.
foreach (grep /$match/, keys %$arg) {
die unless defined $arg->{$_};
my $callback = $arg->{$_};

if (blessed $callback) {
if ($callback->isa('Reflex::Callback::Promise')) {
return $callback;
}

if ($callback->isa('Reflex::Callback')) {
$callback->object($owner) unless $callback->object();
$return{$_} = $callback;
next;
}

die "blessed callback $_";
}

# Unblessed callback types must be coerced.

if (ref($callback) eq "CODE") {
$return{$_} = Reflex::Callback::CodeRef->new(
object => $owner,
code_ref => $callback,
);
next;
}

die "unblessed callback $_";
}

return Reflex::Callbacks->new( callback_map => \%return );
}

sub deliver {
my ($self, $event) = @_;

my $event_name = $event->_name();
$event_name =~ s/^(on_)?/on_/;

$self->callback_map()->{$event_name}->deliver($event);
}

sub make_emitter {
my $caller = shift();

my $meta = Class::MOP::class_of($caller);

my ($method_name, $event_name) = @_;

my $method = $meta->method_metaclass->wrap(
package_name => $caller,
name => $method_name,
body => sub {
my ($self, $event) = @_;
$self->re_emit( $event, -name => $event_name );
},
);

$meta->add_method($method_name => $method);

return $method_name;
}

sub make_terminal_emitter {
my $caller = shift();

my $meta = Class::MOP::class_of($caller);

my ($method_name, $event_name) = @_;

my $method = $meta->method_metaclass->wrap(
package_name => $caller,
name => $method_name,
body => sub {
my ($self, $event) = @_;
$self->re_emit( $event, -name => $event_name );
$self->stopped();
},
);

$meta->add_method($method_name => $method);

return $method_name;
}

sub make_error_handler {
my $caller = shift();

my $meta = Class::MOP::class_of($caller);

my ($method_name, $event_name) = @_;

my $method = $meta->method_metaclass->wrap(
package_name => $caller,
name => $method_name,
body => sub {
my ($self, $event) = @_;
warn $event->formatted(), "\n";
$self->stopped();
},
);

$meta->add_method($method_name => $method);

return $method_name;
}

sub make_null_handler {
my $caller = shift();

my $meta = Class::MOP::class_of($caller);

my ($method_name, $event_name) = @_;

my $method = $meta->method_metaclass->wrap(
package_name => $caller,
name => $method_name,
body => sub { undef },
);

$meta->add_method($method_name => $method);

return $method_name;
}

1;

__END__

=head1 NAME

Reflex::Callbacks - Convenience functions for creating and using callbacks

=head1 SYNOPSIS

This package contains several helper functions, each with its own use
case. Please see individual examples.

=head1 DESCRIPTION

Reflex::Callback and its subclasses implement the different types of
calbacks that Reflex supports. Reflex::Callbacks provides convenience
functions that are almost always used instead of Reflex::Callback
objects.

Reflex::Callback's generic interface is a constructor and a single
method, deliver(), which routes its parameters to their destination.
Subclasses may implement additional methods to support specific use
cases.

=head2 cb_method

Creates and returns Reflex::Callback::Method object. Accepts two
positional parameters: the object reference and method name to invoke
when the callback is delivered.

Relex::Callback::Method's SYNOPSIS has an example, as does the eg
directory in Reflex's distribution.

=head2 cb_object

cb_object() converts the specification of multiple callbacks into a
list of callback parameter names and their Reflex::Callback::Method
objects. The returned list is in a form suitable for a Reflex::Base
constructor.

cb_object() takes two positional parameters. The first is the object
reference that will handle callbacks. The second describes the events
and methods that will handle them. It may be a scalar string, an
array reference, or a hash reference.

If the second parameter is a scalar string, then a single method will
handle a single event. The event and method names will be identical.
cb_object() will then return two values: the event name, and the
Reflex::Callback::Method to invoke the corresponding object method.

use Reflex::Callbacks qw(cb_object);
my $object = bless {};
my @cbs = cb_object($object, "event");

# ... is equivalent to:

use Reflex::Callback::Method;
my $object = bless {};
my @cbs = (
on_event => Reflex::Callback::Method->new(
object => $object, method_name => "event"
)
);

If the second parameter is an array reference of event names, then one
Reflex::Callback::Method will be created for each event. The event
names and method names will be identical.

use Reflex::Callbacks qw(cb_object);
my $object = bless {};
my @cbs = cb_object($object, ["event_one", "event_two"]);

# ... is equivalent to:

use Reflex::Callback::Method;
my $object = bless {};
my @cbs = (
on_event_one => Reflex::Callback::Method->new(
object => $object, method_name => "event_one"
),
on_event_two => Reflex::Callback::Method->new(
object => $object, method_name => "event_two"
),
);

If the second parameter is a hash reference, then it should be keyed
on event name. The corresponding values should be method names. This
syntax allows event and method names to differ.

use Reflex::Callbacks qw(cb_object);
my $object = bless {};
my @cbs = cb_object($object, { event_one => "method_one" });

# ... is equivalent to:

use Reflex::Callback::Method;
my $object = bless {};
my @cbs = (
on_event_one => Reflex::Callback::Method->new(
object => $object, method_name => "method_one"
)
);

=head2 cb_class

cb_class() is an alias for cb_object(). Perl object and class methods
currently behave the same, so there is no need for additional code at
this time.

=head2 cb_role

cb_role() implements Reflex's role-based callbacks. These callbacks
rely on method names to contain clues about the objects and events
being handled. For instance, a method named on_resolver_answer()
hints that it handles the "answer" events from a sub-object with the
role of "resolver".

cb_role() requires two parameters and has a third optional one. The
first two parameters are the callback object reference and the role of
the object for which it handles events. The third optional parameter
overrides the "on" prefix with a different one.

{
package Handler;
sub on_resolver_answer { ... }
sub on_resolver_failure { ... }
}

# This role-based definition:

use Reflex::Callbacks qw(cb_role);
my $object = Handler->new();
my @cbs = cb_role($object, "resolver");

# ... is equivalent to:

use Reflex::Callbacks qw(cb_object);
my $object = Handler->new();
my @cbs = cb_object(
$object, {
answer => "on_resolver_answer",
failure => "on_resolver_failure",
}
);

# ... or:

use Reflex::Callbacks qw(cb_method);
my $object = Handler->new();
my @cbs = (
on_answer => Reflex::Callback::Method->new(
object => $object, method_name => "on_resolver_answer"
),
on_failure => Reflex::Callback::Method->new(
object => $object, method_name => "on_resolver_failure"
),
);

=head2 cb_promise

cb_promise() takes a scalar reference. This reference will be
populated with a Reflex::Callback::Promise object.

cb_promise() returns two values that are suitable to insert onto a
Reflex::Base's constructor. The first value is a special event name,
"on_promise", that tells Reflex::Base objects they may be used inline
as promises. The second return value is the same
Reflex::Callback::Promise object that was inserted into cb_promise()'s
parameter.

use Reflex::Callbacks qw(cb_promise);
my $promise;
my @cbs = cb_promise(\$promise);

# ... is eqivalent to:

use Reflex::Callback::Promise;
my $promise = Reflex::Callback::Promise->new();
@cbs = ( on_promise => $promise );

=head2 cb_coderef

cb_coderef() takes a single parameter, a coderef to callback. It
returns a single value: a Reflex::Callback::Coderef object that will
deliver events to the callback.

cb_coderef() neither takes nor returns an event name. As such, the
Reflex::Base parameter name must be supplied outside cb_coderef().

my $timer = Reflex::Interval->new(
interval => 1,
auto_repeat => 1,
on_tick => cb_coderef { print "tick!\n" },
);

As shown above, cb_coderef() is prototyped to make the callback's
C<sub> declaration optional.

=head1 Usages Outside Reflex

Reflex callbacks are designed to be independent of any form of
concurrency. Reflex::Callbacks provides two convenience functions
that other class libraries may find useful but Reflex doesn't use.

Please contact the authors if there's interest in using these
functions, otherwise they may be deprecated.

=head2 gather_cb

The gather_cb() function extracts callbacks from an object's
constructor parameters and encapsulates them in a Reflex::Callbacks
object.

gather_cb() takes three parameters: The object that will own the
callbacks, a hash reference containing a constructor's named
parameters, and an optional regular expression to match callback
parameter names. By default, gather_cb() will collect
parameters matching C</^on_/>.

package ThingWithCallbacks;
use Moose;

use Reflex::Callbacks qw(gather_cb);

has cb => ( is => 'rw', isa => 'Reflex::Callbacks' );

sub BUILD {
my ($self, $arg) = @_;
$self->cb(gather_cb($self, $arg));
}

sub run {
my $self = shift;
$self->cb()->deliver( event => {} );
}

=head1 deliver

deliver() is a method of Reflex::Callback, not a function. It takes
two parameters: the name of an event to deliver, and a hash reference
containing named values to include with the event.

deliver() finds the callback that corresponds to its event. It then
delivers the event to that callback. The callback must have been
collected by gather_cb().

See the example for gather_cb(), which also invokes deliver().

=head1 SEE ALSO

L<Reflex>
L<Reflex::Callback::CodeRef>
L<Reflex::Callback::Method>
L<Reflex::Callback::Promise>
L<Reflex::Callbacks> documents callback convenience functions.

L<Reflex/ACKNOWLEDGEMENTS>
L<Reflex/ASSISTANCE>
L<Reflex/AUTHORS>
L<Reflex/BUGS>
L<Reflex/BUGS>
L<Reflex/CONTRIBUTORS>
L<Reflex/COPYRIGHT>
L<Reflex/LICENSE>
L<Reflex/TODO>

=cut
Something went wrong with that request. Please try again.