Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
1. 修复Channel方法bug
  • Loading branch information
chaz6chez committed Nov 18, 2023
1 parent 10e59f9 commit 478eadf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions src/Traits/ChannelMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
if (isset(self::$_listeners[$key])) {
throw new Error("Channel $key listener already exist. ");
}
self::$_listeners[$key] = $listener;
self::_Atomic($key, function () use (
$key, $workerId, $func, $params, &$result
) {
Expand All @@ -137,7 +136,9 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
$channel = self::_Get($channelName = self::GetChannelKey($key), []);

// 设置回调
$channel[$workerId]['futureId'] = $result = Future::add(function () use ($key, $workerId) {
$channel[$workerId]['futureId'] =
self::$_listeners[$key] =
$result = Future::add(function () use ($key, $workerId) {
// 原子性执行
self::_Atomic($key, function () use ($key, $workerId) {
$channel = self::_Get($channelName = self::GetChannelKey($key), []);
Expand Down Expand Up @@ -178,31 +179,30 @@ protected static function _ChCreateListener(string $key, string|int $workerId, C
* @param bool $remove 是否移除消息
* @return void
*/
protected static function _ChRemoveListener(string $key, string|int $workerId, bool $remove = true): void
protected static function _ChRemoveListener(string $key, string|int $workerId, bool $remove = false): void
{
$func = __FUNCTION__;
$params = func_get_args();
self::_Atomic($key, function () use (
$key, $workerId, $func, $params, $remove
) {
/**
* [
* workerId = [
* 'futureId' => futureId,
* 'value' => array
* ]
* ]
*/
$channel = self::_Get($channelName = self::GetChannelKey($key), []);
if ($id = $channel[$workerId]['futureId'] ?? null) {
if ($id = self::$_listeners[$key] ?? null) {
Future::del($id);
if ($remove) {
/**
* [
* workerId = [
* 'futureId' => futureId,
* 'value' => array
* ]
* ]
*/
$channel = self::_Get($channelName = self::GetChannelKey($key), []);
unset($channel[$workerId]);
} else {
$channel[$workerId]['futureId'] = null;
self::_Set($channelName, $channel);
}
unset(self::$_listeners[$key]);
self::_Set($channelName, $channel);

}

return [
Expand Down
4 changes: 2 additions & 2 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testChannelRemoveListener(): void
// 确认数据
$this->assertEquals([
'1' => [
'futureId' => null,
'futureId' => 1,
'value' => []
]
], apcu_fetch(Cache::GetChannelKey($channel)));
Expand Down Expand Up @@ -225,7 +225,7 @@ public function testChannelRemoveListenerByChild(): void
// 确认数据
$this->assertEquals([
'1' => [
'futureId' => null,
'futureId' => 1,
'value' => []
]
], apcu_fetch(Cache::GetChannelKey($channel)));
Expand Down

0 comments on commit 478eadf

Please sign in to comment.