… | |
… | |
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 $guard = AnyEvent::post_detect { BLOCK } |
|
|
555 | |
|
|
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). |
|
|
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 | |
554 | =item @AnyEvent::detect |
563 | =item @AnyEvent::post_detect |
555 | |
564 | |
556 | 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 |
557 | before or after loading AnyEvent), then they will called directly after |
566 | before or after loading AnyEvent), then they will called directly after |
558 | the event loop has been chosen. |
567 | the event loop has been chosen. |
559 | |
568 | |
560 | 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: |
561 | 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, |
562 | and the array will be ignored. |
571 | and the array will be ignored. |
|
|
572 | |
|
|
573 | Best use C<AnyEvent::post_detect { BLOCK }> instead. |
563 | |
574 | |
564 | =back |
575 | =back |
565 | |
576 | |
566 | =head1 WHAT TO DO IN A MODULE |
577 | =head1 WHAT TO DO IN A MODULE |
567 | |
578 | |
… | |
… | |
709 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
720 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
710 | ); |
721 | ); |
711 | |
722 | |
712 | 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); |
713 | |
724 | |
714 | our @detect; |
725 | our @post_detect; |
|
|
726 | |
|
|
727 | sub post_detect(&) { |
|
|
728 | my ($cb) = @_; |
|
|
729 | |
|
|
730 | if ($MODEL) { |
|
|
731 | $cb->(); |
|
|
732 | |
|
|
733 | 1 |
|
|
734 | } else { |
|
|
735 | push @post_detect, $cb; |
|
|
736 | |
|
|
737 | defined wantarray |
|
|
738 | ? bless \$cb, "AnyEvent::Util::Guard" |
|
|
739 | : () |
|
|
740 | } |
|
|
741 | } |
|
|
742 | |
|
|
743 | sub AnyEvent::Util::Guard::DESTROY { |
|
|
744 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
|
|
745 | } |
715 | |
746 | |
716 | sub detect() { |
747 | sub detect() { |
717 | unless ($MODEL) { |
748 | unless ($MODEL) { |
718 | no strict 'refs'; |
749 | no strict 'refs'; |
719 | |
750 | |
… | |
… | |
760 | } |
791 | } |
761 | |
792 | |
762 | unshift @ISA, $MODEL; |
793 | unshift @ISA, $MODEL; |
763 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
794 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
764 | |
795 | |
765 | (shift @detect)->() while @detect; |
796 | (shift @post_detect)->() while @post_detect; |
766 | } |
797 | } |
767 | |
798 | |
768 | $MODEL |
799 | $MODEL |
769 | } |
800 | } |
770 | |
801 | |