Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implicit unsubscription works

  • Loading branch information...
commit 6cf8b587bd3d701a965e79bfe9ceee86515ce28a 1 parent 8036807
@pdcawley authored
View
30 lib/Announcements/Subscription.pm
@@ -16,9 +16,12 @@ has do => (
required => 1,
);
-has for => (
- is => 'ro',
- isa => 'Ref',
+has subscriber => (
+ is => 'ro',
+ isa => 'Ref',
+ init_arg => 'for',
+ predicate => 'has_subscriber',
+ weak_ref => 1
);
has _registry => (
@@ -51,20 +54,23 @@ sub BUILDARGS {
return $params;
}
-
-
sub send {
my $self = shift;
my $announcement = shift;
my $announcer = shift;
- return unless $self->matches($announcement, $announcer);
-
- $self->do->(
- $announcement,
- $announcer,
- $self,
- );
+ if ($self->has_subscriber && !$self->subscriber) {
+ $self->unsubscribe;
+ }
+ else {
+ return unless $self->matches($announcement, $announcer);
+
+ $self->do->(
+ $announcement,
+ $announcer,
+ $self,
+ );
+ }
}
sub matches {
View
2  lib/Announcements/SubscriptionRegistry.pm
@@ -50,7 +50,7 @@ sub _subscriptions_for {
my @subs = @_;
$self->_foreach_subscription(sub {
- my $for = $_->for;
+ my $for = $_->subscriber;
push @subs, $_ if $for && $for == $subscriber;
});
return @subs;
View
27 t/unsubscribing.t
@@ -189,6 +189,33 @@ subtest "unsubscription by subscriber" => sub {
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;
Please sign in to comment.
Something went wrong with that request. Please try again.