diff options
Diffstat (limited to 'lib/Thread/Task/Role')
-rw-r--r-- | lib/Thread/Task/Role/Task.pm | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/Thread/Task/Role/Task.pm b/lib/Thread/Task/Role/Task.pm new file mode 100644 index 0000000..61f9cbe --- /dev/null +++ b/lib/Thread/Task/Role/Task.pm @@ -0,0 +1,54 @@ +use 5.008003; +use MooseX::Declare; + +role Thread::Task::Role::Task { + use MooseX::Types::Moose qw(Int); + require Scalar::Util; + + my $SEQUENCE = 0; + my %INDEX = 0; + + has task_revision => ( + isa => Int, + is => 'ro', + lazy_build => 1, + predicate => '_has_taskrev', + writer => '_set_taskrev', + ); + + method _build_task_revision() { + my $rev = ++$SEQUENCE; + $INDEX{$rev} = $self; + Scalar::Util::weaken($INDEX{$rev}); + + return $rev; + } + + method task_reset() { + if ($self->_has_taskrev) { + delete $INDEX{$self->task_revision}; + } + $self->_set_taskrev(++$SEQUENCE); + return; + } + + method task_owner($invocant: Int $task_rev) { + return $INDEX{$task_rev}; + } + + method task_request(:$task, %params) { + Class::MOP::load_class($task); + $task->isa('Thread::Task') + or die "Bad class $task, not a Thread::Task"; + $class->new(owner=>$self,%params)->schedule; + } + + method task_response(Task_T $task) { + my $class = $self->meta->name; + my $task_class = $task->meta->name; + + die "Unhandled task_response for $class (recieved $task_class)"; + } +} + +1; |