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