/
syntax.pm
119 lines (85 loc) · 2.05 KB
/
syntax.pm
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::Reactive
Has the cb() member.
BUILD
Maps constructor parameters to callbacks.
Maps callback types to watchers:
discrete callbacks = discrete watchers
role callbacks = role watchers
no callbacks = unwatched (promise?)
=head1 Reflex::Role::Reactive::BUILD calls cb_gather()
emit() syntax is preserved.
1. Role::Reactive handles it normally.
2. Local delivery is through the callback object.
$self->emit( event => \%args );
=head1 Callback constructor parameter.
=cut
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_coderef(\&subroutine),
);
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_method($self, "method"),
);
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
on_tick => cb_method("class", "method"),
);
=head1 Role constructor parameter.
=cut
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
cb_role($self, "rolename"),
);
=head1 Promise.
=cut
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
# cb_promise
);
while (my $event = $timer->next()) {
...;
}
=head1 Watcher.
=cut
my $timer = Reflex::Interval->new(
interval => 5,
auto_repeat => 1,
promise => 1,
);
$self->watch(
watcher => $timer,
event => "tick",
callback => ANY_RCB_EXCEPT_PROMISE,
);
=head1 Promise Again
The current syntax is too verbose.
$watcher has no purpose except to call watch().
Perhaps $watcher could be replaced by a Promise class that watches
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::Interval;
use Reflex::Callbacks qw(cb_promise);
use ExampleHelpers qw(eg_say);
my $watcher = Reflex::Base->new();
my $promise;
my $timer = Reflex::Interval->new(
interval => 1,
auto_repeat => 1,
);
$watcher->watch($timer, cb_promise(\$promise));
while (my $event = $promise->next()) {
eg_say("next() returned an event (@$event)");
}