Permalink
Browse files

Merge pull request #12 from hubie/master

Add an option for immediate state transitions in POE::NFA.
  • Loading branch information...
2 parents 3444b16 + 71b1647 commit 88d27d68b46de035a21f3e20b68700804f941cf0 @rcaputo committed Apr 29, 2012
Showing with 29 additions and 10 deletions.
  1. +29 −10 lib/POE/NFA.pm
View
@@ -16,6 +16,7 @@ sub SPAWN_RUNSTATE () { 'runstate' }
sub OPT_TRACE () { 'trace' }
sub OPT_DEBUG () { 'debug' }
sub OPT_DEFAULT () { 'default' }
+sub OPT_IMMEDIATE () { 'immediate' }
sub EN_DEFAULT () { '_default' }
sub EN_START () { '_start' }
@@ -745,8 +746,7 @@ sub callback {
sub goto_state {
my ($self, $new_state, $entry_event, @entry_args) = @_;
-
- if (defined $self->[SELF_CURRENT]) {
+ if (defined $self->[SELF_CURRENT] && !$self->[SELF_OPTIONS]->{+OPT_IMMEDIATE}) {
$POE::Kernel::poe_kernel->post(
$self, NFA_EN_GOTO_STATE,
$new_state, $entry_event, @entry_args
@@ -767,16 +767,30 @@ sub stop {
sub call_state {
my ($self, $return_event, $new_state, $entry_event, @entry_args) = @_;
- $POE::Kernel::poe_kernel->post(
- $self, NFA_EN_PUSH_STATE,
- $return_event,
- $new_state, $entry_event, @entry_args
- );
+ if ($self->[SELF_OPTIONS]->{+OPT_IMMEDIATE}) {
+ $POE::Kernel::poe_kernel->call(
+ $self, NFA_EN_PUSH_STATE,
+ $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 {
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;
@@ -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
pass in a C<Package::Name>
-The C<runstate> parameter allows C<RUNSTATE> to be initialized differently
-at instantiation time. C<RUNSTATE>, like heaps, are usually anonymous hashrefs,
+The C<runstate> parameter allows C<RUNSTATE> to be initialized differently
+at instantiation time. C<RUNSTATE>, like heaps, are usually anonymous hashrefs,
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]]
goto_state() puts the machine into a new state. If an ENTRY_EVENT is

0 comments on commit 88d27d6

Please sign in to comment.