Added events to GroupManager

This commit is contained in:
2024-02-21 22:43:31 +01:00
parent 418ece5155
commit a6f70dbb76
2 changed files with 35 additions and 2 deletions

View File

@ -2,12 +2,19 @@
namespace NoccyLabs\React\WebSocket\Group;
use Evenement\EventEmitterInterface;
use Evenement\EventEmitterTrait;
use NoccyLabs\React\WebSocket\WebSocketInterface;
use React\EventLoop\Loop;
use WeakReference;
class GroupManager
class GroupManager implements EventEmitterInterface
{
use EventEmitterTrait;
const EVENT_CREATED = 'created';
const EVENT_DESTROYED = 'destroyed';
/** @var array<string,ConnectionGroup> */
private array $groups = [];
@ -20,11 +27,13 @@ class GroupManager
$group->on(ConnectionGroup::EVENT_LEAVE, function () use ($group) {
Loop::futureTick(function () use ($group) {
if (count($group) === 0) {
$this->emit(self::EVENT_DESTROYED, [ $group ]);
$group->removeAllListeners();
unset($this->groups[$group->getName()]);
}
});
});
$this->emit(self::EVENT_CREATED, [ $group ]);
} else {
$group = $this->groups[$name];
}