friendship ended with social-app. php is my new best friend
1<?php
2
3namespace React\Promise\Internal;
4
5/**
6 * @internal
7 */
8final class CancellationQueue
9{
10 /** @var bool */
11 private $started = false;
12
13 /** @var object[] */
14 private $queue = [];
15
16 public function __invoke(): void
17 {
18 if ($this->started) {
19 return;
20 }
21
22 $this->started = true;
23 $this->drain();
24 }
25
26 /**
27 * @param mixed $cancellable
28 */
29 public function enqueue($cancellable): void
30 {
31 if (!\is_object($cancellable) || !\method_exists($cancellable, 'then') || !\method_exists($cancellable, 'cancel')) {
32 return;
33 }
34
35 $length = \array_push($this->queue, $cancellable);
36
37 if ($this->started && 1 === $length) {
38 $this->drain();
39 }
40 }
41
42 private function drain(): void
43 {
44 for ($i = \key($this->queue); isset($this->queue[$i]); $i++) {
45 $cancellable = $this->queue[$i];
46 assert(\method_exists($cancellable, 'cancel'));
47
48 $exception = null;
49
50 try {
51 $cancellable->cancel();
52 } catch (\Throwable $exception) {
53 }
54
55 unset($this->queue[$i]);
56
57 if ($exception) {
58 throw $exception;
59 }
60 }
61
62 $this->queue = [];
63 }
64}