diff --git a/composer.json b/composer.json index f252275b..44316a16 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "autoload": { "psr-4": { "React\\EventLoop\\": "src" - } + }, + "files": ["src/functions.php"] }, "extra": { "branch-alias": { diff --git a/src/ExtEventLoop.php b/src/ExtEventLoop.php index 6df3fa71..89f98540 100644 --- a/src/ExtEventLoop.php +++ b/src/ExtEventLoop.php @@ -114,7 +114,7 @@ public function removeStream($stream) */ public function addTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, false); + $timer = new Timer($interval, $callback, false); $this->scheduleTimer($timer); @@ -126,7 +126,7 @@ public function addTimer($interval, callable $callback) */ public function addPeriodicTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, true); + $timer = new Timer($interval, $callback, true); $this->scheduleTimer($timer); diff --git a/src/Factory.php b/src/Factory.php deleted file mode 100644 index 207bc13c..00000000 --- a/src/Factory.php +++ /dev/null @@ -1,21 +0,0 @@ -getCallback(), $timer); @@ -125,7 +125,7 @@ public function addTimer($interval, callable $callback) */ public function addPeriodicTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, true); + $timer = new Timer($interval, $callback, true); $callback = function () use ($timer) { call_user_func($timer->getCallback(), $timer); diff --git a/src/LibEventLoop.php b/src/LibEventLoop.php index a55d6104..ecf45f79 100644 --- a/src/LibEventLoop.php +++ b/src/LibEventLoop.php @@ -119,7 +119,7 @@ public function removeStream($stream) */ public function addTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, false); + $timer = new Timer($interval, $callback, false); $this->scheduleTimer($timer); @@ -131,7 +131,7 @@ public function addTimer($interval, callable $callback) */ public function addPeriodicTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, true); + $timer = new Timer($interval, $callback, true); $this->scheduleTimer($timer); diff --git a/src/State.php b/src/State.php new file mode 100644 index 00000000..9e2ae3d9 --- /dev/null +++ b/src/State.php @@ -0,0 +1,8 @@ +timers->add($timer); @@ -109,7 +109,7 @@ public function addTimer($interval, callable $callback) */ public function addPeriodicTimer($interval, callable $callback) { - $timer = new Timer($this, $interval, $callback, true); + $timer = new Timer($interval, $callback, true); $this->timers->add($timer); diff --git a/src/Tick/FutureTickQueue.php b/src/Tick/FutureTickQueue.php index eeffd363..2c34d62d 100644 --- a/src/Tick/FutureTickQueue.php +++ b/src/Tick/FutureTickQueue.php @@ -2,20 +2,14 @@ namespace React\EventLoop\Tick; -use React\EventLoop\LoopInterface; use SplQueue; class FutureTickQueue { - private $eventLoop; private $queue; - /** - * @param LoopInterface $eventLoop The event loop passed as the first parameter to callbacks. - */ - public function __construct(LoopInterface $eventLoop) + public function __construct() { - $this->eventLoop = $eventLoop; $this->queue = new SplQueue(); } @@ -40,10 +34,7 @@ public function tick() $count = $this->queue->count(); while ($count--) { - call_user_func( - $this->queue->dequeue(), - $this->eventLoop - ); + call_user_func($this->queue->dequeue()); } } diff --git a/src/Tick/NextTickQueue.php b/src/Tick/NextTickQueue.php index 5b8e1de8..12d162be 100644 --- a/src/Tick/NextTickQueue.php +++ b/src/Tick/NextTickQueue.php @@ -2,20 +2,14 @@ namespace React\EventLoop\Tick; -use React\EventLoop\LoopInterface; use SplQueue; class NextTickQueue { - private $eventLoop; private $queue; - /** - * @param LoopInterface $eventLoop The event loop passed as the first parameter to callbacks. - */ - public function __construct(LoopInterface $eventLoop) + public function __construct() { - $this->eventLoop = $eventLoop; $this->queue = new SplQueue(); } @@ -38,10 +32,7 @@ public function add(callable $listener) public function tick() { while (!$this->queue->isEmpty()) { - call_user_func( - $this->queue->dequeue(), - $this->eventLoop - ); + call_user_func($this->queue->dequeue()); } } diff --git a/src/Timer/Timer.php b/src/Timer/Timer.php index ac64d2b0..ad6729a4 100644 --- a/src/Timer/Timer.php +++ b/src/Timer/Timer.php @@ -2,36 +2,29 @@ namespace React\EventLoop\Timer; -use React\EventLoop\LoopInterface; +use React\EventLoop as l; class Timer implements TimerInterface { const MIN_INTERVAL = 0.000001; - protected $loop; protected $interval; protected $callback; protected $periodic; protected $data; - public function __construct(LoopInterface $loop, $interval, callable $callback, $periodic = false, $data = null) + public function __construct($interval, callable $callback, $periodic = false, $data = null) { if ($interval < self::MIN_INTERVAL) { $interval = self::MIN_INTERVAL; } - $this->loop = $loop; $this->interval = (float) $interval; $this->callback = $callback; $this->periodic = (bool) $periodic; $this->data = null; } - public function getLoop() - { - return $this->loop; - } - public function getInterval() { return $this->interval; @@ -59,11 +52,11 @@ public function isPeriodic() public function isActive() { - return $this->loop->isTimerActive($this); + return l\loop()->isTimerActive($this); } public function cancel() { - $this->loop->cancelTimer($this); + l\loop()->cancelTimer($this); } } diff --git a/src/Timer/TimerInterface.php b/src/Timer/TimerInterface.php index 5982b314..38c6ff5b 100644 --- a/src/Timer/TimerInterface.php +++ b/src/Timer/TimerInterface.php @@ -4,7 +4,6 @@ interface TimerInterface { - public function getLoop(); public function getInterval(); public function getCallback(); public function setData($data); diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 00000000..c6f1043a --- /dev/null +++ b/src/functions.php @@ -0,0 +1,30 @@ +loop = $this->createLoop(); + l\register($this->loop); } abstract public function createLoop(); @@ -235,8 +238,7 @@ public function testNextTick() { $called = false; - $callback = function ($loop) use (&$called) { - $this->assertSame($this->loop, $loop); + $callback = function () use (&$called) { $called = true; }; @@ -359,8 +361,7 @@ public function testFutureTick() { $called = false; - $callback = function ($loop) use (&$called) { - $this->assertSame($this->loop, $loop); + $callback = function () use (&$called) { $called = true; }; diff --git a/tests/Timer/AbstractTimerTest.php b/tests/Timer/AbstractTimerTest.php index bb26f52e..62274afc 100644 --- a/tests/Timer/AbstractTimerTest.php +++ b/tests/Timer/AbstractTimerTest.php @@ -4,6 +4,7 @@ use React\Tests\EventLoop\TestCase; use React\EventLoop\Timer\Timers; +use React\EventLoop as l; abstract class AbstractTimerTest extends TestCase { @@ -14,6 +15,7 @@ public function testAddTimer() // usleep is intentionally high $loop = $this->createLoop(); + l\register($loop); $loop->addTimer(0.001, $this->expectCallableOnce()); usleep(1000); @@ -23,6 +25,7 @@ public function testAddTimer() public function testAddPeriodicTimer() { $loop = $this->createLoop(); + l\register($loop); $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(3)); usleep(1000); @@ -36,6 +39,7 @@ public function testAddPeriodicTimer() public function testAddPeriodicTimerWithCancel() { $loop = $this->createLoop(); + l\register($loop); $timer = $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(2)); @@ -55,6 +59,7 @@ public function testAddPeriodicTimerCancelsItself() $i = 0; $loop = $this->createLoop(); + l\register($loop); $loop->addPeriodicTimer(0.001, function ($timer) use (&$i) { $i++; @@ -77,6 +82,7 @@ public function testAddPeriodicTimerCancelsItself() public function testIsTimerActive() { $loop = $this->createLoop(); + l\register($loop); $timer = $loop->addPeriodicTimer(0.001, function () {}); @@ -90,6 +96,7 @@ public function testIsTimerActive() public function testMinimumIntervalOneMicrosecond() { $loop = $this->createLoop(); + l\register($loop); $timer = $loop->addTimer(0, function () {});