Skip to content

Commit

Permalink
修复在事件中取消事件监听报错 (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 20, 2023
1 parent fe905f1 commit d980eaa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Event/TEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function trigger(string $name, array $data = [], ?object $target = null,
finally
{
// 仅触发一次的处理
if ($oneTimeCallbacks)
if ($oneTimeCallbacks && isset($this->__events[$name]))
{
$eventsMap = &$this->__events[$name];
foreach ($eventsMap as $eventsKey => $item)
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Component/Tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public function testOne(): void
'return' => &$return,
], $this);
Assert::assertNull($return);

// 测试在事件中取消事件监听
$return = null;
Event::one('IMITEST.EVENT.OFF', static function () use (&$return) {
Event::off('IMITEST.EVENT.OFF');
$return = 114514;
});
Event::trigger('IMITEST.EVENT.OFF');
Assert::assertEquals(114514, $return);
}

public function testOff(): void
Expand Down

0 comments on commit d980eaa

Please sign in to comment.