Skip to content

Commit

Permalink
Unsubscribe one time event trap
Browse files Browse the repository at this point in the history
  • Loading branch information
mleko committed Oct 16, 2016
1 parent 4d32281 commit 6108f53
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Listener/EventTrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public function __construct($oneTime = true)
*/
public function handle($event, \Mleko\Narrator\Meta $meta)
{
if ($this->oneTime && !empty($this->trappedEvents)) {
return;
if ($this->oneTime) {
$this->close($meta);
if (!empty($this->trappedEvents)) {
return;
}
}
$this->trappedEvents[] = $event;
}
Expand All @@ -55,4 +58,13 @@ public function getFirstEvent()
return isset($this->trappedEvents[0]) ? $this->trappedEvents[0] : null;
}

/**
* @param \Mleko\Narrator\Meta $meta
* @return bool
*/
public function close(\Mleko\Narrator\Meta $meta)
{
return $meta->getEventSource()->unsubscribe($meta->getMatchedName(), $this);
}

}

0 comments on commit 6108f53

Please sign in to comment.