1 | #! perl # mandatory depends=login |
1 | #! perl # mandatory depends=login |
2 | |
2 | |
3 | # sends the following ext message types |
3 | # sends the following ext message types |
4 | # ws_n id # widgetset new |
4 | # ws_n ws # widgetset new |
5 | # ws_d id # widgetset destroy |
5 | # ws_d ws # widgetset destroy |
6 | # ws_c ws id class args # widgetset create |
6 | # ws_c ws id class args # widgetset create |
7 | # w_c id [rid] name args # widget method call |
7 | # w_c id rid name args # widget method call |
8 | # w_s id @attr # widget member set |
8 | # w_s id @attr # widget member set |
9 | # w_g id rid @attr # widget member get |
9 | # w_g id rid @attr # widget member get |
10 | # |
10 | # |
11 | # and expects the following exti message types |
11 | # and expects the following exti message types |
12 | # w_r rid res # widget call return |
12 | # w_r rid res # widget call return |
13 | # w_e id name args # widget_event |
13 | # w_e id name args # widget_event |
14 | |
14 | |
|
|
15 | our $DEBUG = 1; |
|
|
16 | |
15 | cf::client->attach ( |
17 | cf::client->attach ( |
16 | on_connect => sub { |
18 | on_connect => sub { |
17 | my ($ns) = @_; |
19 | my ($ns) = @_; |
18 | |
20 | |
19 | Scalar::Util::weaken (my $weakns = $ns); |
21 | Scalar::Util::weaken (my $weakns = $ns); |
20 | |
22 | |
21 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
22 | $ns->{json_coder}->filter_json_single_key_object (__widget_ref__ => sub { |
24 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
23 | # cannot deserialise ATM |
25 | # cannot deserialise ATM |
24 | undef |
26 | undef |
25 | }); |
27 | }); |
26 | }, |
28 | }, |
27 | ); |
29 | ); |
… | |
… | |
95 | #csc_start $ns; |
97 | #csc_start $ns; |
96 | }, |
98 | }, |
97 | ); |
99 | ); |
98 | |
100 | |
99 | cf::register_exticmd w_e => sub { |
101 | cf::register_exticmd w_e => sub { |
100 | my ($ns, $pkt) = @_; |
102 | my ($ns, $id, $name, $args) = @_; |
101 | |
103 | |
102 | if (my $w = $ns->{widget}{$pkt->{id}}) { |
104 | if (my $w = $ns->{widget}{$id}) { |
103 | if (my $cb = $w->{ev}{$pkt->{name}}) { |
105 | if (my $cb = $w->{ev}{$name}) { |
104 | $_->($w, @{ $pkt->{args} || [] }) |
106 | $_->($w, @$args) |
105 | for @$cb; |
107 | for @$cb; |
106 | } |
108 | } |
107 | } |
109 | } |
108 | |
110 | |
109 | () |
111 | () |
110 | }; |
112 | }; |
111 | |
113 | |
112 | cf::register_exticmd w_r => sub { |
114 | cf::register_exticmd w_r => sub { |
113 | my ($ns, $pkt) = @_; |
115 | my ($ns, $rid, $res) = @_; |
114 | |
116 | |
115 | if (my $cb = delete $ns->{widget_return}{$pkt->{rid}}) { |
117 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
116 | $cb->(@{$pkt->{res} || [] }); |
118 | $cb->(@$res); |
117 | } |
119 | } |
118 | |
120 | |
119 | () |
121 | () |
120 | }; |
122 | }; |
121 | |
123 | |
… | |
… | |
125 | my $id = ++$self->{id}; |
127 | my $id = ++$self->{id}; |
126 | |
128 | |
127 | my $ws = bless { |
129 | my $ws = bless { |
128 | id => $id, |
130 | id => $id, |
129 | ns => $self, |
131 | ns => $self, |
130 | w => {}, |
132 | _w => {}, |
131 | }, "ext::widget::set"; |
133 | }, "ext::widget::set"; |
132 | |
134 | |
133 | $ws->msg (ws_n => id => $id); |
135 | $ws->msg (ws_n => $id); |
134 | |
136 | |
135 | $ws |
137 | $ws |
136 | } |
138 | } |
137 | |
139 | |
138 | ############################################################################# |
140 | ############################################################################# |
… | |
… | |
144 | } |
146 | } |
145 | |
147 | |
146 | sub destroy { |
148 | sub destroy { |
147 | my ($self) = @_; |
149 | my ($self) = @_; |
148 | |
150 | |
149 | $self->msg (ws_d => id => $self->{id}); |
151 | $self->msg (ws_d => $self->{id}); |
150 | delete $self->{ns}; |
152 | delete $self->{ns}; |
151 | $_->destroy |
153 | $_->destroy |
152 | for values %{ $self->{w} }; |
154 | for values %{ $self->{w} }; |
153 | } |
155 | } |
154 | |
156 | |
155 | sub msg { |
157 | sub msg { |
156 | my ($self, $type, %msg) = @_; |
158 | my ($self, @msg) = @_; |
157 | |
159 | |
158 | if (my $ns = shift->{ns}) { |
160 | if (my $ns = shift->{ns}) { |
159 | $msg{msgtype} = $type; |
161 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
160 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\%msg)); |
162 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
161 | } |
163 | } |
162 | } |
164 | } |
163 | |
165 | |
164 | sub new { |
166 | sub new { |
165 | my ($self, $class, %args) = @_; |
167 | my ($self, $class, %args) = @_; |
166 | |
168 | |
167 | my $id = ++$self->{ns}{id}; |
169 | my $id = ++$self->{ns}{id}; |
168 | |
170 | |
169 | my $proxy = $self->{w}{$id} = bless { |
171 | my $proxy = bless { |
170 | id => $id, |
172 | id => $id, |
171 | }, "ext::widget::proxy"; |
173 | }, "ext::widget::proxy"; |
172 | |
174 | |
|
|
175 | Scalar::Util::weaken ($self->{_w}{$id} = $proxy); |
173 | Scalar::Util::weaken ($proxy->{ws} = $self); |
176 | Scalar::Util::weaken ($proxy->{ws} = $self); |
174 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
177 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
175 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
178 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
176 | |
179 | |
177 | for my $ev (grep /^on_/, keys %args) { |
180 | for my $ev (grep /^on_/, keys %args) { |
178 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
181 | push @{$proxy->{ev}{$ev}}, $args{$ev}; |
179 | $args{$ev} = 0; |
182 | $args{$ev} = 0; |
180 | } |
183 | } |
181 | |
184 | |
182 | $self->msg (ws_c => |
185 | $self->msg (ws_c => |
183 | ws => $self->{w}{id}, |
186 | $self->{id}, |
184 | id => $id, |
187 | $id, |
185 | class => $class, |
188 | $class, |
186 | args => \%args, |
189 | \%args, |
187 | ); |
190 | ); |
188 | |
191 | |
189 | $proxy |
192 | $proxy |
190 | } |
193 | } |
191 | |
194 | |
… | |
… | |
197 | my ($self) = @_; |
200 | my ($self) = @_; |
198 | |
201 | |
199 | delete $self->{ns}{widget}{$self->{id}}; |
202 | delete $self->{ns}{widget}{$self->{id}}; |
200 | |
203 | |
201 | if (my $ws = $self->{ws}) { |
204 | if (my $ws = $self->{ws}) { |
|
|
205 | $self->msg (w_c => 0, "destroy"); |
202 | delete $ws->{w}{$self->{id}}; |
206 | delete $ws->{_w}{$self->{id}}; |
203 | $self->msg (w_c => name => "destroy"); |
|
|
204 | } |
207 | } |
205 | } |
208 | } |
206 | |
209 | |
207 | sub msg { |
210 | sub msg { |
208 | my ($self, $type, %msg) = @_; |
211 | my ($self, $type, @arg) = @_; |
209 | |
212 | |
210 | if (my $ws = $self->{ws}) { |
213 | if (my $ws = $self->{ws}) { |
211 | $ws->msg ($type, |
214 | $ws->msg ($type, $self->{id}, @arg); |
212 | %msg, |
|
|
213 | id => $self->{id}, |
|
|
214 | ); |
|
|
215 | } |
215 | } |
216 | } |
216 | } |
217 | |
217 | |
218 | sub msg_cb { |
218 | sub msg_cb { |
219 | my ($self, $cb, $type, %msg) = @_; |
219 | my ($self, $cb, $type, @arg) = @_; |
220 | |
220 | |
221 | if (my $ws = $self->{ws}) { |
221 | if (my $ws = $self->{ws}) { |
222 | |
|
|
223 | my $rid = ++$ws->{ns}{id}; |
222 | my $rid = ++$ws->{ns}{id}; |
224 | |
223 | |
225 | $self->msg ($type, %msg, rid => $rid); |
224 | $self->msg ($type, $rid, @arg); |
226 | |
225 | |
227 | if ($cb) { |
226 | if ($cb) { |
228 | $ws->{ns}{widget_return}{$rid} = $cb; |
227 | $ws->{ns}{widget_return}{$rid} = $cb; |
229 | } else { |
228 | } else { |
230 | # synchronous case |
229 | # synchronous case |
… | |
… | |
243 | |
242 | |
244 | () |
243 | () |
245 | } |
244 | } |
246 | |
245 | |
247 | sub set { |
246 | sub set { |
248 | my ($self, $member, $value) = @_; |
247 | my ($self, @kv) = @_; |
249 | |
248 | |
250 | $self->msg (w_s => attr => [ [$member, $value] ]); |
249 | $self->msg (w_s => \@kv); |
251 | } |
250 | } |
252 | |
251 | |
253 | sub get { |
252 | sub get { |
254 | my ($self, $member, $cb) = @_; |
253 | my ($self, $member, $cb) = @_; |
255 | |
254 | |
256 | $self->msg_cb ($cb, w_g => attr => [$member]); |
255 | $self->msg_cb ($cb, w_g => [$member]); |
257 | } |
256 | } |
258 | |
257 | |
259 | sub TO_JSON { |
258 | sub TO_JSON { |
260 | { __widget_ref__ => $_[0]{id} } |
259 | { __w_ => $_[0]{id} } |
261 | } |
260 | } |
262 | |
261 | |
263 | our $AUTOLOAD; |
262 | our $AUTOLOAD; |
264 | |
263 | |
265 | sub AUTOLOAD { |
264 | sub AUTOLOAD { |
266 | $AUTOLOAD =~ s/^.*:// |
265 | $AUTOLOAD =~ s/^.*:// |
267 | or return; |
266 | or return; |
268 | |
267 | |
269 | my $self = shift; |
268 | my $self = shift; |
270 | |
269 | |
271 | $self->msg (w_c => name => $AUTOLOAD, args => \@_); |
270 | $self->msg (w_c => 0, $AUTOLOAD, \@_); |
272 | |
271 | |
273 | () |
272 | () |
274 | } |
273 | } |
275 | |
274 | |