Jifty/Plugin/PubSub/Subscriptions.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 58 CC 2 SCAR 10.6
CC
2
Cov
0%
update_on
6.0
_new
2.0
add
2.0
CRAP 2.9
1 use strict;
2 use warnings;
3  
4 package Jifty::Plugin::PubSub::Subscriptions;
5  
6 =head1 NAME
7  
8 Jifty::Plugin::PubSub::Subscriptions - Manage browser event subscriptions
9  
10 =head1 DESCRIPTION
11  
12 This class is a global cache of the outstanding subscriptions of
13 requests. When a page is rendered, it may choose to add subscriptions
14 via L</update_on> or L</add>:
15  
16 # Update the current region on an event
17 Jifty->subs->update_on( topic => "some_event" );
18  
19 or:
20  
21 # Send this topic of events to the browser
22 Jifty->subs->add( topic => "some_event" );
23  
24 These subscriptions are not done in the I<rendering> request, but must
25 be stored until the websocket connection occurs later; this class
26 manages that storage.
27  
28 The storage is currently an I<in-memory> store which I<does not purge
29 old subscriptions>. This means that if a page with subscriptions is
30 requested 1000 times, but the websocket for them is never established,
31 those subscriptions will be stored until the server is restarted. In
32 the future, these subscriptions may be stored on the session, and
33 expired in conjunction.
34  
35 The only expected interaction with this module is via L</update_on> and
36 L</add>.
37  
38 =head1 METHODS
39  
40 =cut
41  
42 # This is _new rather than new because it is a singleton
43 sub _new {
44 my $class = shift;
45 my $env = shift;
46  
47 my $self = bless {
48 store => {},
49 client_id => undef,
50 }, $class;
51 return $self;
52 }
53  
54 =head2 add topic => I<TOPIC> [, ...]
55  
56 Adds a subscription. If only the I<TOPIC> is given, the event will be
57 passed through to the web browser to interpret. Otherwise, the
58 arguments are used similarly to L<Jifty::Web::Element> to determine
59 which region to update, and how.
60  
61 =cut
62  
63 sub add {
64 my $self = shift;
65 my %args = (
66 topic => undef,
67 region => undef,
68 path => undef,
69 arguments => undef,
70 mode => undef,
71 element => undef,
72 effect => undef,
73 effect_args => undef,
74 remove_effect => undef,
75 remove_effect_args => undef,
76 @_
77 );
78  
79 $self->{client_id} ||= "jifty_" . Jifty->web->serial;
80  
81 delete $args{$_} for grep {not defined $args{$_}} keys %args;
82  
83 $args{attrs}{$_} = delete $args{$_}
84 for grep {defined $args{$_}}
85 qw/ effect effect_args
86 remove_effect remove_effect_args/;
87  
88 push @{$self->{store}{$self->{client_id}}}, \%args;
89 }
90  
91 =head2 update_on topic => I<TOPIC> [, ...]
92  
93 As L</add>, but defaults to refreshing the current region.
94  
95 =cut
96  
97 sub update_on {
98 my $self = shift;
99 my $region = Jifty->web->current_region;
100 unless ($region) {
101 warn "Jifty->subs->update_on called when not in a region";
102 return;
103 }
104  
105 my %args = %{ $region->arguments };
106 delete $args{region};
107 delete $args{event};
108 $self->add(
109 arguments => \%args,
110 mode => 'Replace',
111 region => $region->qualified_name,
112 path => $region->path,
113 @_,
114 );
115 }
116  
117 =head2 client_id
118  
119 Returns the assigned I<CLIENT_ID> of the current connection. This is
120 C<undef> if the client has not been assigned any subscriptions yet.
121  
122 =cut
123  
124 sub client_id {
125 my $self = shift;
126 return $self->{client_id};
127 }
128  
129 =head2 reset
130  
131 Called internally once per request to reset for the next request.
132  
133 =cut
134  
135 sub reset {
136 my $self = shift;
137 $self->{client_id} = undef;
138 }
139  
140 =head2 retrieve I<CLIENT_ID>
141  
142 Returns the data structure of subscriptions for the given I<CLIENT_ID>,
143 and removes it such that it is not accessible to future requests.
144  
145 =cut
146  
147 sub retrieve {
148 my $self = shift;
149 my $client_id = shift;
150 return delete $self->{store}{$client_id} || [];
151 }
152  
153 1;