Permalink
Browse files

Hide some of the promise awkwardness in a Promise watcher.

It's a better solution, but it's not entirely satisfying.  It will
change as something better comes along.
  • Loading branch information...
1 parent 0d58684 commit f4f25e896e8495a404f3c8e12c9870d9b36583d5 @rcaputo committed Apr 8, 2010
Showing with 60 additions and 9 deletions.
  1. +29 −0 eg/ReflexPromise.pm
  2. +31 −9 eg/eg-30-promise-timer.pl
View
@@ -0,0 +1,29 @@
+package ReflexPromise;
+
+use Moose;
+extends 'Reflex::Object';
+
+use Reflex::Callbacks qw(cb_promise);
+
+has object => (
+ isa => 'Reflex::Object',
+ is => 'ro',
+);
+
+has promise => (
+ isa => 'ScalarRef',
+ is => 'ro',
+ default => sub { return \my $x },
+);
+
+sub BUILD {
+ my $self = shift;
+ $self->observe($self->object(), cb_promise($self->promise()));
+}
+
+sub wait {
+ my $self = shift;
+ return ${$self->promise()}->wait();
+}
+
+1;
View
@@ -9,16 +9,38 @@
use Reflex::Callbacks qw(cb_promise);
use ExampleHelpers qw(eg_say);
-my $watcher = Reflex::Object->new();
+# Most verbose syntax.
+# TODO - The $watcher is useless. How can it be eliminated?
-my $promise;
-my $timer = Reflex::Timer->new(
- interval => 1,
- auto_repeat => 1,
-);
+{
+ my $timer = Reflex::Timer->new(
+ interval => 1,
+ auto_repeat => 1,
+ );
-$watcher->observe($timer, cb_promise(\$promise));
+ my $promise;
+ my $watcher = Reflex::Object->new();
+ $watcher->observe($timer, cb_promise(\$promise));
-while (my $event = $promise->wait()) {
- eg_say("wait() returned an event (@$event)");
+ while (my $event = $promise->wait()) {
+ eg_say("wait() returned an event (@$event)");
+ }
+}
+
+# Wrap some of the syntax into a Reflex::Promise object.
+# TODO - Still has a $watcher, but it's hidden in a Reflex::Promise.
+
+{
+ use ReflexPromise;
+
+ my $p = ReflexPromise->new(
+ object => Reflex::Timer->new(
+ interval => 1,
+ auto_repeat => 1,
+ )
+ );
+
+ while (my $event = $p->wait()) {
+ eg_say("wait() returned an event (@$event)");
+ }
}

0 comments on commit f4f25e8

Please sign in to comment.