PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / react / Promise / CancellationQueue.php

CancellationQueue.php in DecaLog 4.4.0, at includes/libraries/react/Promise/CancellationQueue.php

56 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace React\Promise;
4
5 class CancellationQueue
6 {
7 private $started = false;
8 private $queue = [];
9
10 public function __invoke()
11 {
12 if ($this->started) {
13 return;
14 }
15
16 $this->started = true;
17 $this->drain();
18 }
19
20 public function enqueue($cancellable)
21 {
22 if (!\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
23 return;
24 }
25
26 $length = \array_push($this->queue, $cancellable);
27
28 if ($this->started && 1 === $length) {
29 $this->drain();
30 }
31 }
32
33 private function drain()
34 {
35 for ($i = key($this->queue); isset($this->queue[$i]); $i++) {
36 $cancellable = $this->queue[$i];
37
38 $exception = null;
39
40 try {
41 $cancellable->cancel();
42 } catch (\Throwable $exception) {
43 } catch (\Exception $exception) {
44 }
45
46 unset($this->queue[$i]);
47
48 if ($exception) {
49 throw $exception;
50 }
51 }
52
53 $this->queue = [];
54 }
55 }
56