Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed May 17, 2024
1 parent bd8ad9d commit df45fcc
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/Traits/ChannelMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ trait ChannelMethods
*/
protected static array $_listeners = [];

/**
* @var array = [channelKey => callback]
*/
protected static array $_listenerCallbacks = [];

/**
* @var float|int|null
*/
Expand Down Expand Up @@ -156,17 +151,16 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C

// 设置回调
$channel[$workerId]['futureId'] =
self::$_listenerCallbacks[$key] = $listener;
self::$_listeners[$key] =
$result = Future::add(function () use ($key, $workerId) {
$result = Future::add(function () use ($key, $workerId, $listener) {
// 原子性执行
self::_Atomic($key, function () use ($key, $workerId) {
self::_Atomic($key, function () use ($key, $workerId, $listener) {
$channel = self::_Get($channelName = self::GetChannelKey($key), []);
if ((!empty($value = $channel[$workerId]['value'] ?? []))) {
// 先进先出
$msg = array_shift($value);
$channel[$workerId]['value'] = $value;
call_user_func(self::$_listenerCallbacks[$key], $key, $workerId, $msg);
call_user_func($listener, $key, $workerId, $msg);
self::_Set($channelName, $channel);
}

Expand Down Expand Up @@ -221,7 +215,7 @@ protected static function _ChRemoveListener(string $key, string|int $workerId, b
unset($channel[$workerId]);
self::_Set($channelName, $channel);
}
unset(self::$_listeners[$key], self::$_listenerCallbacks[$key]);
unset(self::$_listeners[$key]);
}

return [
Expand Down

0 comments on commit df45fcc

Please sign in to comment.