|
2 | 2 |
|
3 | 3 | namespace React\Async;
|
4 | 4 |
|
| 5 | +use React\EventLoop\Loop; |
| 6 | +use React\EventLoop\TimerInterface; |
5 | 7 | use React\Promise\Deferred;
|
6 | 8 | use React\Promise\Promise;
|
7 | 9 | use React\Promise\PromiseInterface;
|
|
153 | 155 | * The returned promise is implemented in such a way that it can be cancelled
|
154 | 156 | * when it is still pending. Cancelling a pending promise will cancel any awaited
|
155 | 157 | * promises inside that fiber or any nested fibers. As such, the following example
|
156 |
| - * will only output `ab` and cancel the pending [`sleep()`](https://reactphp.org/promise-timer/#sleep). |
| 158 | + * will only output `ab` and cancel the pending [`delay()`](#delay). |
157 | 159 | * The [`await()`](#await) calls in this example would throw a `RuntimeException`
|
158 |
| - * from the cancelled [`sleep()`](https://reactphp.org/promise-timer/#sleep) call |
159 |
| - * that bubbles up through the fibers. |
| 160 | + * from the cancelled [`delay()`](#delay) call that bubbles up through the fibers. |
160 | 161 | *
|
161 | 162 | * ```php
|
162 | 163 | * $promise = async(static function (): int {
|
163 | 164 | * echo 'a';
|
164 | 165 | * await(async(static function (): void {
|
165 | 166 | * echo 'b';
|
166 |
| - * await(React\Promise\Timer\sleep(2)); |
| 167 | + * delay(2); |
167 | 168 | * echo 'c';
|
168 | 169 | * })());
|
169 | 170 | * echo 'd';
|
@@ -215,7 +216,6 @@ function async(callable $function): callable
|
215 | 216 | };
|
216 | 217 | }
|
217 | 218 |
|
218 |
| - |
219 | 219 | /**
|
220 | 220 | * Block waiting for the given `$promise` to be fulfilled.
|
221 | 221 | *
|
@@ -352,6 +352,127 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber, $lowL
|
352 | 352 | return $fiber->suspend();
|
353 | 353 | }
|
354 | 354 |
|
| 355 | +/** |
| 356 | + * Delay program execution for duration given in `$seconds`. |
| 357 | + * |
| 358 | + * ```php |
| 359 | + * React\Async\delay($seconds); |
| 360 | + * ``` |
| 361 | + * |
| 362 | + * This function will only return after the given number of `$seconds` have |
| 363 | + * elapsed. If there are no other events attached to this loop, it will behave |
| 364 | + * similar to PHP's [`sleep()` function](https://www.php.net/manual/en/function.sleep.php). |
| 365 | + * |
| 366 | + * ```php |
| 367 | + * echo 'a'; |
| 368 | + * React\Async\delay(1.0); |
| 369 | + * echo 'b'; |
| 370 | + * |
| 371 | + * // prints "a" at t=0.0s |
| 372 | + * // prints "b" at t=1.0s |
| 373 | + * ``` |
| 374 | + * |
| 375 | + * Unlike PHP's [`sleep()` function](https://www.php.net/manual/en/function.sleep.php), |
| 376 | + * this function may not necessarily halt execution of the entire process thread. |
| 377 | + * Instead, it allows the event loop to run any other events attached to the |
| 378 | + * same loop until the delay returns: |
| 379 | + * |
| 380 | + * ```php |
| 381 | + * echo 'a'; |
| 382 | + * Loop::addTimer(1.0, function () { |
| 383 | + * echo 'b'; |
| 384 | + * }); |
| 385 | + * React\Async\delay(3.0); |
| 386 | + * echo 'c'; |
| 387 | + * |
| 388 | + * // prints "a" at t=0.0s |
| 389 | + * // prints "b" at t=1.0s |
| 390 | + * // prints "c" at t=3.0s |
| 391 | + * ``` |
| 392 | + * |
| 393 | + * This behavior is especially useful if you want to delay the program execution |
| 394 | + * of a particular routine, such as when building a simple polling or retry |
| 395 | + * mechanism: |
| 396 | + * |
| 397 | + * ```php |
| 398 | + * try { |
| 399 | + * something(); |
| 400 | + * } catch (Throwable) { |
| 401 | + * // in case of error, retry after a short delay |
| 402 | + * React\Async\delay(1.0); |
| 403 | + * something(); |
| 404 | + * } |
| 405 | + * ``` |
| 406 | + * |
| 407 | + * Because this function only returns after some time has passed, it can be |
| 408 | + * considered *blocking* from the perspective of the calling code. You can avoid |
| 409 | + * this blocking behavior by wrapping it in an [`async()` function](#async) call. |
| 410 | + * Everything inside this function will still be blocked, but everything outside |
| 411 | + * this function can be executed asynchronously without blocking: |
| 412 | + * |
| 413 | + * ```php |
| 414 | + * Loop::addTimer(0.5, React\Async\async(function () { |
| 415 | + * echo 'a'; |
| 416 | + * React\Async\delay(1.0); |
| 417 | + * echo 'c'; |
| 418 | + * })); |
| 419 | + * |
| 420 | + * Loop::addTimer(1.0, function () { |
| 421 | + * echo 'b'; |
| 422 | + * }); |
| 423 | + * |
| 424 | + * // prints "a" at t=0.5s |
| 425 | + * // prints "b" at t=1.0s |
| 426 | + * // prints "c" at t=1.5s |
| 427 | + * ``` |
| 428 | + * |
| 429 | + * See also the [`async()` function](#async) for more details. |
| 430 | + * |
| 431 | + * Internally, the `$seconds` argument will be used as a timer for the loop so that |
| 432 | + * it keeps running until this timer triggers. This implies that if you pass a |
| 433 | + * really small (or negative) value, it will still start a timer and will thus |
| 434 | + * trigger at the earliest possible time in the future. |
| 435 | + * |
| 436 | + * The function is implemented in such a way that it can be cancelled when it is |
| 437 | + * running inside an [`async()` function](#async). Cancelling the resulting |
| 438 | + * promise will clean up any pending timers and throw a `RuntimeException` from |
| 439 | + * the pending delay which in turn would reject the resulting promise. |
| 440 | + * |
| 441 | + * ```php |
| 442 | + * $promise = async(function () { |
| 443 | + * echo 'a'; |
| 444 | + * delay(3.0); |
| 445 | + * echo 'b'; |
| 446 | + * }); |
| 447 | + * |
| 448 | + * Loop::addTimer(2.0, function () use ($promise) { |
| 449 | + * $promise->cancel(); |
| 450 | + * }); |
| 451 | + * |
| 452 | + * // prints "a" at t=0.0s |
| 453 | + * // rejects $promise at t=2.0 |
| 454 | + * // never prints "b" |
| 455 | + * ``` |
| 456 | + * |
| 457 | + * @return void |
| 458 | + * @throws \RuntimeException when the function is cancelled inside an `async()` function |
| 459 | + * @see async() |
| 460 | + * @uses await() |
| 461 | + */ |
| 462 | +function delay(float $seconds): void |
| 463 | +{ |
| 464 | + /** @var ?TimerInterface $timer */ |
| 465 | + $timer = null; |
| 466 | + |
| 467 | + await(new Promise(function (callable $resolve) use ($seconds, &$timer): void { |
| 468 | + $timer = Loop::addTimer($seconds, fn() => $resolve(null)); |
| 469 | + }, function () use (&$timer): void { |
| 470 | + assert($timer instanceof TimerInterface); |
| 471 | + Loop::cancelTimer($timer); |
| 472 | + throw new \RuntimeException('Delay cancelled'); |
| 473 | + })); |
| 474 | +} |
| 475 | + |
355 | 476 | /**
|
356 | 477 | * Execute a Generator-based coroutine to "await" promises.
|
357 | 478 | *
|
|
0 commit comments