AnyEvent-Blackboard

 view release on metacpan or  search on metacpan

lib/AnyEvent/Blackboard.pm  view on Meta::CPAN

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
=cut
 
=back
 
=head1 METHODS
 
=over 4
 
=item timeout SECONDS, [ KEY, [, DEFAULT ] ]
 
Set a timer for N seconds to provide "default" value as a value, defaults to
`undef`.  This can be used to ensure that blackboard workflows do not reach a
dead-end if a required value is difficult to obtain.
 
=cut
 
sub timeout {
    my ($self, $seconds, $key, $default) = @_;
 
    $key = [ $key ] unless (ref $key eq "ARRAY");
 
    unless ($self->has($key)) {
        my $guard = AnyEvent->timer(
            after => $seconds,
            cb    => sub {
                unless ($self->has($key)) {
                    $self->put($_ => $default) for @$key;
                }
            }
        );
 
        # Cancel the timer if we find the object first (otherwise this is a NOOP).
        $self->_watch($key, sub { undef $guard });
    }
}
 
=item watch KEYS, WATCHER [, KEYS, WATCHER ]
 
=item watch KEY, WATCHER [, KEYS, WATCHER ]
 
Overrides L<Async::Blackboard> only for the purpose of adding a timeout.



( run in 0.520 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )