small/swoole-events

1.0.4 2024-02-06 10:08 UTC

This package is auto-updated.

Last update: 2024-11-06 11:57:21 UTC


README

small-events provide PSR event dispatcher taking advantage of OpenSwoole async framework

            

Event

An event is every class that extends AbstractEvent.

The event data must be defined in constructor via setData method in order to allow multithreading data share.

class SimpleEvent extends AbstractEvent
{

    public function __construct(int $data1, int $data2)
    {

        $this->setId(1);
        $this->data = new Collection(['data1' => $data1, 'data2' => $data2]);
        $this->createDataTable();
        $this->setData('data1', $data1);
        $this->setData('data2', $data2);

    }

    public function getData($field): mixed
    {

        $data = $this->gatherResult();
        return $data[$field];

    }

    public function jsonSerialize(): mixed
    {

        return $this->gatherResult();;

    }

}

Event Listener

An event listener is a class that handle event by doing some task and/or update event data.

It must implement ListenerInterface, which imply that you implement at least :

  • getPriority : return the priority of listener
  • getHandleableEvents : return list of events types (class) that listener ca handle
  • handle : handle an event

For example, here is a listener that can handle the previous SimpleEvent :

class AppListener implements ListenerInterface
{

    public static function getPriority(): string
    {
        return Priority::getPriorityByName(Priority::APP_EXTERNAL_LOW);
    }

    public static function getHandleableEvents(): array
    {

        return [SimpleEvent::class];

    }

    /**
     * @param SimpleEvent $event
     * @return ListenerInterface
     * @throws \Exception
     */
    public function handle(AbstractEvent $event): ListenerInterface
    {

        $event->setData('data1', 1);
        $event->setData('data2', $event->getData('data2') + 1);

        return $this;

    }

}

The priority of handling event by listeners is defined in Priority class :

\Small\SwooleEvents\Listener\Priority::KERNEL_HIGH;
\Small\SwooleEvents\Listener\Priority::KERNEL_MIDDLE;
\Small\SwooleEvents\Listener\Priority::KERNEL_LOW;
\Small\SwooleEvents\Listener\Priority::APP_INTERNAL_HIGH;
\Small\SwooleEvents\Listener\Priority::APP_INTERNAL_MIDDLE;
\Small\SwooleEvents\Listener\Priority::APP_INTERNAL_LOW;
\Small\SwooleEvents\Listener\Priority::APP_EXTERNAL_HIGH;
\Small\SwooleEvents\Listener\Priority::APP_EXTERNAL_MIDDLE;
\Small\SwooleEvents\Listener\Priority::APP_EXTERNAL_LOW;

The execution will be done from KERNEL (first) to APP_EXTERNAL (last) and from HIGH to LOW.

Event Dispatch

Create events provider and register listeners

$listenerProvider = new ListenerProvider();
$listenerProvider->addListener(new KernelSimpleEventListener());
$listenerProvider->addListener(new AppSimpleEventListener());

Create event dispatcher

$event = new EventDispatcher(
    $listenerProvider,
    $this->container, // The psr container of your favorite framework
);

Create events and disptatch them

$listenerProvider->dispatch(new SimpleEvent(0, 2));

Order of execution

The events are submitted to listener by batch segmented by priority from KERNEL to APP_EXTERNAL.

Each event handling in batch is executed asynchronous.