… | |
… | |
8 | # w_c id rid name @args # widget method call |
8 | # w_c id rid name @args # widget method call |
9 | # w_s id @attr # widget member set |
9 | # w_s id @attr # widget member set |
10 | # w_g id rid @attr # widget member get |
10 | # w_g id rid @attr # widget member get |
11 | # |
11 | # |
12 | # and expects the following exti message types |
12 | # and expects the following exti message types |
13 | # w_r rid res # widget call return |
|
|
14 | # w_e id rid @args # widget_event |
13 | # w_e id @args # widget_call |
15 | |
14 | |
16 | our $DEBUG = 1; |
15 | our $DEBUG = 1; |
17 | |
16 | |
18 | cf::client->attach ( |
17 | cf::client->attach ( |
19 | on_connect => sub { |
18 | on_connect => sub { |
20 | my ($ns) = @_; |
19 | my ($ns) = @_; |
21 | |
20 | |
22 | Scalar::Util::weaken (my $weakns = $ns); |
21 | Scalar::Util::weaken (my $weakns = $ns); |
23 | |
22 | |
24 | $ns->{id} = "a"; |
23 | $ns->{id} = "a"; |
25 | $ns->{json_coder}->filter_json_single_key_object (__w_ => sub { |
24 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
26 | # cannot deserialise ATM |
25 | $weakns->{widget}{$_[0]} |
27 | undef |
|
|
28 | }); |
26 | }); |
29 | }, |
27 | }, |
30 | ); |
28 | ); |
31 | |
29 | |
32 | sub csc_update_stats { |
30 | sub csc_update_stats { |
… | |
… | |
111 | #demo_start $ns; |
109 | #demo_start $ns; |
112 | }, |
110 | }, |
113 | ); |
111 | ); |
114 | |
112 | |
115 | cf::register_exticmd w_e => sub { |
113 | cf::register_exticmd w_e => sub { |
116 | my ($ns, $id, $rid, @args) = @_; |
114 | my ($ns, $id, @args) = @_; |
117 | |
115 | |
118 | if (my $w = $ns->{widget}{$id}) { |
116 | if (my $cb = $ns->{widget_cb}{$id}) { |
119 | if (my $cb = $w->{ev}{$rid}) { |
|
|
120 | $cb->($w, @args); |
|
|
121 | } |
|
|
122 | } |
|
|
123 | |
|
|
124 | () |
|
|
125 | }; |
|
|
126 | |
|
|
127 | cf::register_exticmd w_r => sub { |
|
|
128 | my ($ns, $rid, $res) = @_; |
|
|
129 | |
|
|
130 | if (my $cb = delete $ns->{widget_return}{$rid}) { |
|
|
131 | $cb->(@$res); |
117 | $cb->(@args); |
132 | } |
118 | } |
133 | |
119 | |
134 | () |
120 | () |
135 | }; |
121 | }; |
136 | |
122 | |
… | |
… | |
148 | $ws->msg (ws_n => $id); |
134 | $ws->msg (ws_n => $id); |
149 | |
135 | |
150 | $ws |
136 | $ws |
151 | } |
137 | } |
152 | |
138 | |
|
|
139 | sub cf::client::alloc_wid { |
|
|
140 | pop @{ $_[0]{ids} } |
|
|
141 | or ++$_[0]{id} |
|
|
142 | } |
|
|
143 | |
|
|
144 | sub cf::client::free_wid { |
|
|
145 | push @{ $_[0]{ids} }, $_[1]; |
|
|
146 | } |
|
|
147 | |
153 | ############################################################################# |
148 | ############################################################################# |
154 | |
149 | |
155 | package ext::widget::set; |
150 | package ext::widget::set; |
156 | |
151 | |
157 | sub DESTROY { |
152 | sub DESTROY { |
… | |
… | |
177 | } |
172 | } |
178 | |
173 | |
179 | sub alloc { |
174 | sub alloc { |
180 | my ($self) = @_; |
175 | my ($self) = @_; |
181 | |
176 | |
182 | my $id = ++$self->{ns}{id}; |
177 | my $id = $self->{ns}->alloc_wid; |
183 | |
178 | |
184 | my $proxy = bless { |
179 | my $proxy = bless { |
185 | id => $id, |
180 | id => $id, |
186 | }, "ext::widget::proxy"; |
181 | }, "ext::widget::proxy"; |
187 | |
182 | |
… | |
… | |
198 | |
193 | |
199 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
194 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
200 | Scalar::Util::weaken ($proxy->{ws} = $self); |
195 | Scalar::Util::weaken ($proxy->{ws} = $self); |
201 | |
196 | |
202 | for my $ev (grep /^on_/, keys %args) { |
197 | for my $ev (grep /^on_/, keys %args) { |
203 | my $rid = ++$self->{ns}{id}; |
198 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
204 | $proxy->{ev}{$rid} = $args{$ev}; |
|
|
205 | $args{$ev} = $rid; |
|
|
206 | } |
199 | } |
207 | |
200 | |
208 | $self->msg (ws_c => |
201 | $self->msg (ws_c => |
209 | $self->{id}, |
202 | $self->{id}, |
210 | $proxy->{id}, |
203 | $proxy->{id}, |
… | |
… | |
250 | $self->msg (w_c => 0, "destroy"); |
243 | $self->msg (w_c => 0, "destroy"); |
251 | delete $ws->{_w}{$self->{id}}; |
244 | delete $ws->{_w}{$self->{id}}; |
252 | } |
245 | } |
253 | } |
246 | } |
254 | |
247 | |
|
|
248 | sub cb { |
|
|
249 | my ($self, $cb) = @_; |
|
|
250 | |
|
|
251 | my $proxy = bless { |
|
|
252 | ns => $self->{ns}, |
|
|
253 | id => $self->{ns}->alloc_wid, |
|
|
254 | }, "ext::widget::callback"; |
|
|
255 | |
|
|
256 | Scalar::Util::weaken $proxy->{ns}; |
|
|
257 | |
|
|
258 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
259 | |
|
|
260 | $proxy |
|
|
261 | } |
|
|
262 | |
|
|
263 | sub oneshot_cb { |
|
|
264 | my ($self, $cb) = @_; |
|
|
265 | |
|
|
266 | if ("CODE" eq ref $cb) { |
|
|
267 | my $ocb = $cb; |
|
|
268 | $cb = $self->cb (sub { |
|
|
269 | undef $cb; |
|
|
270 | &$ocb |
|
|
271 | }); |
|
|
272 | } |
|
|
273 | |
|
|
274 | $cb |
|
|
275 | } |
|
|
276 | |
255 | sub msg { |
277 | sub msg { |
256 | my ($self, $type, @arg) = @_; |
278 | my ($self, $type, @arg) = @_; |
257 | |
279 | |
258 | if (my $ns = $self->{ns}) { |
280 | if (my $ns = $self->{ns}) { |
259 | my @msg = ($type, $self->{id}, @arg); |
281 | my @msg = ($type, $self->{id}, @arg); |
… | |
… | |
264 | |
286 | |
265 | sub msg_cb { |
287 | sub msg_cb { |
266 | my ($self, $cb, $type, @arg) = @_; |
288 | my ($self, $cb, $type, @arg) = @_; |
267 | |
289 | |
268 | if (my $ws = $self->{ws}) { |
290 | if (my $ws = $self->{ws}) { |
269 | my $rid = ++$ws->{ns}{id}; |
291 | my $rid = $ws->{ns}->alloc_wid; |
270 | |
|
|
271 | $self->msg ($type, $rid, @arg); |
|
|
272 | |
292 | |
273 | if ($cb) { |
293 | if ($cb) { |
274 | $ws->{ns}{widget_return}{$rid} = $cb; |
294 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
295 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
296 | $ws->{ns}->free_wid ($rid); |
|
|
297 | &$cb |
|
|
298 | }; |
|
|
299 | |
|
|
300 | $self->msg ($type, $rid, @arg); |
275 | } else { |
301 | } else { |
276 | # synchronous case |
302 | # synchronous case |
277 | my $wait = new Coro::Signal; |
303 | my $wait = new Coro::Signal; |
278 | my @res; |
304 | my @res; |
279 | |
305 | |
280 | $ws->{ns}{widget_return}{$rid} = sub { |
306 | $ws->{ns}{widget_cb}{$rid} = sub { |
|
|
307 | delete $ws->{ns}{widget_cb}{$rid}; |
|
|
308 | $ws->{ns}->free_wid ($rid); |
|
|
309 | |
281 | @res = @_; |
310 | @res = @_; |
282 | $wait->send; |
311 | $wait->send; |
283 | }; |
312 | }; |
|
|
313 | $self->msg ($type, $rid, @arg); |
284 | $wait->wait; |
314 | $wait->wait; |
285 | |
315 | |
286 | return @res; |
316 | return @res; |
287 | } |
317 | } |
288 | } |
318 | } |
… | |
… | |
297 | } |
327 | } |
298 | |
328 | |
299 | sub get { |
329 | sub get { |
300 | my ($self, $member, $cb) = @_; |
330 | my ($self, $member, $cb) = @_; |
301 | |
331 | |
302 | $self->msg_cb ($cb, w_g => [$member]); |
332 | $self->msg_cb ($cb, w_g => ref $member ? @$member : $member); |
303 | } |
333 | } |
304 | |
334 | |
305 | sub TO_JSON { |
335 | sub TO_JSON { |
306 | { __w_ => $_[0]{id} } |
336 | { "\fw" => $_[0]{id} } |
307 | } |
337 | } |
308 | |
338 | |
309 | our $AUTOLOAD; |
339 | our $AUTOLOAD; |
310 | |
340 | |
311 | sub AUTOLOAD { |
341 | sub AUTOLOAD { |
… | |
… | |
318 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
348 | $self->msg (w_c => 0, $AUTOLOAD, @_); |
319 | |
349 | |
320 | () |
350 | () |
321 | } |
351 | } |
322 | |
352 | |
|
|
353 | package ext::widget::callback; |
|
|
354 | |
|
|
355 | sub DESTROY { |
|
|
356 | my ($self) = @_; |
|
|
357 | |
|
|
358 | if (my $ns = $self->{ns}) { |
|
|
359 | delete $ns->{widget_cb}{$self->{id}}; |
|
|
360 | $ns->free_wid ($self->{id}); |
|
|
361 | } |
|
|
362 | } |
|
|
363 | |
|
|
364 | sub TO_JSON { |
|
|
365 | { "\fc" => $_[0]{id} } |
|
|
366 | } |
|
|
367 | |