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