22
33namespace Clue \React \Block ;
44
5+ use React \EventLoop \Loop ;
56use React \EventLoop \LoopInterface ;
6- use React \Promise \PromiseInterface ;
7- use React \Promise \CancellablePromiseInterface ;
8- use UnderflowException ;
9- use Exception ;
107use React \Promise ;
8+ use React \Promise \CancellablePromiseInterface ;
9+ use React \Promise \PromiseInterface ;
1110use React \Promise \Timer ;
1211use React \Promise \Timer \TimeoutException ;
12+ use Exception ;
13+ use UnderflowException ;
1314
1415/**
1516 * Wait/sleep for `$time` seconds.
2829 * really small (or negative) value, it will still start a timer and will thus
2930 * trigger at the earliest possible time in the future.
3031 *
32+ * This function takes an optional `LoopInterface|null $loop` parameter that can be used to
33+ * pass the event loop instance to use. You can use a `null` value here in order to
34+ * use the [default loop](https://github.com/reactphp/event-loop#loop). This value
35+ * SHOULD NOT be given unless you're sure you want to explicitly use a given event
36+ * loop instance.
37+ *
3138 * @param float $time
32- * @param LoopInterface $loop
39+ * @param ? LoopInterface $loop
3340 * @return void
3441 */
35- function sleep ($ time , LoopInterface $ loop )
42+ function sleep ($ time , LoopInterface $ loop = null )
3643{
3744 await (Timer \resolve ($ time , $ loop ), $ loop );
3845}
@@ -68,6 +75,12 @@ function sleep($time, LoopInterface $loop)
6875 *
6976 * See also the [examples](../examples/).
7077 *
78+ * This function takes an optional `LoopInterface|null $loop` parameter that can be used to
79+ * pass the event loop instance to use. You can use a `null` value here in order to
80+ * use the [default loop](https://github.com/reactphp/event-loop#loop). This value
81+ * SHOULD NOT be given unless you're sure you want to explicitly use a given event
82+ * loop instance.
83+ *
7184 * If no `$timeout` argument is given and the promise stays pending, then this
7285 * will potentially wait/block forever until the promise is settled. To avoid
7386 * this, API authors creating promises are expected to provide means to
@@ -80,18 +93,19 @@ function sleep($time, LoopInterface $loop)
8093 * start a timer and will thus trigger at the earliest possible time in the future.
8194 *
8295 * @param PromiseInterface $promise
83- * @param LoopInterface $loop
84- * @param null| float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
96+ * @param ? LoopInterface $loop
97+ * @param ? float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
8598 * @return mixed returns whatever the promise resolves to
8699 * @throws Exception when the promise is rejected
87100 * @throws TimeoutException if the $timeout is given and triggers
88101 */
89- function await (PromiseInterface $ promise , LoopInterface $ loop , $ timeout = null )
102+ function await (PromiseInterface $ promise , LoopInterface $ loop = null , $ timeout = null )
90103{
91104 $ wait = true ;
92105 $ resolved = null ;
93106 $ exception = null ;
94107 $ rejected = false ;
108+ $ loop = $ loop ?: Loop::get ();
95109
96110 if ($ timeout !== null ) {
97111 $ promise = Timer \timeout ($ promise , $ timeout , $ loop );
@@ -164,6 +178,12 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
164178 * Once ALL promises reject, this function will fail and throw an `UnderflowException`.
165179 * Likewise, this will throw if an empty array of `$promises` is passed.
166180 *
181+ * This function takes an optional `LoopInterface|null $loop` parameter that can be used to
182+ * pass the event loop instance to use. You can use a `null` value here in order to
183+ * use the [default loop](https://github.com/reactphp/event-loop#loop). This value
184+ * SHOULD NOT be given unless you're sure you want to explicitly use a given event
185+ * loop instance.
186+ *
167187 * If no `$timeout` argument is given and ALL promises stay pending, then this
168188 * will potentially wait/block forever until the promise is fulfilled. To avoid
169189 * this, API authors creating promises are expected to provide means to
@@ -176,14 +196,14 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
176196 * value, it will still start a timer and will thus trigger at the earliest
177197 * possible time in the future.
178198 *
179- * @param array $promises
180- * @param LoopInterface $loop
181- * @param null| float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
199+ * @param array $promises
200+ * @param ? LoopInterface $loop
201+ * @param ? float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
182202 * @return mixed returns whatever the first promise resolves to
183203 * @throws Exception if ALL promises are rejected
184204 * @throws TimeoutException if the $timeout is given and triggers
185205 */
186- function awaitAny (array $ promises , LoopInterface $ loop , $ timeout = null )
206+ function awaitAny (array $ promises , LoopInterface $ loop = null , $ timeout = null )
187207{
188208 // Explicitly overwrite argument with null value. This ensure that this
189209 // argument does not show up in the stack trace in PHP 7+ only.
@@ -249,6 +269,12 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
249269 * and throw an `Exception`. If the promise did not reject with an `Exception`,
250270 * then this function will throw an `UnexpectedValueException` instead.
251271 *
272+ * This function takes an optional `LoopInterface|null $loop` parameter that can be used to
273+ * pass the event loop instance to use. You can use a `null` value here in order to
274+ * use the [default loop](https://github.com/reactphp/event-loop#loop). This value
275+ * SHOULD NOT be given unless you're sure you want to explicitly use a given event
276+ * loop instance.
277+ *
252278 * If no `$timeout` argument is given and ANY promises stay pending, then this
253279 * will potentially wait/block forever until the promise is fulfilled. To avoid
254280 * this, API authors creating promises are expected to provide means to
@@ -261,14 +287,14 @@ function awaitAny(array $promises, LoopInterface $loop, $timeout = null)
261287 * value, it will still start a timer and will thus trigger at the earliest
262288 * possible time in the future.
263289 *
264- * @param array $promises
265- * @param LoopInterface $loop
266- * @param null| float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
290+ * @param array $promises
291+ * @param ? LoopInterface $loop
292+ * @param ? float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
267293 * @return array returns an array with whatever each promise resolves to
268294 * @throws Exception when ANY promise is rejected
269295 * @throws TimeoutException if the $timeout is given and triggers
270296 */
271- function awaitAll (array $ promises , LoopInterface $ loop , $ timeout = null )
297+ function awaitAll (array $ promises , LoopInterface $ loop = null , $ timeout = null )
272298{
273299 // Explicitly overwrite argument with null value. This ensure that this
274300 // argument does not show up in the stack trace in PHP 7+ only.
0 commit comments