Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 65 lines (51 sloc) 1.547 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',
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
34 setup => { interval => 0.25, auto_repeat => 1 },
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
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;
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
46 $self->cb()->deliver(
47 Reflex::Event->new(
48 _name => "event",
49 _emitters => [ $self ],
50 )
51 );
afd7c10 @rcaputo Fix tab damage. No significant changes.
authored
52 }
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
53 }
54
55 use Reflex::Callbacks qw(cb_promise);
56
57 my $promise;
58 my $pt = PromiseThing->new( cb_promise(\$promise) );
59
0b515ca @rcaputo Convert the callback examples into tests.
authored
60 for (1..3) {
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored
61 my $event = $promise->next();
0b515ca @rcaputo Convert the callback examples into tests.
authored
62 last unless $event;
5c57d49 @rcaputo !!! Experiment with Reflex events being objects.
authored
63 pass("next($_) returned an event (" . $event->_name() . ")");
701249a @rcaputo Initial implementation of promises, completing the callback types.
authored
64 }
Something went wrong with that request. Please try again.