Skip Menu |

This queue is for tickets about the Notification-Center CPAN distribution.

Report information
The Basics
Id: 44794
Status: resolved
Priority: 0/
Queue: Notification-Center

People
Owner: Nobody in particular
Requestors: baldmantom [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.0.4
Fixed in: (no value)



Subject: Can't register observer for multiple notifications
I have a class which needs to observe multiple objects. In version 0.0.4 of Notification::Center, only the last added event is used. I'm attaching a test case which exhibits my problem, and a patch which (I believe) solves it. Thanks, Tom Helsel
Subject: 03-multi.t
#! perl use strict; use warnings; use Test::More qw / no_plan /; { package MyInt; use Moose; use Notification::Center; has 'name'=> ( is => 'ro', isa => 'Str', required => 1 ); has 'int' => ( is => 'ro', isa => 'Int', default => 0 ); sub set_int { my ( $self, $new ) = @_; my $ns = Notification::Center->default; $self->{int} = $new; $ns->notify( { event => $self->{name}.'changed', args => $self->{int} } ); } no Moose; package MultiWatcher; use Moose; has 'int1_name' => ( isa => 'Str', is => 'ro', required => 1 ); has 'int1_val' => ( isa => 'Int', default => 0 ); has 'int2_name' => ( isa => 'Str', is => 'ro', required => 1 ); has 'int2_val' => ( isa => 'Int', default => 0 ); sub BUILD { my $self = shift; my $nc = Notification::Center->default; $nc->add({ observer => $self, event => $self->{int1_name}.'changed', method => 'update_int1'}); $nc->add({ observer => $self, event => $self->{int2_name}.'changed', method => 'update_int2'}); } sub update_int1 { my ( $self, $val ) = @_; $self->{int1_val} = $val; } sub update_int2 { my ( $self, $val ) = @_; $self->{int2_val} = $val; } sub get_total { my $self = shift; return $self->{int1_val} + $self->{int2_val}; } no Moose; } my $int1 = MyInt->new(name=>'int1'); my $int2 = MyInt->new(name=>'int2'); my $mw = MultiWatcher->new(int1_name=>'int1',int2_name=>'int2'); is ( $mw->get_total, 0, 'initial value' ); $int1->set_int(5); is ( $mw->get_total, 5, 'set int1 value' ); $int2->set_int(6); is ( $mw->get_total, 11, 'set int2 value' );
Subject: Center.pm.patch
--- ./lib/Notification/Center.pm +++ ./lib/Notification/Center.pm.new @@ -36,7 +36,7 @@ $self->observers->{$event} ||= Set::Object->new; $self->observers->{$event}->insert($observer); - $self->method_calls->{ refaddr $observer } = $method; + $self->method_calls->{ refaddr $observer }->{$event} = $method; } sub remove { @@ -45,7 +45,7 @@ my $event = $args->{'event'} || 'DEFAULT'; my $observer = $args->{'observer'}; - delete $self->method_calls->{ refaddr $observer }; + delete $self->method_calls->{ refaddr $observer }->{$event}; $self->observers->{$event}->remove($observer); } @@ -65,7 +65,8 @@ } foreach my $observer ( $observers->members ) { - my $method = $self->method_calls->{ refaddr $observer }; + my $method = $self->method_calls->{ refaddr $observer }->{$event} || + $self->method_calls->{ refaddr $observer }->{'DEFAULT'}; $observer->$method($args->{'args'}) if $observer->can($method); } } @@ -149,7 +150,7 @@ or use IOC using Bread::Board - + use Bread::Board; my $c = container 'TestApp' => as {
On Fri Apr 03 23:40:57 2009, baldmantom wrote: Show quoted text
> I have a class which needs to observe multiple objects. In version > 0.0.4 of Notification::Center, only the last added event is used. I'm > attaching a test case which exhibits my problem, and a patch which (I > believe) solves it. > > Thanks, > Tom Helsel
Sorry for the wait... I didn't get the email telling me about your request... I will talk a look at this tonight...
Updated in 0.0.5