Lines 96.55% 56 / 58
Methods 86.66% 13 / 15
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 4 / 4 100.00% 1 / 1 1
 deliverVia 100.00% 1 / 1 100.00% 1 / 1 1
 reportHandlerCallsTo 100.00% 1 / 1 100.00% 1 / 1 1
 wrapHandlerToReportCalls 100.00% 5 / 5 100.00% 1 / 1 2
 emit 100.00% 5 / 5 100.00% 1 / 1 2
 onTeardown 100.00% 4 / 4 100.00% 1 / 1 2
 attachedTo 100.00% 3 / 3 100.00% 1 / 1 2
 claimStage 0.00% 0 / 1 0.00% 0 / 1 2
 trackOutstanding 66.66% 2 / 3 0.00% 0 / 1 2.15
 untrackOutstanding 100.00% 2 / 2 100.00% 1 / 1 1
 closeWhenDownstreamSettles 100.00% 2 / 2 100.00% 1 / 1 1
 isActive 100.00% 1 / 1 100.00% 1 / 1 1
 close 100.00% 14 / 14 100.00% 1 / 1 5
 runEmission 100.00% 6 / 6 100.00% 1 / 1 2
 closeIfSettled 100.00% 6 / 6 100.00% 1 / 1 6
13final class SubscriptionScope
14{
15    /** @var Closure(Closure(): void): void */
16    private Closure $sink;
17
18    /** @var ?Closure(): void */
19    private ?Closure $handlerCallObserver = null;
20
21    /** @var list<Closure(): void> */
22    private array $teardowns = [];
23
24    private ?Subscription $subscription = null;
25
26    private bool $closed = false;
27
28    private int $stages = 0;
29
30    /** @var SplObjectStorage<object, int> */
31    private SplObjectStorage $outstanding;
32
33    private ?int $closeBelowStage = null;
34
35    private int $runningEmissions = 0;
36
37    public function __construct()
38    {
39        $this->sink = static function (Closure $emission): void {
40            $emission();
41        };
42        $this->outstanding = new SplObjectStorage();
43    }
44
45    /** @param Closure(Closure(): void): void $sink */
46    public function deliverVia(Closure $sink): void
47    {
48        $this->sink = $sink;
49    }
50
51    /** @param Closure(): void $observer */
52    public function reportHandlerCallsTo(Closure $observer): void
53    {
54        $this->handlerCallObserver = $observer;
55    }
56
57    // The source sets the observer only once the stream attaches, so it is read at each call.
58    /**
59     * @template T
60     * @param Closure(T): void $handler
61     * @return Closure(T): void
62     */
63    public function wrapHandlerToReportCalls(Closure $handler): Closure
64    {
65        return function (mixed $event) use ($handler): void {
66            if ($this->handlerCallObserver !== null) {
67                ($this->handlerCallObserver)();
68            }
69
70            $handler($event);
71        };
72    }
73
74    /** @param Closure(): void $emission */
75    public function emit(Closure $emission): void
76    {
77        if ($this->closed) {
78            return;
79        }
80
81        ($this->sink)(function () use ($emission): void {
82            $this->runEmission($emission);
83        });
84    }
85
86    /** @param Closure(): void $teardown */
87    public function onTeardown(Closure $teardown): void
88    {
89        if ($this->closed) {
90            $teardown();
91
92            return;
93        }
94
95        $this->teardowns[] = $teardown;
96    }
97
98    public function attachedTo(Subscription $subscription): void
99    {
100        $this->subscription = $subscription;
101
102        if ($this->closed) {
103            $subscription->unsubscribe();
104        }
105    }
106
107    public function claimStage(): int
108    {
109        return $this->stages++;
110    }
111
112    public function trackOutstanding(object $pending, int $stage): void
113    {
114        if ($this->closed) {
115            return;
116        }
117
118        $this->outstanding[$pending] = $stage;
119    }
120
121    public function untrackOutstanding(object $pending): void
122    {
123        unset($this->outstanding[$pending]);
124
125        $this->closeIfSettled();
126    }
127
128    public function closeWhenDownstreamSettles(int $stage): void
129    {
130        $this->closeBelowStage = min($this->closeBelowStage ?? $stage, $stage);
131
132        $this->closeIfSettled();
133    }
134
135    public function isActive(): bool
136    {
137        return !$this->closed;
138    }
139
140    public function close(): void
141    {
142        if ($this->closed) {
143            return;
144        }
145
146        $this->closed = true;
147        $this->outstanding = new SplObjectStorage();
148
149        $teardowns = $this->teardowns;
150        $this->teardowns = [];
151        $failure = null;
152
153        foreach ($teardowns as $teardown) {
154            try {
155                $teardown();
156            } catch (Throwable $e) {
157                $failure ??= $e;
158            }
159        }
160
161        $this->subscription?->unsubscribe();
162
163        if ($failure !== null) {
164            throw $failure;
165        }
166    }
167
168    /** @param Closure(): void $emission */
169    private function runEmission(Closure $emission): void
170    {
171        if ($this->closed) {
172            return;
173        }
174
175        ++$this->runningEmissions;
176
177        try {
178            $emission();
179        } finally {
180            --$this->runningEmissions;
181            $this->closeIfSettled();
182        }
183    }
184
185    private function closeIfSettled(): void
186    {
187        if ($this->closed || $this->closeBelowStage === null || $this->runningEmissions > 0) {
188            return;
189        }
190
191        foreach ($this->outstanding as $pending) {
192            if ($this->outstanding[$pending] < $this->closeBelowStage) {
193                return;
194            }
195        }
196
197        $this->close();
198    }
199}