Skip to content
Newer
Older
100644 135 lines (107 sloc) 2.57 KB
6da75c7 @rcaputo Get all callbacks working in an approximation of a "good" way.
authored Apr 8, 2010
1 #!/usr/bin/env perl
2
3 # This is pretty close to the final syntax.
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored Jun 15, 2010
4 # TODO - Provide a way to next() on multiple objects at once.
5 # ...... maybe by next() on a collection?
6da75c7 @rcaputo Get all callbacks working in an approximation of a "good" way.
authored Apr 9, 2010
6 # TODO - Clean out all previous promise-like examples.
7
8 use warnings;
9 use strict;
10
11 use lib qw(../lib);
12
13 use Reflex::Timer;
14 use ExampleHelpers qw(eg_say);
15
16 ### Handle timer ticks with coderefs.
17
18 use Reflex::Callbacks qw(cb_coderef);
19 my $ct = Reflex::Timer->new(
20 interval => 1 + rand(),
21 auto_repeat => 1,
22 on_tick => cb_coderef( sub { eg_say("coderef callback triggered") } ),
23 );
24
25 ### Handle timer ticks with object methods. Class methods work, too.
26
27 {
28 package MethodHandler;
29 use Moose;
30 extends 'Reflex::Object';
31 use Reflex::Callbacks qw(cb_method);
32 use ExampleHelpers qw(eg_say);
33
34 has ticker => (
35 isa => 'Maybe[Reflex::Timer]',
36 is => 'rw',
37 );
38
39 sub BUILD {
40 my $self = shift;
41 $self->ticker(
42 Reflex::Timer->new(
43 interval => 1 + rand(),
44 auto_repeat => 1,
45 on_tick => cb_method($self, "callback"),
46 )
47 );
48 }
49
50 sub callback {
51 eg_say("method callback triggered");
52 }
53 }
54
55 my $mh = MethodHandler->new();
56
57 ### Handle timer ticks with objects.
58 ### This is a convenience wrapper for mapping several events to
59 ### methods in one go.
60
61 {
62 package ObjectHandler;
63 use Moose;
64 extends 'Reflex::Object';
65 use Reflex::Callbacks qw(cb_object);
66 use ExampleHelpers qw(eg_say);
67
68 has ticker => (
69 isa => 'Maybe[Reflex::Timer]',
70 is => 'rw',
71 );
72
73 sub BUILD {
74 my $self = shift;
75 $self->ticker(
76 Reflex::Timer->new(
77 interval => 1 + rand(),
78 auto_repeat => 1,
79 cb_object($self, { tick => "callback" }),
80 )
81 );
82 }
83
84 sub callback {
85 eg_say("object callback triggered");
86 }
87 }
88
89 my $oh = ObjectHandler->new();
90
91 ### Handle timer ticks with role-based method names.
92
93 {
94 package RoleHandler;
95 use Moose;
96 extends 'Reflex::Object';
97 use Reflex::Callbacks qw(cb_role);
98 use ExampleHelpers qw(eg_say);
99
100 has ticker => (
101 isa => 'Maybe[Reflex::Timer]',
102 is => 'rw',
103 );
104
105 sub BUIILD {
106 my $self = shift;
107 $self->ticker(
108 Reflex::Timer->new(
109 interval => 1 + rand(),
110 auto_repeat => 1,
111 cb_role($self, "timer"),
112 )
113 );
114 }
115
116 sub on_timer_tick {
117 eg_say("object callback triggered");
118 }
119 }
120
121 my $rh = RoleHandler->new();
122
123 ### Poll for events with a condvar-like promise construct. Goes last
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored Jun 16, 2010
124 ### because the while() loop will "block". Meanwhile, next() is also
6da75c7 @rcaputo Get all callbacks working in an approximation of a "good" way.
authored Apr 9, 2010
125 ### allowing the other timers to run.
126
127 my $pt = Reflex::Timer->new(
128 interval => 1 + rand(),
129 auto_repeat => 1,
130 );
131
5f9458c @rcaputo Rename observe() to watch(). Rename wait() to next().
authored Jun 16, 2010
132 while (my $event = $pt->next()) {
72d32d4 @rcaputo Bump the revision for release. Make little fixes here and there so al…
authored Apr 23, 2010
133 eg_say("promise timer returned an event ($event->{name})");
6da75c7 @rcaputo Get all callbacks working in an approximation of a "good" way.
authored Apr 9, 2010
134 }
Something went wrong with that request. Please try again.