8000 Removed NextTickLoopInterface, AbstractNextTickLoop and renamed Strea… · reactphp/event-loop@3028fca · GitHub
[go: up one dir, main page]

Skip to content

Commit 3028fca

Browse files
committed
Removed NextTickLoopInterface, AbstractNextTickLoop and renamed StreamSelectNextTickLoop to StreamSelectLoop.
1 parent 35e386c commit 3028fca

8 files changed

+270
-481
lines changed

AbstractNextTickLoop.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

ExtEventLoop.php

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44

55
use Event;
66
use EventBase;
7+
use React\EventLoop\NextTick\NextTickQueue;
78
use React\EventLoop\Timer\Timer;
89
use React\EventLoop\Timer\TimerInterface;
910
use SplObjectStorage;
1011
use stdClass;
1112

1213
/**
13-
* An ext-event based event-loop with support for nextTick().
14+
* An ext-event based event-loop.
1415
*/
15-
class ExtEventLoop extends AbstractNextTickLoop
16+
class ExtEventLoop implements LoopInterface
1617
{
1718
private $eventBase;
19+
private $nextTickQueue;
1820
private $timerEvents;
1921
private $streamEvents;
22+
private $running;
2023
private $keepAlive;
2124

2225
/**
@@ -29,6 +32,7 @@ public function __construct(EventBase $eventBase = null)
2932
}
3033

3134
$this->eventBase = $eventBase;
35+
$this->nextTickQueue = new NextTickQueue($this);
3236
$this->timerEvents = new SplObjectStorage;
3337
$this->streamEvents = [];
3438

@@ -37,8 +41,6 @@ public function __construct(EventBase $eventBase = null)
3741
// to prevent the PHP fatal error caused by ext-event:
3842
// "Cannot destroy active lambda function"
3943
$this->keepAlive = [];
40-
41-
parent::__construct();
4244
}
4345

4446
/**
@@ -176,32 +178,64 @@ public function isTimerActive(TimerInterface $timer)
176178
}
177179

178180
/**
179-
* Flush any timer and IO events.
181+
* Schedule a callback to be invoked on the next tick of the event loop.
180182
*
181-
* @param boolean $blocking True if loop should block waiting for next event.
183+
* Callbacks are guaranteed to be executed in the order they are enqueued,
184+
* before any timer or stream events.
185+
*
186+
* @param callable $listener The callback to invoke.
182187
*/
183-
protected function tickLogic($blocking)
188+
public function nextTick(callable $listener)
184189
{
185-
$flags = EventBase::LOOP_ONCE;
190+
$this->nextTickQueue->add($listener);
191+
}
186192

187-
if (!$blocking) {
188-
$flags |= EventBase::LOOP_NONBLOCK;
189-
}
193+
/**
194+
* Perform a single iteration of the event loop.
195+
*
196+
* @param boolean $blocking True if loop should block waiting for next event.
197+
*/
198+
public function tick()
199+
{
200+
$this->nextTickQueue->tick();
190201

191-
$this->eventBase->loop($flags);
202+
$this->eventBase->loop(EventBase::LOOP_ONCE | EventBase::LOOP_NONBLOCK);
192203

193204
$this->keepAlive = [];
194205
}
195206

196207
/**
197-
* Check if the loop has any pending timers or streams.
198-
*
199-
* @return boolean
208+
* Run the event loop until there are no more tasks to perform.
209+
*/
210+
public function run()
211+
{
212+
$this->running = true;
213+
214+
while ($this->running) {
215+
216+
if (
217+
!$this->streamEvents
218+
&& !$this->timerEvents->count()
219+
&& $this->nextTickQueue->isEmpty()
220+
) {
221+
break;
222+
}
223+
224+
$this->nextTickQueue->tick();
225+
226+
$this->eventBase->loop(EventBase::LOOP_ONCE);
227+
228+
$this->keepAlive = [];
229+
230+
}
231+
}
232+
233+
/**
234+
* Instruct a running event loop to stop.
200235
*/
201-
protected function isEmpty()
236+
public function stop()
202237
{
203-
return 0 === count($this->timerEvents)
204-
&& 0 === count($this->streamEvents);
238+
$this->running = false;
205239
}
206240

207241
/**

LibEvLoop.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ public function removeStream($stream)
5555
$this->removeWriteStream($stream);
5656
}
5757

58+
/**
59+
* Schedule a callback to be invoked on the next tick of the event loop.
60+
*
61+
* Callbacks are guaranteed to be executed in the order they are enqueued,
62+
* before any timer or stream events.
63+
*
64+
* @param callable $listner The callback to invoke.
65+
*/
66+
public function nextTick(callable $listener)
67+
{
68+
throw new \Exception('Not yet implemented.');
69+
}
70+
5871
private function addStream($stream, $listener, $flags)
5972
{
6073
$listener = $this->wrapStreamListener($stream, $listener, $flags);

LibEventLoop.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ public function removeWriteStream($stream)
101101
$this->removeStreamEvent($stream, EV_WRITE, 'write');
102102
}
103103

104+
/**
105+
* Schedule a callback to be invoked on the next tick of the event loop.
106+
*
107+
* Callbacks are guaranteed to be executed in the order they are enqueued,
108+
* before any timer or stream events.
109+
*
110+
* @param callable $listner The callback to invoke.
111+
*/
112+
public function nextTick(callable $listener)
113+
{
114+
throw new \Exception('Not yet implemented.');
115+
}
116+
104117
protected function removeStreamEvent($stream, $eventClass, $type)
105118
{
106119
$id = (int) $stream;

LoopInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ public function cancelTimer(TimerInterface $timer);
8585
*/
8686
public function isTimerActive(TimerInterface $timer);
8787

88+
/**
89+
* Schedule a callback to be invoked on the next tick of the event loop.
90+
*
91+
* Callbacks are guaranteed to be executed in the order they are enqueued,
92+
* before any timer or stream events.
93+
*
94+
* @param callable $listner The callback to invoke.
95+
*/
96+
public function nextTick(callable $listener);
97+
8898
/**
8999
* Perform a single iteration of the event loop.
90100
*/

NextTickLoopInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0