Jifty
view release on metacpan or search on metacpan
lib/Jifty/Plugin/PubSub/Subscriptions.pm view on Meta::CPAN
=head1 METHODS
=cut
# This is _new rather than new because it is a singleton
sub _new {
my $class = shift;
my $env = shift;
my $self = bless {
store => {},
client_id => undef,
}, $class;
return $self;
}
=head2 add topic => I<TOPIC> [, ...]
Adds a subscription. If only the I<TOPIC> is given, the event will be
passed through to the web browser to interpret. Otherwise, the
arguments are used similarly to L<Jifty::Web::Element> to determine
which region to update, and how.
=cut
sub add {
my $self = shift;
my %args = (
topic => undef,
region => undef,
path => undef,
arguments => undef,
mode => undef,
element => undef,
effect => undef,
effect_args => undef,
remove_effect => undef,
remove_effect_args => undef,
@_
);
$self->{client_id} ||= "jifty_" . Jifty->web->serial;
delete $args{$_} for grep {not defined $args{$_}} keys %args;
$args{attrs}{$_} = delete $args{$_}
for grep {defined $args{$_}}
qw/ effect effect_args
remove_effect remove_effect_args/;
push @{$self->{store}{$self->{client_id}}}, \%args;
}
=head2 update_on topic => I<TOPIC> [, ...]
As L</add>, but defaults to refreshing the current region.
=cut
sub update_on {
my $self = shift;
my $region = Jifty->web->current_region;
unless ($region) {
warn "Jifty->subs->update_on called when not in a region";
return;
}
my %args = %{ $region->arguments };
delete $args{region};
delete $args{event};
$self->add(
arguments => \%args,
mode => 'Replace',
region => $region->qualified_name,
path => $region->path,
@_,
);
}
=head2 client_id
Returns the assigned I<CLIENT_ID> of the current connection. This is
C<undef> if the client has not been assigned any subscriptions yet.
=cut
sub client_id {
my $self = shift;
return $self->{client_id};
}
=head2 reset
Called internally once per request to reset for the next request.
=cut
sub reset {
my $self = shift;
$self->{client_id} = undef;
}
=head2 retrieve I<CLIENT_ID>
Returns the data structure of subscriptions for the given I<CLIENT_ID>,
and removes it such that it is not accessible to future requests.
=cut
sub retrieve {
my $self = shift;
my $client_id = shift;
return delete $self->{store}{$client_id} || [];
}
1;
( run in 0.748 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )