pull/632/head
n0nag0n 4 weeks ago
parent 2e68ccb0d0
commit 046a034455

@ -35,7 +35,7 @@ class EventDispatcher
foreach ($this->listeners[$event] as $callback) { foreach ($this->listeners[$event] as $callback) {
$result = call_user_func_array($callback, $args); $result = call_user_func_array($callback, $args);
// If you return false, it will break the loop and stop the other event listeners. // If you return false, it will break the loop and stop the other event listeners.
if ($result === false) { if ($result === false) {
break; // Stop executing further listeners break; // Stop executing further listeners
} }

@ -195,7 +195,7 @@ class EventSystemTest extends TestCase
Flight::onEvent('test.event', 'not_a_callable'); Flight::onEvent('test.event', 'not_a_callable');
} }
/** /**
* Test that event propagation stops if a listener returns false. * Test that event propagation stops if a listener returns false.
*/ */
public function testStopPropagation() public function testStopPropagation()
@ -203,23 +203,23 @@ class EventSystemTest extends TestCase
$firstCalled = false; $firstCalled = false;
$secondCalled = false; $secondCalled = false;
$thirdCalled = false; $thirdCalled = false;
Flight::onEvent('test.event', function () use (&$firstCalled) { Flight::onEvent('test.event', function () use (&$firstCalled) {
$firstCalled = true; $firstCalled = true;
return true; // Continue propagation return true; // Continue propagation
}); });
Flight::onEvent('test.event', function () use (&$secondCalled) { Flight::onEvent('test.event', function () use (&$secondCalled) {
$secondCalled = true; $secondCalled = true;
return false; // Stop propagation return false; // Stop propagation
}); });
Flight::onEvent('test.event', function () use (&$thirdCalled) { Flight::onEvent('test.event', function () use (&$thirdCalled) {
$thirdCalled = true; $thirdCalled = true;
}); });
Flight::triggerEvent('test.event'); Flight::triggerEvent('test.event');
$this->assertTrue($firstCalled, 'First listener should be called'); $this->assertTrue($firstCalled, 'First listener should be called');
$this->assertTrue($secondCalled, 'Second listener should be called'); $this->assertTrue($secondCalled, 'Second listener should be called');
$this->assertFalse($thirdCalled, 'Third listener should not be called after propagation stopped'); $this->assertFalse($thirdCalled, 'Third listener should not be called after propagation stopped');

Loading…
Cancel
Save