NAME

Net::Object::Peer::Ephemeral - Proxy role for ephemeal peers

VERSION

version 0.04

DESCRIPTION

This role's sole purpose is to inform a subscriber that the emitter is ephemeral (e.g., will disappear without an additional reference) and it should keep track of it. Normally only weak references to emittes are kept, so as to prevent them outliving their intended scope.

Sometimes, however, it is necessary that the emitter outlive its scope. For example, assume that $subscriber expects event A to be emitted when some condition has been met by $emitter, but $emitter actually emits B. $subscriber could tie $emitter's B event to it's own B callback, but if $subscriber searches its subscriptions for A event emitters, it won't find this one.

One workaround is to create a proxy object which subscribes to $emitter's B event and re-emits it (with $emitter as the emitter) as event A. How does one keep that object alive? Typically it would be created on the fly when passed to the subscriber, e.g.

$subscriber->subscribe( Translator->new( emitter => $emitter,
                                    from => C<B>, to => C<A> ),
                                    C<A> );

If $subscriber doesn't hold on to the proxy object, it will be destroyed immediately after the subscription, and the whole process fails.

Instead, if Translator consumes the Net::Ojbect::Peer::Ephemeral role, $subscriber will ensure it is not destroyed by holding strong reference to it. In this example, Translator should subscribe to the $emitter's detach event so that it can detach itself from $subscriber and thus be destroyed.

SYNTAX

# in class which will act as an intermediate/proxy
package Ephemeral;
with 'Net::Object::Peer::Ephemeral';

[...]

AUTHOR

Diab Jerius <djerius@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2016 by Smithsonian Astrophysical Observatory.

This is free software, licensed under:

The GNU General Public License, Version 3, June 2007