ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.109 by root, Sat May 10 00:45:18 2008 UTC vs.
Revision 1.112 by root, Sat May 10 01:04:42 2008 UTC

549Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model 549Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model
550if necessary. You should only call this function right before you would 550if necessary. You should only call this function right before you would
551have created an AnyEvent watcher anyway, that is, as late as possible at 551have created an AnyEvent watcher anyway, that is, as late as possible at
552runtime. 552runtime.
553 553
554=item AnyEvent::on_detect { BLOCK } 554=item $guard = AnyEvent::post_detect { BLOCK }
555 555
556Arranges for the code block to be executed as soon as the event model is 556Arranges for the code block to be executed as soon as the event model is
557autodetected (or immediately if this has already happened). 557autodetected (or immediately if this has already happened).
558 558
559If called in scalar or list context, then it creates and returns an object
560that automatically removes the callback again when it is destroyed. See
561L<Coro::BDB> for a case where this is useful.
562
559=item @AnyEvent::on_detect 563=item @AnyEvent::post_detect
560 564
561If there are any code references in this array (you can C<push> to it 565If there are any code references in this array (you can C<push> to it
562before or after loading AnyEvent), then they will called directly after 566before or after loading AnyEvent), then they will called directly after
563the event loop has been chosen. 567the event loop has been chosen.
564 568
565You should check C<$AnyEvent::MODEL> before adding to this array, though: 569You should check C<$AnyEvent::MODEL> before adding to this array, though:
566if it contains a true value then the event loop has already been detected, 570if it contains a true value then the event loop has already been detected,
567and the array will be ignored. 571and the array will be ignored.
568 572
569Best use C<AnyEvent::on_detect { BLOCK }> instead. 573Best use C<AnyEvent::post_detect { BLOCK }> instead.
570 574
571=back 575=back
572 576
573=head1 WHAT TO DO IN A MODULE 577=head1 WHAT TO DO IN A MODULE
574 578
716 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 720 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
717); 721);
718 722
719our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); 723our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY);
720 724
721our @on_detect; 725our @post_detect;
722 726
723sub on_detect(&) { 727sub post_detect(&) {
728 my ($cb) = @_;
729
724 if ($MODEL) { 730 if ($MODEL) {
725 $_[0]->(); 731 $cb->();
732
733 1
726 } else { 734 } else {
727 push @on_detect, $_[0]; 735 push @post_detect, $cb;
736
737 defined wantarray
738 ? bless \$cb, "AnyEvent::Util::Guard"
739 : ()
728 } 740 }
741}
742
743sub AnyEvent::Util::Guard::DESTROY {
744 @post_detect = grep $_ != ${$_[0]}, @post_detect;
729} 745}
730 746
731sub detect() { 747sub detect() {
732 unless ($MODEL) { 748 unless ($MODEL) {
733 no strict 'refs'; 749 no strict 'refs';
775 } 791 }
776 792
777 unshift @ISA, $MODEL; 793 unshift @ISA, $MODEL;
778 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 794 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
779 795
780 (shift @on_detect)->() while @on_detect; 796 (shift @post_detect)->() while @post_detect;
781 } 797 }
782 798
783 $MODEL 799 $MODEL
784} 800}
785 801

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines