|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Block; |
| 4 | + |
| 5 | +use React\EventLoop\LoopInterface; |
| 6 | +use React\Promise\PromiseInterface; |
| 7 | +use React\Promise\CancellablePromiseInterface; |
| 8 | +use UnderflowException; |
| 9 | +use Exception; |
| 10 | + |
| 11 | +class Blocker |
| 12 | +{ |
| 13 | + private $loop; |
| 14 | + |
| 15 | + public function __construct(LoopInterface $loop) |
| 16 | + { |
| 17 | + $this->loop = $loop; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * wait/sleep for $time seconds |
| 22 | + * |
| 23 | + * @param float $time |
| 24 | + */ |
| 25 | + public function wait($time) |
| 26 | + { |
| 27 | + $loop = $this->loop; |
| 28 | + $loop->addTimer($time, function () use ($loop) { |
| 29 | + $loop->stop(); |
| 30 | + }); |
| 31 | + $loop->run(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * block waiting for the given $promise to resolve |
| 36 | + * |
| 37 | + * @param PromiseInterface $promise |
| 38 | + * @return mixed returns whatever the promise resolves to |
| 39 | + * @throws Exception when the promise is rejected |
| 40 | + */ |
| 41 | + public function awaitOne(PromiseInterface $promise) |
| 42 | + { |
| 43 | + $resolved = null; |
| 44 | + $exception = null; |
| 45 | + |
| 46 | + $promise->then( |
| 47 | + function ($c) use (&$resolved) { |
| 48 | + $resolved = $c; |
| 49 | + }, |
| 50 | + function ($error) use (&$exception) { |
| 51 | + $exception = $error; |
| 52 | + } |
| 53 | + ); |
| 54 | + |
| 55 | + while ($resolved === null && $exception === null) { |
| 56 | + $this->loop->tick(); |
| 57 | + } |
| 58 | + |
| 59 | + if ($exception !== null) { |
| 60 | + throw $exception; |
| 61 | + } |
| 62 | + |
| 63 | + return $resolved; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * wait for ANY of the given promises to resolve |
| 68 | + * |
| 69 | + * Once the first promise is resolved, this will try to cancel() all |
| 70 | + * remaining promises and return whatever the first promise resolves to. |
| 71 | + * |
| 72 | + * If ALL promises fail to resolve, this will fail and throw an Exception. |
| 73 | + * |
| 74 | + * @param array $promises |
| 75 | + * @return mixed returns whatever the first promise resolves to |
| 76 | + * @throws Exception if ALL promises are rejected |
| 77 | + */ |
| 78 | + public function awaitRace(array $promises) |
| 79 | + { |
| 80 | + $wait = count($promises); |
| 81 | + $value = null; |
| 82 | + $success = false; |
| 83 | + |
| 84 | + foreach ($promises as $key => $promise) { |
| 85 | + /* @var $promise PromiseInterface */ |
| 86 | + $promise->then( |
| 87 | + function ($return) use (&$value, &$wait, &$success, $promises) { |
| 88 | + if (!$wait) { |
| 89 | + // only store first promise value |
| 90 | + return; |
| 91 | + } |
| 92 | + $value = $return; |
| 93 | + $wait = 0; |
| 94 | + $success = true; |
| 95 | + |
| 96 | + // cancel all remaining promises |
| 97 | + foreach ($promises as $promise) { |
| 98 | + if ($promise instanceof CancellablePromiseInterface) { |
| 99 | + $promise->cancel(); |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | + function ($e) use (&$wait) { |
| 104 | + if ($wait) { |
| 105 | + // count number of promises to await |
| 106 | + // cancelling promises will reject all remaining ones, ignore this |
| 107 | + --$wait; |
| 108 | + } |
| 109 | + } |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + while ($wait) { |
| 114 | + $this->loop->tick(); |
| 115 | + } |
| 116 | + |
| 117 | + if (!$success) { |
| 118 | + throw new UnderflowException('No promise could resolve'); |
| 119 | + } |
| 120 | + |
| 121 | + return $value; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * wait for ALL of the given promises to resolve |
| 126 | + * |
| 127 | + * Once the last promise resolves, this will return an array with whatever |
| 128 | + * each promise resolves to. Array keys will be left intact, i.e. they can |
| 129 | + * be used to correlate the return array to the promises passed. |
| 130 | + * |
| 131 | + * If ANY promise fails to resolve, this will try to cancel() all |
| 132 | + * remaining promises and throw an Exception. |
| 133 | + * |
| 134 | + * @param array $promises |
| 135 | + * @return array returns an array with whatever each promise resolves to |
| 136 | + * @throws Exception when ANY promise is rejected |
| 137 | + */ |
| 138 | + public function awaitAll(array $promises) |
| 139 | + { |
| 140 | + $wait = count($promises); |
| 141 | + $exception = null; |
| 142 | + $values = array(); |
| 143 | + |
| 144 | + foreach ($promises as $key => $promise) { |
| 145 | + /* @var $promise PromiseInterface */ |
| 146 | + $promise->then( |
| 147 | + function ($value) use (&$values, $key, &$wait) { |
| 148 | + $values[$key] = $value; |
| 149 | + --$wait; |
| 150 | + }, |
| 151 | + function ($e) use ($promises, &$exception, &$wait) { |
| 152 | + if (!$wait) { |
| 153 | + // cancelling promises will reject all remaining ones, only store first error |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + $exception = $e; |
| 158 | + $wait = 0; |
| 159 | + |
| 160 | + // cancel all remaining promises |
| 161 | + foreach ($promises as $promise) { |
| 162 | + if ($promise instanceof CancellablePromiseInterface) { |
| 163 | + $promise->cancel(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + while ($wait) { |
| 171 | + $this->loop->tick(); |
| 172 | + } |
| 173 | + |
| 174 | + if ($exception !== null) { |
| 175 | + throw $exception; |
| 176 | + } |
| 177 | + |
| 178 | + return $values; |
| 179 | + } |
| 180 | +} |
0 commit comments