From: Matt S Trout Date: Thu, 1 Dec 2011 22:53:52 +0000 (+0000) Subject: clone_or_self X-Git-Tag: v0.001002~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=542ab4162ceb2ed5deadae8e8440af2046e45674;p=scpubgit%2FTak.git clone_or_self --- diff --git a/lib/Tak/Client.pm b/lib/Tak/Client.pm index e8f7d60..a86ad4d 100644 --- a/lib/Tak/Client.pm +++ b/lib/Tak/Client.pm @@ -54,4 +54,12 @@ sub result_of { return $result; } +sub clone_or_self { + my ($self) = @_; + (ref $self)->new( + service => $self->service->clone_or_self, + curried => [ @{$self->curried} ], + ); +} + 1; diff --git a/lib/Tak/Role/Service.pm b/lib/Tak/Role/Service.pm index 3ee3d7f..5bcf7ca 100644 --- a/lib/Tak/Role/Service.pm +++ b/lib/Tak/Role/Service.pm @@ -33,4 +33,9 @@ sub receive { } } +# This assumes that by default either services are not stateful +# or do want to have persistent state. It's notably overriden by Router. + +sub clone_or_self { $_[0] } + 1; diff --git a/lib/Tak/Router.pm b/lib/Tak/Router.pm index a8b071c..7637c1c 100644 --- a/lib/Tak/Router.pm +++ b/lib/Tak/Router.pm @@ -42,11 +42,9 @@ sub deregister { delete $self->services->{$name} } -sub clone { +sub clone_or_self { my ($self) = @_; - my $new = (ref $self)->new(services => $self->services); - $new->register(meta => Tak::MetaService->new(router => $new)); - $new; + (ref $self)->new(services => { %{$self->services} }); } 1; diff --git a/lib/Tak/WeakClient.pm b/lib/Tak/WeakClient.pm index dd4ae0e..3beb39c 100644 --- a/lib/Tak/WeakClient.pm +++ b/lib/Tak/WeakClient.pm @@ -6,4 +6,13 @@ extends 'Tak::Client'; has service => (is => 'ro', required => 1, weak_ref => 1); +sub clone_or_self { + my ($self) = @_; + my $new = $self->service->clone_or_self; + ($new ne $self->service + ? 'Tak::Client' + : ref($self))->new(service => $new, curried => [ @{$self->curried} ]); +} + + 1;