NAME

Reflex::Callback::Promise - Non-callback, inline Promise adapter

VERSION

This document describes version 0.100, released on April 02, 2017.

SYNOPSIS

Used within Reflex:

use Reflex::Interval;
use ExampleHelpers qw(eg_say);

my $pt = Reflex::Interval->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::Interval->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

Please see those modules/websites for more information related to this module.

BUGS AND LIMITATIONS

You can make new bug reports, and view existing ones, through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Reflex.

AUTHOR

Rocco Caputo <rcaputo@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Rocco Caputo.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

AVAILABILITY

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see https://metacpan.org/module/Reflex/.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.