Skip to content

[2.x] Replace observables with iterables #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
"react/async": "^4.2",
"react/event-loop": "^1.5",
"react/promise": "^2.11 || ^3.0",
"reactivex/rxphp": "^2.0.12",
"wyrihaximus/constants": "^1.6.0",
"wyrihaximus/metrics": "^1.0 || ^2",
"wyrihaximus/react-awaitable-observable": "^1",
"wyrihaximus/react-event-loop-rx-scheduler-hook-up": "^0.1.1"
"wyrihaximus/metrics": "^1.0 || ^2"
},
"require-dev": {
"wyrihaximus/async-test-utilities": "^7.2 || ^5"
Expand Down
185 changes: 4 additions & 181 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timeout": 120,
"timeout": 1800,
"source": {
"directories": [
"src"
Expand All @@ -9,8 +9,12 @@
"text": "./var/infection.log",
"summary": "./var/infection-summary.log",
"json": "./var/infection.json",
"perMutator": "./var/infection-per-mutator.md"
"perMutator": "./var/infection-per-mutator.md",
"github": true
},
"minMsi": 100,
"minCoveredMsi": 100,
"ignoreMsiWithNoMutations": true,
"mutators": {
"@default": true
},
Expand Down
14 changes: 14 additions & 0 deletions src/Done.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace ReactParallel\EventLoop;

/**
* @internal
*
* Marker class for when a stream is done
*/
final class Done
{
}
13 changes: 5 additions & 8 deletions src/EventLoopBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use React\EventLoop\Loop;
use React\EventLoop\TimerInterface;
use React\Promise\Deferred;
use Rx\Subject\Subject;
use WyriHaximus\Metrics\Label;

use function count;
use function React\Async\await;
use function spl_object_hash;
use function spl_object_id;
use function WyriHaximus\React\awaitObservable;

use const WyriHaximus\Constants\Numeric\ZERO;

Expand All @@ -40,7 +38,7 @@ final class EventLoopBridge

private TimerInterface|null $timer = null;

/** @var array<int, Subject> */
/** @var array<int, Stream> */
private array $channels = [];

/** @var array<int, Deferred> */
Expand Down Expand Up @@ -68,8 +66,7 @@ public function withMetrics(Metrics $metrics): self
/** @return iterable<mixed> */
public function observe(Channel $channel): iterable
{
$subject = new Subject();
$this->channels[spl_object_id($channel)] = $subject;
$this->channels[spl_object_id($channel)] = new Stream();
$this->events->addChannel($channel);

if ($this->metrics instanceof Metrics) {
Expand All @@ -78,7 +75,7 @@ public function observe(Channel $channel): iterable

$this->startTimer();

return awaitObservable($subject);
yield from $this->channels[spl_object_id($channel)]->iterable();
}

public function await(Future $future): mixed
Expand Down Expand Up @@ -228,7 +225,7 @@ private function handleFutureReadEvent(Events\Event $event): void

private function handleChannelReadEvent(Events\Event $event): void
{
$this->channels[spl_object_id($event->object)]->onNext($event->value);
$this->channels[spl_object_id($event->object)]->value($event->value);
$this->events->addChannel($event->object); /** @phpstan-ignore-line */

if (! ($this->metrics instanceof Metrics)) {
Expand All @@ -240,7 +237,7 @@ private function handleChannelReadEvent(Events\Event $event): void

private function handleCloseEvent(Events\Event $event): void
{
$this->channels[spl_object_id($event->object)]->onCompleted();
$this->channels[spl_object_id($event->object)]->done();
unset($this->channels[spl_object_id($event->object)]);

if (! ($this->metrics instanceof Metrics)) {
Expand Down
54 changes: 54 additions & 0 deletions src/Stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace ReactParallel\EventLoop;

use React\Promise\Deferred;
use SplQueue;

use function React\Async\await;

final class Stream
{
private SplQueue $queue;
private Deferred $wait;

public function __construct()
{
$this->queue = new SplQueue();
$this->queue->setIteratorMode(SplQueue::IT_MODE_DELETE);
$this->wait = new Deferred();
}

public function value(mixed $value): void
{
$this->queue->enqueue($value);
$this->wait->resolve(new Value());
}

public function done(): void
{
$this->wait->resolve(new Done());
}

/** @return iterable<mixed> */
public function iterable(): iterable
{
do {
$run = false;
$type = await($this->wait->promise());

foreach ($this->queue as $value) {
yield $value;
}

if ($type instanceof Done) {
continue;
}

$this->wait = new Deferred();
$run = true;
} while ($run);
}
}
14 changes: 14 additions & 0 deletions src/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace ReactParallel\EventLoop;

/**
* @internal
*
* Marker class for when a stream is received a value
*/
final class Value
{
}
Loading