Skip to content

Instantly share code, notes, and snippets.

@jberger
Created September 29, 2013 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jberger/6755113 to your computer and use it in GitHub Desktop.
Save jberger/6755113 to your computer and use it in GitHub Desktop.
A scope-guarded proxy-object for Mojo::IOLoop which starts the loop when it goes out of scope.
package LoopGuard;
use Mojo::Base -base;
use Mojo::IOLoop;
use Mojo::Util 'monkey_patch';
has ioloop => sub { Mojo::IOLoop->singleton };
sub import {
my $caller = caller;
monkey_patch $caller, i => sub { __PACKAGE__->new };
}
sub AUTOLOAD {
my $method = (split /::/, our $AUTOLOAD )[-1];
shift->ioloop->$method(@_);
}
sub DESTROY {
my $loop = shift->ioloop;
$loop->start unless $loop->is_running;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment