Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

EventEmitter::listeners() returns "[" when no listeners available. #6

Closed
wants to merge 3 commits into from

2 participants

@skenqbx

Hello,

there was a naming conflict in class EventEmitter, listeners was a function and an array, which resulted in the function returning "[" instead of an empty array when no listeners for an event are found.

@skenqbx skenqbx closed this
@igorw
Owner

What's the status on this? Where were you using this? What is extending the EventEmitter with its own listeners variable?

@skenqbx

I actually tried to use an event emitter as a proxy object for dnode.

So, problem solved. Thanks for asking.

@skenqbx

Example, with dnode:

class Proxy extends EventEmitter {
}
$proxy = new Proxy();
$dnode = new DNode\DNode($proxy);

I now use an $emitter member variable, so dnode does not have to sync the event emitters members, and i don't have a strange behavior when calling listeners() for events that have no listeners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 26, 2012
  1. @skenqbx
  2. @skenqbx

    typo

    skenqbx authored
  3. @skenqbx
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 11 deletions.
  1. +10 −10 src/Evenement/EventEmitter.php
  2. +1 −1  src/Evenement/EventEmitter2.php
View
20 src/Evenement/EventEmitter.php
@@ -28,7 +28,7 @@
class EventEmitter
{
- protected $listeners = array();
+ protected $_listeners = array();
public function on($event, $listener)
{
@@ -36,11 +36,11 @@ public function on($event, $listener)
throw new \InvalidArgumentException('The provided listener was not a valid callable.');
}
- if (!isset($this->listeners[$event])) {
- $this->listeners[$event] = array();
+ if (!isset($this->_listeners[$event])) {
+ $this->_listeners[$event] = array();
}
- $this->listeners[$event][] = $listener;
+ $this->_listeners[$event][] = $listener;
}
public function once($event, $listener)
@@ -58,9 +58,9 @@ public function once($event, $listener)
public function removeListener($event, $listener)
{
- if (isset($this->listeners[$event])) {
- if (false !== $index = array_search($listener, $this->listeners[$event], true)) {
- unset($this->listeners[$event][$index]);
+ if (isset($this->_listeners[$event])) {
+ if (false !== $index = array_search($listener, $this->_listeners[$event], true)) {
+ unset($this->_listeners[$event][$index]);
}
}
}
@@ -68,15 +68,15 @@ public function removeListener($event, $listener)
public function removeAllListeners($event = null)
{
if ($event !== null) {
- unset($this->listeners[$event]);
+ unset($this->_listeners[$event]);
} else {
- $this->listeners = array();
+ $this->_listeners = array();
}
}
public function listeners($event)
{
- return isset($this->listeners[$event]) ? $this->listeners[$event] : array();
+ return isset($this->_listeners[$event]) ? $this->_listeners[$event] : array();
}
public function emit($event, array $arguments = array())
View
2  src/Evenement/EventEmitter2.php
@@ -88,7 +88,7 @@ public function listeners($event)
{
$matchedListeners = array();
- foreach ($this->listeners as $name => $listeners) {
+ foreach ($this->_listeners as $name => $listeners) {
foreach ($listeners as $listener) {
if ($this->matchEventName($event, $name)) {
$matchedListeners[] = $listener;
Something went wrong with that request. Please try again.