You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1006 B
51 lines
1006 B
<?php |
|
|
|
namespace core\dispatchers; |
|
|
|
class DeferredEventDispatcher implements EventDispatcher |
|
{ |
|
private $defer = false; |
|
private $queue = []; |
|
private $next; |
|
|
|
public function __construct(EventDispatcher $next) |
|
{ |
|
$this->next = $next; |
|
} |
|
|
|
public function dispatchAll(array $events): void |
|
{ |
|
foreach ($events as $event) { |
|
$this->dispatch($event); |
|
} |
|
} |
|
|
|
public function dispatch($event): void |
|
{ |
|
if ($this->defer) { |
|
$this->queue[] = $event; |
|
} else { |
|
$this->next->dispatch($event); |
|
} |
|
} |
|
|
|
public function defer(): void |
|
{ |
|
$this->defer = true; |
|
} |
|
|
|
public function clean(): void |
|
{ |
|
$this->queue = []; |
|
$this->defer = false; |
|
} |
|
|
|
public function release(): void |
|
{ |
|
foreach ($this->queue as $i => $event) { |
|
$this->next->dispatch($event); |
|
unset($this->queue[$i]); |
|
} |
|
$this->defer = false; |
|
} |
|
}
|
|
|