|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the ReactPHP WebDriver <https://github.com/itnelo/reactphp-webdriver>. |
| 5 | + * |
| 6 | + * (c) 2020 Pavel Petrov <itnelo@gmail.com>. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @license https://opensource.org/licenses/mit MIT |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace Itnelo\React\WebDriver\Routine\Condition; |
| 17 | + |
| 18 | +use Itnelo\React\WebDriver\Timeout\Interceptor as TimeoutInterceptor; |
| 19 | +use LogicException; |
| 20 | +use React\EventLoop\LoopInterface; |
| 21 | +use React\EventLoop\TimerInterface; |
| 22 | +use React\Promise\Deferred; |
| 23 | +use React\Promise\PromiseInterface; |
| 24 | +use RuntimeException; |
| 25 | +use Throwable; |
| 26 | + |
| 27 | +/** |
| 28 | + * Runs periodic condition checks for the web driver, using the specified event loop instance |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +final class CheckRoutine |
| 33 | +{ |
| 34 | + /** |
| 35 | + * Event loop reference |
| 36 | + * |
| 37 | + * @var LoopInterface |
| 38 | + */ |
| 39 | + private LoopInterface $loop; |
| 40 | + |
| 41 | + /** |
| 42 | + * Cancels a driver promise if it isn't resolved for too long |
| 43 | + * |
| 44 | + * @var TimeoutInterceptor |
| 45 | + */ |
| 46 | + private TimeoutInterceptor $timeoutInterceptor; |
| 47 | + |
| 48 | + /** |
| 49 | + * A timer, registered in the event loop, which executes routine logic |
| 50 | + * |
| 51 | + * @var TimerInterface|null |
| 52 | + */ |
| 53 | + private ?TimerInterface $_evaluationTimer; |
| 54 | + |
| 55 | + /** |
| 56 | + * A flag that is set, when the routine already waits promise result from the next check iteration |
| 57 | + * |
| 58 | + * @var bool |
| 59 | + */ |
| 60 | + private bool $_isEvaluating; |
| 61 | + |
| 62 | + /** |
| 63 | + * CheckRoutine constructor. |
| 64 | + * |
| 65 | + * @param LoopInterface $loop Event loop reference |
| 66 | + * @param TimeoutInterceptor $timeoutInterceptor Cancels a driver promise if it isn't resolved for too long |
| 67 | + */ |
| 68 | + public function __construct(LoopInterface $loop, TimeoutInterceptor $timeoutInterceptor) |
| 69 | + { |
| 70 | + $this->loop = $loop; |
| 71 | + $this->timeoutInterceptor = $timeoutInterceptor; |
| 72 | + |
| 73 | + $this->_evaluationTimer = null; |
| 74 | + $this->_isEvaluating = false; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Runs a routine logic in the loop timer and returns a promise, which will be resolved when the condition is met |
| 79 | + * |
| 80 | + * @param callable $conditionMetCallback A condition to be met, as a callback |
| 81 | + * @param float $checkInterval The interval for condition checks, in seconds |
| 82 | + * |
| 83 | + * @return PromiseInterface<null> |
| 84 | + * |
| 85 | + * @throws RuntimeException Whenever a routine is already is the running state |
| 86 | + */ |
| 87 | + public function run(callable $conditionMetCallback, float $checkInterval = 0.5): PromiseInterface |
| 88 | + { |
| 89 | + if ($this->_evaluationTimer instanceof TimerInterface) { |
| 90 | + throw new RuntimeException('Routine is already running.'); |
| 91 | + } |
| 92 | + |
| 93 | + return $this->runInternal($conditionMetCallback, $checkInterval); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns a promise that will be resolved when the check routine successfully evaluates a given callback |
| 98 | + * |
| 99 | + * @param callable $conditionMetCallback A condition to be met, as a callback |
| 100 | + * @param float $checkInterval The interval for condition checks, in seconds |
| 101 | + * |
| 102 | + * @return PromiseInterface<null> |
| 103 | + */ |
| 104 | + private function runInternal(callable $conditionMetCallback, float $checkInterval): PromiseInterface |
| 105 | + { |
| 106 | + $evaluationDeferred = new Deferred(); |
| 107 | + |
| 108 | + $evaluationLogic = function () use ($evaluationDeferred, $conditionMetCallback) { |
| 109 | + // do not try to evaluate a condition if a previous result promise was not resolved/rejected. |
| 110 | + if (false !== $this->_isEvaluating) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + // receiving a new result promise from the condition callback. |
| 116 | + $resultPromise = $conditionMetCallback(); |
| 117 | + |
| 118 | + if (!$resultPromise instanceof PromiseInterface) { |
| 119 | + $invalidPromiseExceptionMessage = sprintf( |
| 120 | + 'Return value from the condition callable must be an instance of %s.', |
| 121 | + PromiseInterface::class |
| 122 | + ); |
| 123 | + |
| 124 | + throw new LogicException($invalidPromiseExceptionMessage); |
| 125 | + } |
| 126 | + |
| 127 | + // handling evaluation results. |
| 128 | + $this->_isEvaluating = true; |
| 129 | + $resultPromise->then( |
| 130 | + function () use ($evaluationDeferred) { |
| 131 | + // at some point, a promise has been successfully resolved. |
| 132 | + $evaluationDeferred->resolve(null); |
| 133 | + }, |
| 134 | + function (Throwable $rejectionReason) { |
| 135 | + // signals that we can take another promise from the condition callback to continue our checks. |
| 136 | + $this->_isEvaluating = false; |
| 137 | + } |
| 138 | + ); |
| 139 | + } catch (Throwable $exception) { |
| 140 | + $reason = new RuntimeException('Unable to evaluate a condition callback.', 0, $exception); |
| 141 | + |
| 142 | + $evaluationDeferred->reject($reason); |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + // registering a periodic timer in the event loop. |
| 147 | + $this->_evaluationTimer = $this->loop->addPeriodicTimer($checkInterval, $evaluationLogic); |
| 148 | + |
| 149 | + $conditionMetPromise = $evaluationDeferred->promise(); |
| 150 | + |
| 151 | + $conditionMetTimedPromise = $this->timeoutInterceptor->applyTimeout( |
| 152 | + $conditionMetPromise, |
| 153 | + 'A condition is not met within the specified amount of time.' |
| 154 | + ); |
| 155 | + |
| 156 | + return $conditionMetTimedPromise->then( |
| 157 | + function () { |
| 158 | + // cleaning up a related timer with condition-check logic. |
| 159 | + $this->loop->cancelTimer($this->_evaluationTimer); |
| 160 | + |
| 161 | + return null; |
| 162 | + }, |
| 163 | + function (Throwable $rejectionReason) { |
| 164 | + $this->loop->cancelTimer($this->_evaluationTimer); |
| 165 | + |
| 166 | + throw $rejectionReason; |
| 167 | + } |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments