NAME
Reflex::Callback::Promise - Non-callback, inline Promise adapter
VERSION
version 0.056
SYNOPSIS
Used within Reflex:
use Reflex::Timer;
use ExampleHelpers qw(eg_say);
my $pt = Reflex::Timer->new(
interval => 1 + rand(),
auto_repeat => 1,
);
while (my $event = $pt->next()) {
eg_say("promise timer returned an event (@$event)");
}
Low-level usage:
use Reflex::Callback::Promise;
my $cb = Reflex::Callback::Promise->new();
$cb->deliver( greet => { name => "world" } );
my $event = $cb->next();
print "event '$event->{name}': hello, $event->{arg}{name}\n";
DESCRIPTION
"In computer science, future, promise, and delay refer to constructs used for synchronization in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially not known, usually because the computation of its value has not yet completed." -- http://en.wikipedia.org/wiki/Promise_%28programming%29
Reflex::Callback::Promise enables Reflex objects to be used as inline event streams. Reflex::Callback::Promise and Reflex::Role::Reactive transparently handle the conversion. Reflex objects do not need special code to be used this way.
In most cases, Reflex::Callbacks::cb_promise() or other syntactic sweeteners will be used instead of raw Reflex::Callback::Promise objects. For example, promises are implicitly enabled if no callbacks are defined:
my $t = Reflex::Timer->new(
interval => 1,
auto_repeat => 1,
);
while (my $event = $t->next()) {
print "next() returned an event\n";
}
new
Reflex::Callback::Promise's constructor takes no parameters. It creates a promise queue that is populated by deliver() and drained by next(). Furthermore, next() will block as necessary until it can return an event. This requires the help of some form of concurrency, currently hardcoded to use POE.
A future version may delegate the POE dependency to a subclass.
next
Reflex::Callback::Promise's next() method retrieves the next pending event held in the object's queue. If the queue is empty, next() will dispatch other events until some asynchronous code enqueues a new event in the promise's queue.
deliver
Reflex::Callback::Promise's deliver() enqueues events for the promise. As with other Reflex::Callback subclasses, this deliver() accepts two positional parameters: an event name (which IS used), and a hashref of named parameters to be passed to the callback.
Deliver doesn't return anything meaningful, since the code to handle the event isn't executed at the time of delivery.
SEE ALSO
Reflex Reflex::Callback Reflex::Callbacks
"ACKNOWLEDGEMENTS" in Reflex "ASSISTANCE" in Reflex "AUTHORS" in Reflex "BUGS" in Reflex "BUGS" in Reflex "CONTRIBUTORS" in Reflex "COPYRIGHT" in Reflex "LICENSE" in Reflex "TODO" in Reflex