Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 119 lines (85 sloc) 2.086 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
Documenting Reflex syntax evolution.

=head1 Gathering callbacks.

Reflex::Role::Object
Has the cb() member.
BUILD
Maps constructor parameters to callbacks.
Maps callback types to observers:
discrete callbacks = discrete observers
role callbacks = role observers
no callbacks = unobserved (promise?)

=head1 Reflex::Role::Object::BUILD calls cb_gather()

emit() syntax is preserved.
1. Role::Object handles it normally.
2. Local delivery is through the callback object.

$self->emit( event => \%args );

=head1 Callback constructor parameter.

=cut

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_coderef(\&subroutine),
);

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_method($self, "method"),
);

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_method("class", "method"),
);

=head1 Role constructor parameter.

=cut

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
cb_role($self, "rolename"),
);

=head1 Promise.

=cut

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
# cb_promise
);

while (my $event = $timer->wait()) {
...;
}

=head1 Observer.

=cut

my $timer = Reflex::Timer->new(
interval => 5,
auto_repeat => 1,
promise => 1,
);

$self->observe(
observed => $timer,
event => "tick",
callback => ANY_RCB_EXCEPT_PROMISE,
);

=head1 Promise Again

The current syntax is too verbose.

$watcher has no purpose except to call observe().

Perhaps $watcher could be replaced by a Promise class that observes
and then returns events? Similar to PromiseThing in
eg-25-rcb-promise.pl?

=cut

#!/usr/bin/env perl

use warnings;
use strict;

use lib qw(../lib);

use Reflex::Timer;
use Reflex::Callbacks qw(cb_promise);
use ExampleHelpers qw(eg_say);

my $watcher = Reflex::Object->new();

my $promise;
my $timer = Reflex::Timer->new(
interval => 1,
auto_repeat => 1,
);

$watcher->observe($timer, cb_promise(\$promise));

while (my $event = $promise->wait()) {
eg_say("wait() returned an event (@$event)");
}
Something went wrong with that request. Please try again.