22
33namespace React \EventLoop ;
44
5+ use React \EventLoop \Tick \FutureTickQueue ;
56use React \EventLoop \Tick \NextTickQueue ;
67use React \EventLoop \Timer \Timer ;
78use React \EventLoop \Timer \TimerInterface ;
1213 */
1314class StreamSelectLoop implements LoopInterface
1415{
16+ const MICROSECONDS_PER_SECOND = 1000000 ;
17+
1518 private $ nextTickQueue ;
19+ private $ futureTickQueue ;
1620 private $ timers ;
1721 private $ readStreams = [];
1822 private $ readListeners = [];
@@ -23,6 +27,7 @@ class StreamSelectLoop implements LoopInterface
2327 public function __construct ()
2428 {
2529 $ this ->nextTickQueue = new NextTickQueue ($ this );
30+ $ this ->futureTickQueue = new FutureTickQueue ($ this );
2631 $ this ->timers = new Timers ();
2732 }
2833
@@ -135,13 +140,23 @@ public function nextTick(callable $listener)
135140 $ this ->nextTickQueue ->add ($ listener );
136141 }
137142
143+ /**
144+ * {@inheritdoc}
145+ */
146+ public function futureTick (callable $ listener )
147+ {
148+ $ this ->futureTickQueue ->add ($ listener );
149+ }
150+
138151 /**
139152 * {@inheritdoc}
140153 */
141154 public function tick ()
142155 {
143156 $ this ->nextTickQueue ->tick ();
144157
158+ $ this ->futureTickQueue ->tick ();
159+
145160 $ this ->timers ->tick ();
146161
147162 $ this ->waitForStreamActivity (0 );
@@ -157,10 +172,12 @@ public function run()
157172 while ($ this ->running ) {
158173 $ this ->nextTickQueue ->tick ();
159174
175+ $ this ->futureTickQueue ->tick ();
176+
160177 $ this ->timers ->tick ();
161178
162- // Timers have placed more items on the next-tick queue ...
163- if (!$ this ->nextTickQueue ->isEmpty ()) {
179+ // Next-tick or future-tick queues have pending callbacks ...
180+ if (!$ this ->running || ! $ this -> nextTickQueue -> isEmpty () || ! $ this -> futureTickQueue ->isEmpty ()) {
164181 $ timeout = 0 ;
165182
166183 // There is a pending timer, only block until it is due ...
@@ -178,7 +195,7 @@ public function run()
178195 break ;
179196 }
180197
181- $ this ->waitForStreamActivity ($ timeout );
198+ $ this ->waitForStreamActivity ($ timeout * self :: MICROSECONDS_PER_SECOND );
182199 }
183200 }
184201
0 commit comments