summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2012-03-10 20:37:20 +0100
committerYorhel <git@yorhel.nl>2012-03-10 20:37:20 +0100
commit09a97845da05f1408392db1759eeb266f80186bc (patch)
tree4f068b387c3b11b2c18d7a076708ba471944f895
parent9167da8e7ed487240fdfd5d1791b0cdc3a8f6dab (diff)
Use ref-counted variable to detect whether pattern is unregistered
A bit more reliable than using an ID that may be re-used.
-rw-r--r--Tanja.pm10
1 files changed, 6 insertions, 4 deletions
diff --git a/Tanja.pm b/Tanja.pm
index 92e2b82..a0873f8 100644
--- a/Tanja.pm
+++ b/Tanja.pm
@@ -60,7 +60,7 @@ sub send {
my $ret = Tanja::ReturnPath->_new($cb);
for my $reg (grep Tanja::match($_->[0], $t), values %{$s->{pat}}) {
AnyEvent::postpone {
- $reg->[1]->($t, $ret, $sa);
+ $reg->[1]->($t, $ret, $sa, \$reg->[2]);
}
}
}
@@ -71,7 +71,7 @@ sub _register {
my $id = $s->{lastid};
# Explicitely wrap around long before 2^31, to avoid having Perl turn the ID into a float.
do { ++$id >= 1<<30 and $id = 1 } while $s->{pat}{$id};
- $s->{pat}{$id} = [ $pat, $cb ];
+ $s->{pat}{$id} = [ $pat, $cb, 1 ];
$s->{lastid} = $id;
$_->_srv_reg($id, $pat, $cb) for(values %{$s->{lnk}});
return $id;
@@ -80,7 +80,9 @@ sub _register {
sub _unregister {
my($s, $id) = @_;
- if(delete $s->{pat}{$id}) {
+ if($s->{pat}{$id}) {
+ $s->{pat}{$id}[2] = 0;
+ delete $s->{pat}{$id};
$_->_srv_unreg($id) for(values %{$s->{lnk}});
}
}
@@ -110,7 +112,7 @@ sub new {
sub reg {
my($s, $pat, $cb) = @_;
my $id;
- $id = $s->{node}->_register($pat, sub { $s->{pat}{$id} && $cb->(@_) });
+ $id = $s->{node}->_register($pat, sub { ${$_[3]} && $cb->($_[0], $_[1]) });
$s->{pat}{$id} = 1;
return $id;
}