|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | use Psr\Http\Message\ServerRequestInterface;
|
| 4 | +use React\EventLoop\Loop; |
| 5 | +use React\Promise\Promise; |
| 6 | +use React\Promise\PromiseInterface; |
4 | 7 | use React\Stream\ThroughStream;
|
5 | 8 |
|
6 | 9 | // example uses `@include` for test suite only, real-world examples should use `require` instead
|
7 | 10 | if(!@include __DIR__ . '/../vendor/autoload.php') {
|
8 | 11 | require __DIR__ . '/../../../vendor/autoload.php';
|
9 | 12 | }
|
10 | 13 |
|
| 14 | +/** |
| 15 | + * basic "async sleep" to test deferred responses |
| 16 | + * |
| 17 | + * @return PromiseInterface<void> |
| 18 | + * @link https://github.com/reactphp/async#delay |
| 19 | + * @link https://github.com/reactphp/promise-timer#sleep |
| 20 | + */ |
| 21 | +function asleep(float $s): PromiseInterface |
| 22 | +{ |
| 23 | + /** @var PromiseInterface<void> */ |
| 24 | + return new Promise(function (callable $resolve) use ($s): void { // @phpstan-ignore-line for legacy PHP 7.1 |
| 25 | + Loop::addTimer($s, function () use ($resolve): void { $resolve(null); }); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
11 | 29 | $app = new FrameworkX\App();
|
12 | 30 |
|
13 | 31 | $app->get('/', function () {
|
|
31 | 49 | });
|
32 | 50 |
|
33 | 51 | $app->get('/sleep/fiber', function () {
|
34 |
| - React\Async\await(React\Promise\Timer\sleep(0.1)); |
| 52 | + React\Async\delay(0.1); // React\Async\await(asleep(0.1)); |
35 | 53 | return React\Http\Message\Response::plaintext("OK\n");
|
36 | 54 | });
|
37 | 55 | $app->get('/sleep/coroutine', function () {
|
38 |
| - yield React\Promise\Timer\sleep(0.1); |
| 56 | + yield asleep(0.1); |
39 | 57 | return React\Http\Message\Response::plaintext("OK\n");
|
40 | 58 | });
|
41 | 59 | $app->get('/sleep/promise', function () {
|
42 |
| - return React\Promise\Timer\sleep(0.1)->then(function () { |
| 60 | + return asleep(0.1)->then(function () { |
43 | 61 | return React\Http\Message\Response::plaintext("OK\n");
|
44 | 62 | });
|
45 | 63 | });
|
|
0 commit comments