EntityModel

 view release on metacpan or  search on metacpan

lib/EntityModel/Storage/PerlAsync.pm  view on Meta::CPAN

package EntityModel::Storage::PerlAsync;
{
  $EntityModel::Storage::PerlAsync::VERSION = '0.102';
}
use EntityModel::Class {
	_isa		=> [qw{EntityModel::Storage::Perl}],
	loop		=> { type => 'IO::Async::Loop' }
};

=head1 NAME

EntityModel::Storage::PerlAsync - backend storage interface for L<EntityModel>

=head1 VERSION

version 0.102

=head1 SYNOPSIS

See L<EntityModel>.

=head1 DESCRIPTION

Wrapper around the Perl storage module to defer responses until idle point in an L<IO::Async> loop.

=cut

use Scalar::Util ();

=head1 METHODS

=cut

=head2 new

Subclassed instantiation, requires an L<IO::Async::Loop> passed as the C<loop> named parameter.

=cut

sub setup {
	my $self = shift;
	my %args = %{+shift};
	my $loop = delete $args{loop} or die "No IO::Async::Loop provided?";
	$self->SUPER::setup(\%args);
	Scalar::Util::weaken($self->{loop} = $loop);
	return $self;
}

=head2 read

Reads the data for the given entity and returns hashref with the appropriate data.

Parameters:

=over 4

=item * entity - L<EntityModel::Entity>

=item * id - ID to read data from

=back

Callbacks (included in parameter list above):

=over 4

=item * on_complete - called when the value has been read, includes the value

=item * on_not_found - called if entry not found

=back

Returns $self.

=cut

use Carp qw/cluck/;
sub read {
	my $self = shift;
	my %args = @_;
	return unless exists $args{on_complete};
	# cluck "Request for id " . ($args{id} // 'undef') . " from " . $args{entity}->name;

	my $complete = delete $args{on_complete};
	$self->loop->later($self->sap(sub {
		my $self = shift;
		my $v = $self->SUPER::read(%args);
		warn "Reading " . ($v // "undef") . "\n";
		$complete->($v);
	}));
	return $self;
}

=head2 create

Creates new entry for the given L<EntityModel::Entity>.

Parameters:

=over 4

=item * entity - L<EntityModel::Entity>

=item * data - actual data values



( run in 0.606 second using v1.01-cache-2.11-cpan-39bf76dae61 )