| 1 | package Plack::Middleware::Refresh; | ||
| 2 | use strict; | ||
| 3 | use parent qw(Plack::Middleware); | ||
| 4 | use Module::Refresh; | ||
| 5 | use Plack::Util::Accessor qw(last cooldown); | ||
| 6 | |||
| 7 | sub prepare_app { | ||
| 8 | my $self = shift; | ||
| 9 | $self->cooldown(10) unless defined $self->cooldown; | ||
| 10 | |||
| 11 | Module::Refresh->new; | ||
| 12 | $self->last(time - $self->cooldown); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub call { | ||
| 16 | my($self, $env) = @_; | ||
| 17 | |||
| 18 | if (time > $self->last + $self->cooldown) { | ||
| 19 | Module::Refresh->refresh; | ||
| 20 | $self->last(time); | ||
| 21 | } | ||
| 22 | |||
| 23 | $self->app->($env); | ||
| 24 | } | ||
| 25 | |||
| 26 | 1; | ||
| 27 | |||
| 28 | __END__ |