diff options
author | Yorhel <git@yorhel.nl> | 2012-02-16 17:03:03 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2012-02-16 17:03:03 +0100 |
commit | b689af6c792a4e9a243d6260a2718f49e98b9411 (patch) | |
tree | fe82551e57b860208b9f8b587072431e5e3a8668 | |
parent | 5be2f049b3f0e57c36184070dc348312c81d437e (diff) |
Use AnyEvent::postpone() to make tuple dispatch asynchronous
-rw-r--r-- | Tanja.pm | 14 | ||||
-rwxr-xr-x | test.pl | 5 |
2 files changed, 12 insertions, 7 deletions
@@ -1,8 +1,3 @@ -# Note: this implementation isn't really asynchronous for communication between -# local sessions. Not sure yet whether this is a problem. (Can be fixed easily -# by using single-fire idle events to dispatch tuples. Comes at a performance -# cost, though). - package Tanja; use strict; @@ -56,7 +51,11 @@ sub link { sub send { my($s, $t, $cb) = @_; # TODO: fix return path - $_->[2]->($t, undef) for(grep Tanja::match($_->[0], $t), values %{$s->{pat}}); + for my $reg (grep Tanja::match($_->[0], $t), values %{$s->{pat}}) { + AnyEvent::postpone { + $reg->[2]->($t, undef); + } + } } @@ -102,7 +101,8 @@ sub new { # Returns: (ignored) sub reg { my($s, $pat, $reply, $cb) = @_; - my $id = $s->{server}->_register($pat, $reply, $cb); + my $id; + $id = $s->{server}->_register($pat, $reply, sub { $s->{pat}{$id} && $cb->(@_) }); $s->{pat}{$id} = 1; return $id; } @@ -29,6 +29,7 @@ ok !Tanja::match([2], [1]); isa_ok $ses, 'Tanja::Session'; my $done = AnyEvent->condvar; my $n = 0; + my $n2 = 0; $ses->reg([], 0, sub { my($t, $r) = @_; is $r, undef; @@ -43,10 +44,14 @@ ok !Tanja::match([2], [1]); my($t, $r) = @_; is $r, undef; is_deeply $t, [0]; + $n2++; }); $ses->send([$_]) for (0..10); + is $n, 0; # Make sure that ->send() doesn't run the callbacks. The event system should. + is $n2, 0; $done->recv; is $n, 5; + is $n2, 1; } |