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 134 lines (107 sloc) 2.625 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#!/usr/bin/env perl

# This is pretty close to the final syntax.
# TODO - Provide a way to next() on multiple objects at once.
# ...... maybe by next() on a collection?
# TODO - Clean out all previous promise-like examples.

use warnings;
use strict;

use lib qw(../lib);

use Reflex::Interval;
use ExampleHelpers qw(eg_say);

### Handle timer ticks with coderefs.

use Reflex::Callbacks qw(cb_coderef);
my $ct = Reflex::Interval->new(
interval => 1 + rand(),
auto_repeat => 1,
on_tick => cb_coderef( sub { eg_say("coderef callback triggered") } ),
);

### Handle timer ticks with object methods. Class methods work, too.

{
package MethodHandler;
use Moose;
extends 'Reflex::Base';
use Reflex::Callbacks qw(cb_method);
use ExampleHelpers qw(eg_say);

has ticker => (
isa => 'Maybe[Reflex::Interval]',
is => 'rw',
);

sub BUILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1 + rand(),
auto_repeat => 1,
on_tick => cb_method($self, "callback"),
)
);
}

sub callback {
eg_say("method callback triggered");
}
}

my $mh = MethodHandler->new();

### Handle timer ticks with objects.
### This is a convenience wrapper for mapping several events to
### methods in one go.

{
package ObjectHandler;
use Moose;
extends 'Reflex::Base';
use Reflex::Callbacks qw(cb_object);
use ExampleHelpers qw(eg_say);

has ticker => (
isa => 'Maybe[Reflex::Interval]',
is => 'rw',
);

sub BUILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1 + rand(),
auto_repeat => 1,
cb_object($self, { tick => "callback" }),
)
);
}

sub callback {
eg_say("object callback triggered");
}
}

my $oh = ObjectHandler->new();

### Handle timer ticks with role-based method names.

{
package RoleHandler;
use Moose;
extends 'Reflex::Base';
use Reflex::Callbacks qw(cb_role);
use ExampleHelpers qw(eg_say);

has ticker => (
isa => 'Maybe[Reflex::Interval]',
is => 'rw',
);

sub BUIILD {
my $self = shift;
$self->ticker(
Reflex::Interval->new(
interval => 1 + rand(),
auto_repeat => 1,
cb_role($self, "timer"),
)
);
}

sub on_timer_tick {
eg_say("object callback triggered");
}
}

my $rh = RoleHandler->new();

### Poll for events with promises. Goes last because the while() loop
### will "block". Meanwhile, next() is also allowing the other timers
### to run.

my $pt = Reflex::Interval->new(
interval => 1 + rand(),
auto_repeat => 1,
);

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