Skip to content

Commit

Permalink
Implicit unsubscription works
Browse files Browse the repository at this point in the history
  • Loading branch information
pdcawley committed Oct 15, 2011
1 parent 8036807 commit 6cf8b58
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
30 changes: 18 additions & 12 deletions lib/Announcements/Subscription.pm
Expand Up @@ -16,9 +16,12 @@ has do => (
required => 1, required => 1,
); );


has for => ( has subscriber => (
is => 'ro', is => 'ro',
isa => 'Ref', isa => 'Ref',
init_arg => 'for',
predicate => 'has_subscriber',
weak_ref => 1
); );


has _registry => ( has _registry => (
Expand Down Expand Up @@ -51,20 +54,23 @@ sub BUILDARGS {
return $params; return $params;
} }




sub send { sub send {
my $self = shift; my $self = shift;
my $announcement = shift; my $announcement = shift;
my $announcer = shift; my $announcer = shift;


return unless $self->matches($announcement, $announcer); if ($self->has_subscriber && !$self->subscriber) {

$self->unsubscribe;
$self->do->( }
$announcement, else {
$announcer, return unless $self->matches($announcement, $announcer);
$self,
); $self->do->(
$announcement,
$announcer,
$self,
);
}
} }


sub matches { sub matches {
Expand Down
2 changes: 1 addition & 1 deletion lib/Announcements/SubscriptionRegistry.pm
Expand Up @@ -50,7 +50,7 @@ sub _subscriptions_for {


my @subs = @_; my @subs = @_;
$self->_foreach_subscription(sub { $self->_foreach_subscription(sub {
my $for = $_->for; my $for = $_->subscriber;
push @subs, $_ if $for && $for == $subscriber; push @subs, $_ if $for && $for == $subscriber;
}); });
return @subs; return @subs;
Expand Down
27 changes: 27 additions & 0 deletions t/unsubscribing.t
Expand Up @@ -189,6 +189,33 @@ subtest "unsubscription by subscriber" => sub {
is $count, 1; is $count, 1;
}; };


subtest "subscriptions disappear when a subscriber disappears" => sub {
my $button = PushedButton->new;

my $subscriber = {};

my $count = 0;

$button->add_subscription(
when => 'PushedButton',
do => sub { $count++ },
for => $subscriber,
);

$button->add_subscription(
when => 'PushedButton',
do => sub { $count++ },
);

$button->push;
is $count, 2;

$count = 0;
$subscriber = undef;
$button->push;
is $count, 1;
};



done_testing; done_testing;


Expand Down

0 comments on commit 6cf8b58

Please sign in to comment.