Skip to content

[2.x] Add Examples + ensure docs are up to date and examples in it are working #64

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
Feb 9, 2025
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
74 changes: 34 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,37 @@ event loop, that will work, but it adds additional overhead when you have more t
different major contexts. Share this bridge around so that other packages can use them, and only have one instance
checking for events.

```php
use React\EventLoop\Factory;
use ReactParallel\EventLoop\EventLoopBridge;

$loop = Factory::create();
$eventLoopBridge = new EventLoopBridge($loop);

$loop->run();
```

## Channels

Channels often have a stream of messages going over them, as such the bridge will convert them into an observable.

```php
use parallel\Channel;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;

$loop = Factory::create();
$eventLoopBridge = new EventLoopBridge($loop);

$channel = new Channel(Channel::Infinite);
$eventLoopBridge->observe($channel)->subscribe(function (string $message) {
echo $message, PHP_EOL;
});

$loop->futureTick(function () use ($channel): void {
$channel->send('Hello World!');
$channel->close();
});

$loop->run();
use function React\Async\async;
use function React\Async\await;
use function React\Promise\Timer\sleep;

$eventLoopBridge = new EventLoopBridge();

Loop::futureTick(async(static function () use ($eventLoopBridge) {
/** @var Channel<string> */
$channel = new Channel(Channel::Infinite);

Loop::futureTick(async(function () use ($channel): void {
$channel->send('Hello World!');
// Don't close the channel right after writing to it,
// as it will be closed on both ends and the other
// thread won't receive your message
await(sleep(1));
$channel->close();
}));

foreach ($eventLoopBridge->observe($channel) as $message) {
echo $message, PHP_EOL;
}
}));
```

## Futures
Expand All @@ -64,24 +62,20 @@ Where promises are push, futures are pull, as such the event loop will poll and
available.

```php
use parallel\Channel;
use React\EventLoop\Factory;
use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use function parallel\run;
use function React\Async\async;

$loop = Factory::create();
$eventLoopBridge = new EventLoopBridge($loop);

$future = run(function (): string {
return 'Hello World!';
});
$eventLoopBridge = new EventLoopBridge();

$channel = new Channel(Channel::Infinite);
$eventLoopBridge->await($future)->then(function (string $message) {
echo $message, PHP_EOL;
});
Loop::futureTick(async(static function () use ($eventLoopBridge) {
$future = run(function (): string {
return 'Hello World!';
});

$loop->run();
echo $eventLoopBridge->await($future), PHP_EOL;
}));
```

## Metrics
Expand All @@ -105,7 +99,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## License ##

Copyright 2024 [Cees-Jan Kiewiet](http://wyrihaximus.net/)
Copyright 2025 [Cees-Jan Kiewiet](http://wyrihaximus.net/)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
30 changes: 30 additions & 0 deletions examples/channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

use parallel\Channel;
use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use function React\Async\async;
use function React\Async\await;
use function React\Promise\Timer\sleep;

require_once __DIR__ . '/../vendor/autoload.php';

$eventLoopBridge = new EventLoopBridge();

Loop::futureTick(async(static function () use ($eventLoopBridge) {
/** @var Channel<string> */
$channel = new Channel(Channel::Infinite);

Loop::futureTick(async(function () use ($channel): void {
$channel->send('Hello World!');
// Don't close the channel right after writing to it,
// as it will be closed on both ends and the other
// thread won't receive your message
await(sleep(1));
$channel->close();
}));

foreach ($eventLoopBridge->observe($channel) as $message) {
echo $message, PHP_EOL;
}
}));
18 changes: 18 additions & 0 deletions examples/future.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use function parallel\run;
use function React\Async\async;

require_once __DIR__ . '/../vendor/autoload.php';

$eventLoopBridge = new EventLoopBridge();

Loop::futureTick(async(static function () use ($eventLoopBridge) {
$future = run(function (): string {
return 'Hello World!';
});

echo $eventLoopBridge->await($future), PHP_EOL;
}));
2 changes: 1 addition & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class Stream implements StreamInterface
public function __construct()
{
$this->queue = new SplQueue();
$this->queue->setIteratorMode(SplQueue::IT_MODE_DELETE);
$this->queue->setIteratorMode(SplQueue::IT_MODE_DELETE | SplQueue::IT_MODE_DELETE);
/** @psalm-suppress MixedPropertyTypeCoercion */
$this->wait = new Deferred();
}
Expand Down
Loading