Created
September 29, 2013 18:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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