package Tak::ModuleLoader;

use Moo;

has remote => (is => 'ro', required => 1);

sub inc_callback {
  my ($self) = @_;
  sub { return if our $In_Reentry; $self->maybe_load_module($_[1]) }
}

sub maybe_load_module {
  my ($self, $module) = @_;
  local our $In_Reentry = 1;
  my ($status, $code) = $self->remote->blocking_request(source_for => $module);
  if ($status eq 'RESULT') {
    open my $fh, '<', \$code;
    return $fh;
  }
  return;
}

1;
