| 1 | package DBIx::Loop::Loop::Mojo; | ||
| 2 | |||
| 3 | use 5.008003; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp (); | ||
| 7 | |||
| 8 | our $VERSION = '0.09'; | ||
| 9 | |||
| 10 | # The loop adapter for Mojo::IOLoop, implementing the DBIx::Loop seam | ||
| 11 | # (add_reader / add_writer / remove / timer / new_future / await) over the | ||
| 12 | # Mojo reactor. to_native() bridges a DBIx::Loop::Future to a Mojo::Promise. | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my ($class, %opt) = @_; | ||
| 16 | require Mojo::IOLoop; | ||
| 17 | my $loop = $opt{loop} || Mojo::IOLoop->singleton; | ||
| 18 | return bless { loop => $loop, fh => {} }, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub loop { $_[0]{loop} } | ||
| 22 | |||
| 23 | # watch a raw fd (owned by C): dup it so the reactor can hold a handle; | ||
| 24 | # remove() closes only our dup, never C's fd. | ||
| 25 | sub _fh { | ||
| 26 | my ($self, $fd, $mode) = @_; | ||
| 27 | open my $fh, $mode, $fd or Carp::croak("DBIx::Loop: fdopen($fd): $!"); | ||
| 28 | return $fh; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub add_reader { | ||
| 32 | my ($self, $fd, $cb) = @_; | ||
| 33 | my $e = $self->{fh}{$fd} ||= { fh => $self->_fh($fd, '<&'), r => 0, w => 0 }; | ||
| 34 | $e->{r} = 1; | ||
| 35 | my $reactor = $self->{loop}->reactor; | ||
| 36 | $reactor->io($e->{fh} => sub { | ||
| 37 | my (undef, $writable) = @_; | ||
| 38 | $writable ? ($e->{wcb} && $e->{wcb}->()) : ($e->{rcb} && $e->{rcb}->()); | ||
| 39 | }) unless $e->{registered}++; | ||
| 40 | $e->{rcb} = $cb; | ||
| 41 | $reactor->watch($e->{fh}, $e->{r}, $e->{w}); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub add_writer { | ||
| 45 | my ($self, $fd, $cb) = @_; | ||
| 46 | my $e = $self->{fh}{$fd} ||= { fh => $self->_fh($fd, '>&'), r => 0, w => 0 }; | ||
| 47 | $e->{w} = 1; | ||
| 48 | my $reactor = $self->{loop}->reactor; | ||
| 49 | $reactor->io($e->{fh} => sub { | ||
| 50 | my (undef, $writable) = @_; | ||
| 51 | $writable ? ($e->{wcb} && $e->{wcb}->()) : ($e->{rcb} && $e->{rcb}->()); | ||
| 52 | }) unless $e->{registered}++; | ||
| 53 | $e->{wcb} = $cb; | ||
| 54 | $reactor->watch($e->{fh}, $e->{r}, $e->{w}); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub remove { | ||
| 58 | my ($self, $fd) = @_; | ||
| 59 | my $e = delete $self->{fh}{$fd} or return; | ||
| 60 | $self->{loop}->reactor->remove($e->{fh}); | ||
| 61 | close $e->{fh}; | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub timer { | ||
| 66 | my ($self, $after, $cb) = @_; | ||
| 67 | return $self->{loop}->timer($after => $cb); | ||
| 68 | } | ||
| 69 | |||
| 70 | # ecosystem-native pending future for this loop | ||
| 71 | sub new_future { | ||
| 72 | require Mojo::Promise; | ||
| 73 | return Mojo::Promise->new; | ||
| 74 | } | ||
| 75 | |||
| 76 | # bridge a DBIx::Loop::Future to a Mojo::Promise | ||
| 77 | sub to_native { | ||
| 78 | my ($self, $future) = @_; | ||
| 79 | require Mojo::Promise; | ||
| 80 | my $p = Mojo::Promise->new; | ||
| 81 | $future->on_ready(sub { | ||
| 82 | my ($f) = @_; | ||
| 83 | $f->is_done ? $p->resolve($f->get) : $p->reject($f->failure); | ||
| 84 | }); | ||
| 85 | return $p; | ||
| 86 | } | ||
| 87 | |||
| 88 | # run the loop until $future (a DBIx::Loop::Future) is ready | ||
| 89 | sub await { | ||
| 90 | my ($self, $future) = @_; | ||
| 91 | $self->{loop}->one_tick until $future->is_ready; | ||
| 92 | return $future; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | __END__ |