Skip to content

Commit

Permalink
A subscription can now "do_once"
Browse files Browse the repository at this point in the history
  • Loading branch information
pdcawley committed Nov 4, 2011
1 parent e7bfd91 commit c396108
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/Announcements/Subscription.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ has _registry => (
}
);

sub BUILDARGS {
my $class = shift;
my $params = $class->SUPER::BUILDARGS(@_);

if (my $oneshot = delete $params->{do_once}) {
$params->{do} = sub {
$_[2]->unsubscribe;
$oneshot->(@_);
};
}
return $params;
}



sub send {
my $self = shift;
Expand Down
17 changes: 17 additions & 0 deletions t/unsubscribing.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ subtest "Basic unsubscription" => sub {
is $announcement_count, 1;
};

subtest "Basic unsubscription" => sub {
my $nuke = PushedButton->new;
my $announcement_count = 0;

ok $nuke->add_subscription(
when => 'PushedButton',
do_once => sub {
$announcement_count++;
}
);

$nuke->push;
$nuke->push;

is $announcement_count, 1;
};

subtest "Double subscription is wrong, m'kay?" => sub {
my $nuke = PushedButton->new;
my $announcement_count = 0;
Expand Down

0 comments on commit c396108

Please sign in to comment.