Skip to content
Browse files

Cache event and role to avoid some overhead in repeated emit() calls.

Add (and comment out) experimental edge-detection code.  This avoids re-emitting a message when an emitter is set to its current value.
  • Loading branch information...
1 parent 7301d96 commit e8bce167f953231cc0bb16e998e92dde7033bfd7 @rcaputo committed
Showing with 26 additions and 5 deletions.
  1. +21 −4 docs/EmitterTrait.pm
  2. +5 −1 docs/ObserverTrait.pm
View
25 docs/EmitterTrait.pm
@@ -8,17 +8,35 @@ has trigger => (
my $meta_self = shift;
# $meta_self->name() is not set yet.
- # Weaken $meta_self so that the closure isn't fatal.
- weaken $meta_self;
+ # Weaken $meta_self so that the closure isn't permanent.
+
+ my $event;
+ #my $last_value;
sub {
my ($self, $value) = @_;
+ # Edge-detection. Only emit when a value has changed.
+ # TODO - Make this logic optional. Sometimes an application
+ # needs level logic rather than edge logic.
+
+ #return if (
+ # (!defined($value) and !defined($last_value))
+ # or
+ # (defined($value) and defined($last_value) and $value eq $last_value)
+ #);
+ #
+ #$last_value = $value;
+ #weaken $last_value if defined($last_value) and ref($last_value);
+
$self->emit(
args => {
value => $value,
},
- event => $self->meta->get_attribute($meta_self->name())->event(),
+ event => (
+ $event ||=
+ $self->meta->find_attribute_by_name($meta_self->name())->event()
+ ),
);
}
}
@@ -27,7 +45,6 @@ has trigger => (
has event => (
isa => 'Str',
is => 'ro',
- lazy => 1,
default => sub {
my $self = shift;
return $self->name();
View
6 docs/ObserverTrait.pm
@@ -12,6 +12,7 @@ has trigger => (
# TODO - If we can get the name out here, then we save a name()
# method call every trigger.
weaken $meta_self;
+ my $role;
sub {
my ($self, $value) = @_;
@@ -24,7 +25,10 @@ has trigger => (
$self->observe_role(
observed => $value,
- role => $self->meta->get_attribute($meta_self->name())->role(),
+ role => (
+ $role ||=
+ $self->meta->find_attribute_by_name($meta_self->name())->role()
+ ),
);
}
}

0 comments on commit e8bce16

Please sign in to comment.
Something went wrong with that request. Please try again.