| 1 | # ABSTRACT: Proxy role for ephemeal peers | ||
| 2 | package Net::Object::Peer::Ephemeral; | ||
| 3 | |||
| 4 | 2 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | our $VERSION = '0.06'; # TRIAL | |
| 7 | |||
| 8 | 2 | 1; | |
| 9 | |||
| 10 | # | ||
| 11 | # This file is part of Net-Object-Peer | ||
| 12 | # | ||
| 13 | # This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory. | ||
| 14 | # | ||
| 15 | # This is free software, licensed under: | ||
| 16 | # | ||
| 17 | # The GNU General Public License, Version 3, June 2007 | ||
| 18 | # | ||
| 19 | |||
| 20 | =pod | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | Net::Object::Peer::Ephemeral - Proxy role for ephemeal peers | ||
| 25 | |||
| 26 | =head1 VERSION | ||
| 27 | |||
| 28 | version 0.06 | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This role's sole purpose is to inform a subscriber that the emitter is | ||
| 33 | ephemeral (e.g., will disappear without an additional reference) and | ||
| 34 | it should keep track of it. Normally only weak references to emittes | ||
| 35 | are kept, so as to prevent them outliving their intended scope. | ||
| 36 | |||
| 37 | Sometimes, however, it is necessary that the emitter outlive its | ||
| 38 | scope. For example, assume that C<$subscriber> expects event C<A> to | ||
| 39 | be emitted when some condition has been met by C<$emitter>, but | ||
| 40 | C<$emitter> actually emits C<B>. C<$subscriber> could tie | ||
| 41 | C<$emitter>'s C<B> event to it's own C<B> callback, but if | ||
| 42 | C<$subscriber> searches its subscriptions for C<A> event emitters, | ||
| 43 | it won't find this one. | ||
| 44 | |||
| 45 | One workaround is to create a proxy object which subscribes to | ||
| 46 | C<$emitter>'s C<B> event and re-emits it (with C<$emitter> as the | ||
| 47 | emitter) as event C<A>. How does one keep that object alive? Typically | ||
| 48 | it would be created on the fly when passed to the subscriber, e.g. | ||
| 49 | |||
| 50 | $subscriber->subscribe( Translator->new( emitter => $emitter, | ||
| 51 | from => C<B>, to => C<A> ), | ||
| 52 | C<A> ); | ||
| 53 | |||
| 54 | If C<$subscriber> doesn't hold on to the proxy object, it will be | ||
| 55 | destroyed immediately after the subscription, and the whole process fails. | ||
| 56 | |||
| 57 | Instead, if C<Translator> consumes the C<Net::Ojbect::Peer::Ephemeral> | ||
| 58 | role, C<$subscriber> will ensure it is not destroyed by holding strong | ||
| 59 | reference to it. In this example, C<Translator> should subscribe to | ||
| 60 | the C<$emitter>'s C<detach> event so that it can detach itself from | ||
| 61 | C<$subscriber> and thus be destroyed. | ||
| 62 | |||
| 63 | =head1 SYNTAX | ||
| 64 | |||
| 65 | # in class which will act as an intermediate/proxy | ||
| 66 | package Ephemeral; | ||
| 67 | with 'Net::Object::Peer::Ephemeral'; | ||
| 68 | |||
| 69 | [...] | ||
| 70 | |||
| 71 | =head1 AUTHOR | ||
| 72 | |||
| 73 | Diab Jerius <djerius@cpan.org> | ||
| 74 | |||
| 75 | =head1 COPYRIGHT AND LICENSE | ||
| 76 | |||
| 77 | This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory. | ||
| 78 | |||
| 79 | This is free software, licensed under: | ||
| 80 | |||
| 81 | The GNU General Public License, Version 3, June 2007 | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | __END__ | ||