Skip to content

Commit

Permalink
op: optimize event manager, allow destroy event data after trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 6, 2019
1 parent 12df881 commit 50bf43d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
13 changes: 13 additions & 0 deletions src/event/src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public function __construct(string $name = '', array $params = [])
}
}

/**
* Destroy event data
*
* @return $this
*/
public function destroy(): self
{
$this->params = [];
$this->target = [];

return $this;
}

/**
* @param string $name
*
Expand Down
75 changes: 50 additions & 25 deletions src/event/src/Manager/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ class EventManager implements EventManagerInterface
*/
// protected $parent;

/**
* @var EventInterface
*/
protected $basicEvent;

/**
* Predefined event store
*
Expand All @@ -58,14 +53,14 @@ class EventManager implements EventManagerInterface
* 'event name' => (object)EventInterface -- event description
* ]
*/
protected $events = [];
private $events = [];

/**
* Listener storage
*
* @var ListenerQueue[]
*/
protected $listeners = [];
private $listeners = [];

/**
* All listened event name map
Expand All @@ -74,6 +69,18 @@ class EventManager implements EventManagerInterface
*/
private $listenedEvents = [];

/**
* @var EventInterface
*/
private $basicEvent;

/**
* Destroy event data after trigger event.
*
* @var bool
*/
private $destroyAfterFire = false;

/**
* EventManager constructor.
*
Expand Down Expand Up @@ -159,22 +166,23 @@ public function addSubscriber(EventSubscriberInterface $object): void
/**
* Add a listener and associate it to one (multiple) event
*
* $definition Allowed:
* $definition = [
* 'event name' => priority(int),
* 'event name1' => priority(int),
* ]
* OR
* $definition = [
* 'event name','event name1',
* ]
* OR
* $definition = 'event name'
* OR
* // The priority of the listener
* $definition = 1
*
* @param Closure|callback|mixed $listener listener
* @param array|string|int $definition Event name, priority setting
* Allowed:
* $definition = [
* 'event name' => priority(int),
* 'event name1' => priority(int),
* ]
* OR
* $definition = [
* 'event name','event name1',
* ]
* OR
* $definition = 'event name'
* OR
* // The priority of the listener
* $definition = 1
*
* @return bool
*/
Expand Down Expand Up @@ -237,8 +245,9 @@ public function addListener($listener, $definition = null): bool
}

/**
* {@inheritDoc}
* @throws InvalidArgumentException
* @param EventInterface $event
*
* @return EventInterface
*/
public function triggerEvent(EventInterface $event): EventInterface
{
Expand Down Expand Up @@ -322,7 +331,7 @@ public function trigger($event, $target = null, array $args = []): EventInterfac
$this->triggerListeners($this->listeners[$name], $event, $method);

if ($event->isPropagationStopped()) {
return $event;
return $this->destroyAfterFire ? $event->destroy() : $event;
}
}

Expand All @@ -331,7 +340,7 @@ public function trigger($event, $target = null, array $args = []): EventInterfac
$this->triggerListeners($this->listeners['*'], $event);
}

return $event;
return $this->destroyAfterFire ? $event->destroy() : $event;
}

/**
Expand Down Expand Up @@ -737,4 +746,20 @@ protected function triggerListeners($listeners, EventInterface $event, string $m
}
}
}

/**
* @return bool
*/
public function isDestroyAfterFire(): bool
{
return $this->destroyAfterFire;
}

/**
* @param bool $destroyAfterFire
*/
public function setDestroyAfterFire(bool $destroyAfterFire): void
{
$this->destroyAfterFire = $destroyAfterFire;
}
}
30 changes: 15 additions & 15 deletions src/framework/src/Swoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,6 @@ public static function getReflection(string $class): array
return Reflections::get($class);
}

/**
* Trigger an swoft application event
*
* @param string|EventInterface $event eg: 'app.start' 'app.stop'
* @param null|mixed $target
* @param array $params
*
* @return EventInterface
*/
public static function trigger($event, $target = null, ...$params): EventInterface
{
/** @see EventManager::trigger() */
return BeanFactory::getSingleton('eventManager')->trigger($event, $target, $params);
}

/**
* @param string $key
* @param array $params
Expand All @@ -178,6 +163,21 @@ public static function t(string $key, array $params = [], string $locale = ''):
return $i18n->translate($key, $params, $locale);
}

/**
* Trigger an swoft application event
*
* @param string|EventInterface $event eg: 'app.start' 'app.stop'
* @param null|mixed $target
* @param array $params
*
* @return EventInterface
*/
public static function trigger($event, $target = null, ...$params): EventInterface
{
/** @see EventManager::trigger() */
return BeanFactory::getSingleton('eventManager')->trigger($event, $target, $params);
}

/**
* Trigger an swoft application event. like self::trigger(), but params is array
*
Expand Down

0 comments on commit 50bf43d

Please sign in to comment.