… | |
… | |
17 | |
17 | |
18 | cf::client->attach ( |
18 | cf::client->attach ( |
19 | on_connect => sub { |
19 | on_connect => sub { |
20 | my ($ns) = @_; |
20 | my ($ns) = @_; |
21 | |
21 | |
22 | Scalar::Util::weaken (my $weakns = $ns); |
|
|
23 | |
|
|
24 | $ns->{id} = "a"; |
22 | $ns->{id} = "a"; |
25 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
23 | $ns->{json_coder}->filter_json_single_key_object ("\fw" => sub { |
26 | $weakns->{widget}{$_[0]} |
24 | $ns->{widget}{$_[0]} |
27 | }); |
25 | }); |
|
|
26 | |
|
|
27 | cf::weaken $ns; |
28 | }, |
28 | }, |
29 | ); |
29 | ); |
30 | |
30 | |
31 | sub csc_update_stats { |
31 | sub csc_update_stats { |
32 | my ($ns) = @_; |
32 | my ($ns) = @_; |
… | |
… | |
199 | id => $id, |
199 | id => $id, |
200 | ns => $self, |
200 | ns => $self, |
201 | _w => {}, |
201 | _w => {}, |
202 | }, "ext::widget::set"; |
202 | }, "ext::widget::set"; |
203 | |
203 | |
|
|
204 | cf::weaken $ws->{ns}; |
|
|
205 | |
204 | $ws->msg (ws_n => $id); |
206 | $ws->msg (ws_n => $id); |
205 | |
207 | |
206 | $ws |
208 | $ws |
207 | } |
209 | } |
208 | |
210 | |
… | |
… | |
234 | |
236 | |
235 | sub msg { |
237 | sub msg { |
236 | my ($self, @msg) = @_; |
238 | my ($self, @msg) = @_; |
237 | |
239 | |
238 | if (my $ns = shift->{ns}) { |
240 | if (my $ns = shift->{ns}) { |
|
|
241 | return unless $ns->{json_coder};#d# might be gone at destroy time(??) |
239 | warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
242 | #d#warn "msg " . $ns->{json_coder}->encode (\@msg) if $DEBUG;#d# |
240 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
243 | $ns->send_packet ("ext " . $ns->{json_coder}->encode (\@msg)); |
241 | } |
244 | } |
242 | } |
245 | } |
243 | |
246 | |
244 | sub alloc { |
247 | sub alloc { |
… | |
… | |
248 | |
251 | |
249 | my $proxy = bless { |
252 | my $proxy = bless { |
250 | id => $id, |
253 | id => $id, |
251 | }, "ext::widget::proxy"; |
254 | }, "ext::widget::proxy"; |
252 | |
255 | |
253 | Scalar::Util::weaken ($proxy->{ns} = $self->{ns}); |
256 | cf::weaken ($proxy->{ns} = $self->{ns}); |
254 | Scalar::Util::weaken ($self->{ns}{widget}{$id} = $proxy); |
257 | cf::weaken ($self->{ns}{widget}{$id} = $proxy); |
255 | |
258 | |
256 | $proxy |
259 | $proxy |
257 | } |
260 | } |
258 | |
261 | |
259 | sub new { |
262 | sub new { |
260 | my ($self, $class, %args) = @_; |
263 | my ($self, $class, %args) = @_; |
261 | |
264 | |
262 | my $proxy = $self->alloc; |
265 | my $proxy = $self->alloc; |
263 | |
266 | |
264 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
267 | cf::weaken |
265 | Scalar::Util::weaken ($proxy->{ws} = $self); |
268 | +($self->{_w}{$proxy->{id}} = $proxy), |
|
|
269 | +($proxy->{ws} = $self); |
266 | |
270 | |
267 | for my $ev (grep /^on_/, keys %args) { |
271 | for my $ev (grep /^on_/, keys %args) { |
268 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
272 | $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); |
269 | } |
273 | } |
270 | |
274 | |
… | |
… | |
288 | |
292 | |
289 | my $proxy = $self->alloc; |
293 | my $proxy = $self->alloc; |
290 | |
294 | |
291 | $self->{delete $args->{alias} or $name} = $proxy; |
295 | $self->{delete $args->{alias} or $name} = $proxy; |
292 | |
296 | |
|
|
297 | cf::weaken |
293 | Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); |
298 | +($self->{_w}{$proxy->{id}} = $proxy), |
294 | Scalar::Util::weaken ($proxy->{ws} = $self); |
299 | +($proxy->{ws} = $self); |
295 | |
300 | |
296 | for my $ev (grep /^on_/, keys %$args) { |
301 | for my $ev (grep /^on_/, keys %$args) { |
297 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
302 | $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); |
298 | } |
303 | } |
299 | |
304 | |
… | |
… | |
350 | |
355 | |
351 | ############################################################################# |
356 | ############################################################################# |
352 | |
357 | |
353 | package ext::widget::proxy; |
358 | package ext::widget::proxy; |
354 | |
359 | |
355 | sub DESTROY { |
|
|
356 | my ($self) = @_; |
|
|
357 | |
|
|
358 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
359 | |
|
|
360 | if (my $ws = $self->{ws}) { |
|
|
361 | $self->msg (w_c => 0, "destroy"); |
|
|
362 | delete $ws->{_w}{$self->{id}}; |
|
|
363 | } |
|
|
364 | } |
|
|
365 | |
|
|
366 | sub cb { |
|
|
367 | my ($self, $cb) = @_; |
|
|
368 | |
|
|
369 | my $proxy = bless { |
|
|
370 | ns => $self->{ns}, |
|
|
371 | id => $self->{ns}->alloc_wid, |
|
|
372 | }, "ext::widget::callback"; |
|
|
373 | |
|
|
374 | Scalar::Util::weaken $proxy->{ns}; |
|
|
375 | |
|
|
376 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
377 | |
|
|
378 | $proxy |
|
|
379 | } |
|
|
380 | |
|
|
381 | sub oneshot_cb { |
|
|
382 | my ($self, $cb) = @_; |
|
|
383 | |
|
|
384 | if ("CODE" eq ref $cb) { |
|
|
385 | my $ocb = $cb; |
|
|
386 | $cb = $self->cb (sub { |
|
|
387 | undef $cb; |
|
|
388 | &$ocb |
|
|
389 | }); |
|
|
390 | } |
|
|
391 | |
|
|
392 | $cb |
|
|
393 | } |
|
|
394 | |
|
|
395 | sub msg { |
360 | sub msg { |
396 | my ($self, $type, @arg) = @_; |
361 | my ($self, $type, @arg) = @_; |
397 | |
362 | |
398 | if (my $ns = $self->{ns}) { |
363 | if (my $ns = $self->{ns}) { |
399 | my @msg = ($type, $self->{id}, @arg); |
364 | my @msg = ($type, $self->{id}, @arg); |
… | |
… | |
436 | } |
401 | } |
437 | |
402 | |
438 | () |
403 | () |
439 | } |
404 | } |
440 | |
405 | |
|
|
406 | sub DESTROY { |
|
|
407 | my ($self) = @_; |
|
|
408 | |
|
|
409 | delete $self->{ns}{widget}{$self->{id}}; |
|
|
410 | |
|
|
411 | if (my $ws = $self->{ws}) { |
|
|
412 | $self->msg (w_c => 0, "destroy"); |
|
|
413 | delete $ws->{_w}{$self->{id}}; |
|
|
414 | } |
|
|
415 | } |
|
|
416 | |
|
|
417 | sub cb { |
|
|
418 | my ($self, $cb) = @_; |
|
|
419 | |
|
|
420 | my $proxy = bless { |
|
|
421 | ns => $self->{ns}, |
|
|
422 | id => $self->{ns}->alloc_wid, |
|
|
423 | }, "ext::widget::callback"; |
|
|
424 | |
|
|
425 | cf::weaken $proxy->{ns}; |
|
|
426 | |
|
|
427 | $self->{ns}{widget_cb}{$proxy->{id}} = $cb; |
|
|
428 | |
|
|
429 | $proxy |
|
|
430 | } |
|
|
431 | |
|
|
432 | sub oneshot_cb { |
|
|
433 | my ($self, $cb) = @_; |
|
|
434 | |
|
|
435 | if ("CODE" eq ref $cb) { |
|
|
436 | my $ocb = $cb; |
|
|
437 | $cb = cb $self, sub { |
|
|
438 | undef $cb; |
|
|
439 | &$ocb |
|
|
440 | }; |
|
|
441 | } |
|
|
442 | |
|
|
443 | $cb |
|
|
444 | } |
|
|
445 | |
441 | sub set { |
446 | sub set { |
442 | my ($self, @kv) = @_; |
447 | my ($self, @kv) = @_; |
443 | |
448 | |
444 | $self->msg (w_s => \@kv); |
449 | $self->msg (w_s => \@kv); |
445 | } |
450 | } |