Note to dngor, do not apply this, it needs much more work!

Following is a patch that lets you add hooks around invoke_state, I had to chang quite 
alot from the code I originally had.

I didn't have time rewriting a proper API for it, that is coming by tuesday :), but I 
wanted this out so it can be discussed.

Right now the only way to add a hook is to push it on 
POE::Kernel::pre|post_invoke_state_hooks, if a pre_invoke_state_hook returns true it 
is assumed that one should not call POEs invoke state and instead take the return 
value and put it in $return. 

Additions to be made are a way to add and remove hooks, a way to filter hooks on 
events and possibly on sessions. Do people want sessions? Session ids? ???

The patch modifies samples/create, it is a simple hook that prints all states!

Artur

Index: POE/Kernel.pm
===================================================================
RCS file: /cvsroot/poe/poe/POE/Kernel.pm,v
retrieving revision 1.136
diff -r1.136 Kernel.pm
11a12,15
> use vars qw($hook_session $hook_source_session $hook_local_state $hook_etc 
>$hook_file $hook_line);
> use vars qw(@pre_invoke_state_hooks @post_invoke_state_hooks);
> 
> 
920a925,927
> 
> 
> 
922,923c929,963
<   my $return =
<     $session->_invoke_state($source_session, $local_state, $etc, $file, $line);
---
> 
>   my $return;
> 
> 
>   DISPATCH: {
>       if(@pre_invoke_state_hooks) {
>         ($hook_session, $hook_source_session,$hook_local_state, $hook_etc, 
>$hook_file, $hook_line) = 
>         (     $session,      $source_session,     $local_state,      $etc,      
>$file,      $line);
> 
>         foreach (@pre_invoke_state_hooks) {
>             my $retval = $_->();
>             if($retval) {
>                 $return = $retval;
>                 ($hook_session, $hook_source_session, $hook_local_state, $hook_etc, 
>$hook_file, $hook_line) = 
>                     (undef, undef, undef, undef, undef, undef);
>                 last DISPATCH;
>             }
>         }
>       }
> 
>       $return = $session->_invoke_state($source_session, $local_state, $etc, $file, 
>$line);
>       
>       if(@post_invoke_state_hooks) {
>         ($hook_session, $hook_source_session,$hook_local_state, $hook_etc, 
>$hook_file, $hook_line) = 
>         (     $session,      $source_session,     $local_state,      $etc,      
>$file,      $line);
>         foreach(@post_invoke_state_hooks) {
>             $_->();
>         }
>       }
>       ($hook_session, $hook_source_session, $hook_local_state, $hook_etc, 
>$hook_file, $hook_line) = 
>         (undef, undef, undef, undef, undef, undef);
>   }
> 
> 
> 
Index: samples/create.perl
===================================================================
RCS file: /cvsroot/poe/poe/samples/create.perl,v
retrieving revision 1.6
diff -r1.6 create.perl
17a18,20
> 
> push @POE::Kernel::pre_invoke_state_hooks, sub { print 
>"$POE::Kernel::hook_local_state called from 
>$POE::Kernel::hook_file:$POE::Kernel::hook_line\n"; return 0};
> 





Reply via email to