Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 60 lines (46 sloc) 1.463 kB
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
1 #!/usr/bin/env perl
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored
2 # vim: ts=2 sw=2 noexpandtab
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
3
4 # This example illustrates explicit a promise-like form of callback.
5 # The promise acts as an event pipeline. Events emitted from the
6 # object are available one at a time from a promise method.
7 #
8 # Promises require some form of asynchrony. This example is larger
9 # than the others because it includes some custom mock-up code to
10 # stand in for the rest of Reflex.
11
12 # Reflex::Callbacks and the Reflex::Callback helper classes will
13 # abstract callbacks to fulfill a number of goals. The goals are
5f76cdd @rcaputo requirements.otl becomes TODO.otl, as it ought to be.
authored
14 # detailed in docs/TODO.otl and summarized in eg/eg-20-rcb-callback.pl
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
15
16 use warnings;
17 use strict;
15add25 @rcaputo Fix library paths to find common test helper libraries.
authored
18 use lib qw(t/lib);
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
19
0b515ca @rcaputo Convert the callback examples into tests.
authored
20 use Test::More tests => 3;
21
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
22 # Create a thing that will invoke callbacks.
23
24 {
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
25 package PromiseThing;
26 use Moose;
1f5ed27 @rcaputo Rename Reflex::Role::Object to Reflex::Role::Reactive; Reflex::Object…
authored
27 extends 'Reflex::Base';
bbf149d @rcaputo Create Reflex::Role::Interval, and reify it as Reflex::Interval. Nee…
authored
28 use Reflex::Interval;
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
29 use Reflex::Callbacks qw(gather_cb);
e0d6b99 @rcaputo Explicitly export watches() and emits() from their corresponding Refl…
authored
30 use Reflex::Trait::Watched qw(watches);
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
31
9513af3 @rcaputo Rename "observe" to "watch".
authored
32 watches ticker => (
bbf149d @rcaputo Create Reflex::Role::Interval, and reify it as Reflex::Interval. Nee…
authored
33 isa => 'Reflex::Interval',
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
34 setup => { interval => 1, auto_repeat => 1 },
35 );
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
36
37 has cb => ( is => 'rw', isa => 'Reflex::Callbacks' );
38
39 sub BUILD {
40 my ($self, $arg) = @_;
6eae87c @rcaputo Add "emits" and "observes" attribute declarations.
authored
41 $self->cb(gather_cb($self, $arg));
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
42 }
43
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
44 sub on_ticker_tick {
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
45 my $self = shift;
0d58684 @rcaputo Get promises working. Use Reflex::Timer with a promise.
authored
46 $self->cb()->deliver( event => {} );
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
47 }
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
48 }
49
50 use Reflex::Callbacks qw(cb_promise);
51
52 my $promise;
53 my $pt = PromiseThing->new( cb_promise(\$promise) );
54
0b515ca @rcaputo Convert the callback examples into tests.
authored
55 for (1..3) {
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
56 my $event = $promise->next();
0b515ca @rcaputo Convert the callback examples into tests.
authored
57 last unless $event;
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
58 pass("next($_) returned an event ($event->{name})");
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
59 }
Something went wrong with that request. Please try again.