Lines 100.00% 6 / 6
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
TakeUntilOperator 100.00% 6 / 6 100.00% 2 / 2 2 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 attach 100.00% 5 / 5 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Support\Stream;
6
7use Closure;
8use Stewart\Contracts\EventStream;
9use Stewart\Contracts\Subscription;
10
11/**
12 * @internal
13 * @template T
14 * @implements StreamSource<T>
15 */
16final readonly class TakeUntilOperator implements StreamSource
17{
18    /**
19     * @param StreamSource<T> $source
20     * @param EventStream<mixed> $notifier
21     */
22    public function __construct(
23        private StreamSource $source,
24        private EventStream $notifier,
25    ) {}
26
27    public function attach(SubscriptionScope $scope, Closure $downstream): Subscription
28    {
29        $watching = $this->notifier->subscribe(static function () use ($scope): void {
30            $scope->close();
31        });
32
33        $scope->onTeardown($watching->unsubscribe(...));
34
35        return $this->source->attach($scope, $downstream);
36    }
37}