ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/widget.ext
(Generate patch)

Comparing deliantra/server/ext/widget.ext (file contents):
Revision 1.22 by root, Fri Dec 28 12:44:45 2007 UTC vs.
Revision 1.23 by root, Sat Dec 29 21:07:23 2007 UTC

17 17
18cf::client->attach ( 18cf::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
31sub csc_update_stats { 31sub 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
259sub new { 261sub new {
260 my ($self, $class, %args) = @_; 262 my ($self, $class, %args) = @_;
261 263
262 my $proxy = $self->alloc; 264 my $proxy = $self->alloc;
263 265
264 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 266 cf::weaken
265 Scalar::Util::weaken ($proxy->{ws} = $self); 267 +($self->{_w}{$proxy->{id}} = $proxy),
268 +($proxy->{ws} = $self);
266 269
267 for my $ev (grep /^on_/, keys %args) { 270 for my $ev (grep /^on_/, keys %args) {
268 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev}); 271 $args{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args{$ev});
269 } 272 }
270 273
288 291
289 my $proxy = $self->alloc; 292 my $proxy = $self->alloc;
290 293
291 $self->{delete $args->{alias} or $name} = $proxy; 294 $self->{delete $args->{alias} or $name} = $proxy;
292 295
296 cf::weaken
293 Scalar::Util::weaken ($self->{_w}{$proxy->{id}} = $proxy); 297 +($self->{_w}{$proxy->{id}} = $proxy),
294 Scalar::Util::weaken ($proxy->{ws} = $self); 298 +($proxy->{ws} = $self);
295 299
296 for my $ev (grep /^on_/, keys %$args) { 300 for my $ev (grep /^on_/, keys %$args) {
297 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev}); 301 $args->{$ev} = $proxy->{"_$ev"} = $proxy->cb ($args->{$ev});
298 } 302 }
299 303
350 354
351############################################################################# 355#############################################################################
352 356
353package ext::widget::proxy; 357package ext::widget::proxy;
354 358
355sub 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
366sub 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
381sub 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
395sub msg { 359sub msg {
396 my ($self, $type, @arg) = @_; 360 my ($self, $type, @arg) = @_;
397 361
398 if (my $ns = $self->{ns}) { 362 if (my $ns = $self->{ns}) {
399 my @msg = ($type, $self->{id}, @arg); 363 my @msg = ($type, $self->{id}, @arg);
436 } 400 }
437 401
438 () 402 ()
439} 403}
440 404
405sub DESTROY {
406 my ($self) = @_;
407
408 delete $self->{ns}{widget}{$self->{id}};
409
410 if (my $ws = $self->{ws}) {
411 $self->msg (w_c => 0, "destroy");
412 delete $ws->{_w}{$self->{id}};
413 }
414}
415
416sub cb {
417 my ($self, $cb) = @_;
418
419 my $proxy = bless {
420 ns => $self->{ns},
421 id => $self->{ns}->alloc_wid,
422 }, "ext::widget::callback";
423
424 cf::weaken $proxy->{ns};
425
426 $self->{ns}{widget_cb}{$proxy->{id}} = $cb;
427
428 $proxy
429}
430
431sub oneshot_cb {
432 my ($self, $cb) = @_;
433
434 if ("CODE" eq ref $cb) {
435 my $ocb = $cb;
436 $cb = cb $self, sub {
437 undef $cb;
438 &$ocb
439 };
440 }
441
442 $cb
443}
444
441sub set { 445sub set {
442 my ($self, @kv) = @_; 446 my ($self, @kv) = @_;
443 447
444 $self->msg (w_s => \@kv); 448 $self->msg (w_s => \@kv);
445} 449}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines