summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-02-16 17:03:03 +0100
committerYorhel <git@yorhel.nl>2012-02-16 17:03:03 +0100
commitb689af6c792a4e9a243d6260a2718f49e98b9411 (patch)
treefe82551e57b860208b9f8b587072431e5e3a8668
parent5be2f049b3f0e57c36184070dc348312c81d437e (diff)
Use AnyEvent::postpone() to make tuple dispatch asynchronous
-rw-r--r--Tanja.pm14
-rwxr-xr-xtest.pl5
2 files changed, 12 insertions, 7 deletions
diff --git a/Tanja.pm b/Tanja.pm
index 6827cd8..1062cda 100644
--- a/Tanja.pm
+++ b/Tanja.pm
@@ -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;
}
diff --git a/test.pl b/test.pl
index 870435c..a5aea3c 100755
--- a/test.pl
+++ b/test.pl
@@ -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;
}