… | |
… | |
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. See |
|
|
561 | L<Coro::BDB> for a case where this is useful. |
|
|
562 | |
559 | =item @AnyEvent::on_detect |
563 | =item @AnyEvent::post_detect |
560 | |
564 | |
561 | If there are any code references in this array (you can C<push> to it |
565 | 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 |
566 | before or after loading AnyEvent), then they will called directly after |
563 | the event loop has been chosen. |
567 | the event loop has been chosen. |
564 | |
568 | |
565 | You should check C<$AnyEvent::MODEL> before adding to this array, though: |
569 | 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, |
570 | if it contains a true value then the event loop has already been detected, |
567 | and the array will be ignored. |
571 | and the array will be ignored. |
568 | |
572 | |
569 | Best use C<AnyEvent::on_detect { BLOCK }> instead. |
573 | Best 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 | |
719 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
723 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
720 | |
724 | |
721 | our @on_detect; |
725 | our @post_detect; |
722 | |
726 | |
723 | sub on_detect(&) { |
727 | sub 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 | |
|
|
743 | sub AnyEvent::Util::Guard::DESTROY { |
|
|
744 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
729 | } |
745 | } |
730 | |
746 | |
731 | sub detect() { |
747 | sub 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 | |