dtyq/async-event

Maintainers

👁 twosee

Package info

github.com/dtyq/async-event

pkg:composer/dtyq/async-event

Statistics

Installs: 7 544

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v0.1.6 2026-04-02 12:07 UTC

Suggests

  • swow/swow: Required to create swow components.

Provides

None

Conflicts

None

Replaces

None

Apache License 2.0 2b83e4609a0765cfdde92bd84ad4425654ba082e

phphyperf

This package is auto-updated.

Last update: 2026-06-02 12:33:52 UTC


README

  • 事件将会放到一个协程中,然后按照顺序执行
  • 核心代码为\Dtyq\AsyncEvent\AsyncEventDispatcher::dispatch

安装

  • 安装
composer require dtyq/async-event
  • 发布配置
php bin/hyperf.php vendor:publish dtyq/async-event
  • 运行数据库迁移
php bin/hyperf.php migrate

使用方式

  • 为了不影响原有逻辑,采用新的dispatcher即可

demo

<?php

declare(strict_types=1);
/**
 * Copyright (c) The Magic , Distributed under the software license
 */
 
namespace App\Controller;

use App\Event\DemoEvent;
use Hyperf\Di\Annotation\Inject;
use Dtyq\AsyncEvent\AsyncEventDispatcher;

class IndexController extends AbstractController
{
 /**
 * @Inject()
 */
 protected AsyncEventDispatcher $asyncEventDispatcher;

 public function index()
 {
 $user = $this->request->input('user', 'Hyperf');
 $method = $this->request->getMethod();

 $this->asyncEventDispatcher->dispatch(new DemoEvent([123,222], 9));

 return [
 'method' => $method,
 'message' => "Hello {$user}.",
 ];
 }
}
  • 达到最大执行次数,可以进行消息提醒,但是需要自己增加配置,本项目仅提供达到最大重试事件

注意事项

  • 事件中尽量不要使用协程上下文来传递数据,因为事件是异步的,可能会导致数据不一致