Skip to content

Commit

Permalink
Merge pull request #12 from hubie/master
Browse files Browse the repository at this point in the history
Add an option for immediate state transitions in POE::NFA.
  • Loading branch information
rcaputo committed Apr 29, 2012
2 parents 3444b16 + 71b1647 commit 88d27d6
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions lib/POE/NFA.pm
Expand Up @@ -16,6 +16,7 @@ sub SPAWN_RUNSTATE () { 'runstate' }
sub OPT_TRACE () { 'trace' } sub OPT_TRACE () { 'trace' }
sub OPT_DEBUG () { 'debug' } sub OPT_DEBUG () { 'debug' }
sub OPT_DEFAULT () { 'default' } sub OPT_DEFAULT () { 'default' }
sub OPT_IMMEDIATE () { 'immediate' }


sub EN_DEFAULT () { '_default' } sub EN_DEFAULT () { '_default' }
sub EN_START () { '_start' } sub EN_START () { '_start' }
Expand Down Expand Up @@ -745,8 +746,7 @@ sub callback {


sub goto_state { sub goto_state {
my ($self, $new_state, $entry_event, @entry_args) = @_; my ($self, $new_state, $entry_event, @entry_args) = @_;

if (defined $self->[SELF_CURRENT] && !$self->[SELF_OPTIONS]->{+OPT_IMMEDIATE}) {
if (defined $self->[SELF_CURRENT]) {
$POE::Kernel::poe_kernel->post( $POE::Kernel::poe_kernel->post(
$self, NFA_EN_GOTO_STATE, $self, NFA_EN_GOTO_STATE,
$new_state, $entry_event, @entry_args $new_state, $entry_event, @entry_args
Expand All @@ -767,16 +767,30 @@ sub stop {


sub call_state { sub call_state {
my ($self, $return_event, $new_state, $entry_event, @entry_args) = @_; my ($self, $return_event, $new_state, $entry_event, @entry_args) = @_;
$POE::Kernel::poe_kernel->post( if ($self->[SELF_OPTIONS]->{+OPT_IMMEDIATE}) {
$self, NFA_EN_PUSH_STATE, $POE::Kernel::poe_kernel->call(
$return_event, $self, NFA_EN_PUSH_STATE,
$new_state, $entry_event, @entry_args $return_event,
); $new_state, $entry_event, @entry_args
);
}
else {
$POE::Kernel::poe_kernel->post(
$self, NFA_EN_PUSH_STATE,
$return_event,
$new_state, $entry_event, @entry_args
);
}
} }


sub return_state { sub return_state {
my ($self, @entry_args) = @_; my ($self, @entry_args) = @_;
$POE::Kernel::poe_kernel->post( $self, NFA_EN_POP_STATE, @entry_args ); if ($self->[SELF_OPTIONS]->{+OPT_IMMEDIATE}) {
$POE::Kernel::poe_kernel->call( $self, NFA_EN_POP_STATE, @entry_args );
}
else {
$POE::Kernel::poe_kernel->post( $self, NFA_EN_POP_STATE, @entry_args );
}
} }


1; 1;
Expand Down Expand Up @@ -951,10 +965,15 @@ machine is in C<state_2>, method C<method_1> will be called on $object_2.
C<package_states> is very similar, but instead of using an $object, you C<package_states> is very similar, but instead of using an $object, you
pass in a C<Package::Name> pass in a C<Package::Name>
The C<runstate> parameter allows C<RUNSTATE> to be initialized differently The C<runstate> parameter allows C<RUNSTATE> to be initialized differently
at instantiation time. C<RUNSTATE>, like heaps, are usually anonymous hashrefs, at instantiation time. C<RUNSTATE>, like heaps, are usually anonymous hashrefs,
but C<runstate> may set them to be array references or even objects. but C<runstate> may set them to be array references or even objects.
State transitions are not necessarily executed immediately by default. Rather,
they are placed in POEs event queue behind any currently pending events.
Enabling the C<immediate> option causes state transitions to occur immediately,
regardless of any queued events.
=head2 goto_state NEW_STATE[, ENTRY_EVENT[, EVENT_ARGS]] =head2 goto_state NEW_STATE[, ENTRY_EVENT[, EVENT_ARGS]]
goto_state() puts the machine into a new state. If an ENTRY_EVENT is goto_state() puts the machine into a new state. If an ENTRY_EVENT is
Expand Down

0 comments on commit 88d27d6

Please sign in to comment.