Skip to content

Commit 8bd9ec8

Browse files
authored
Merge pull request #64 from reactphp-parallel/2.x-docs
[2.x] Add Examples + ensure docs are up to date and examples in it are working
2 parents 0d2261f + 705e7a0 commit 8bd9ec8

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

README.md

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,37 @@ event loop, that will work, but it adds additional overhead when you have more t
2323
different major contexts. Share this bridge around so that other packages can use them, and only have one instance
2424
checking for events.
2525

26-
```php
27-
use React\EventLoop\Factory;
28-
use ReactParallel\EventLoop\EventLoopBridge;
29-
30-
$loop = Factory::create();
31-
$eventLoopBridge = new EventLoopBridge($loop);
32-
33-
$loop->run();
34-
```
35-
3626
## Channels
3727

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

4030
```php
4131
use parallel\Channel;
42-
use React\EventLoop\Factory;
32+
use React\EventLoop\Loop;
4333
use ReactParallel\EventLoop\EventLoopBridge;
44-
45-
$loop = Factory::create();
46-
$eventLoopBridge = new EventLoopBridge($loop);
47-
48-
$channel = new Channel(Channel::Infinite);
49-
$eventLoopBridge->observe($channel)->subscribe(function (string $message) {
50-
echo $message, PHP_EOL;
51-
});
52-
53-
$loop->futureTick(function () use ($channel): void {
54-
$channel->send('Hello World!');
55-
$channel->close();
56-
});
57-
58-
$loop->run();
34+
use function React\Async\async;
35+
use function React\Async\await;
36+
use function React\Promise\Timer\sleep;
37+
38+
$eventLoopBridge = new EventLoopBridge();
39+
40+
Loop::futureTick(async(static function () use ($eventLoopBridge) {
41+
/** @var Channel<string> */
42+
$channel = new Channel(Channel::Infinite);
43+
44+
Loop::futureTick(async(function () use ($channel): void {
45+
$channel->send('Hello World!');
46+
// Don't close the channel right after writing to it,
47+
// as it will be closed on both ends and the other
48+
// thread won't receive your message
49+
await(sleep(1));
50+
$channel->close();
51+
}));
52+
53+
foreach ($eventLoopBridge->observe($channel) as $message) {
54+
echo $message, PHP_EOL;
55+
}
56+
}));
5957
```
6058

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

6664
```php
67-
use parallel\Channel;
68-
use React\EventLoop\Factory;
65+
use React\EventLoop\Loop;
6966
use ReactParallel\EventLoop\EventLoopBridge;
7067
use function parallel\run;
68+
use function React\Async\async;
7169

72-
$loop = Factory::create();
73-
$eventLoopBridge = new EventLoopBridge($loop);
74-
75-
$future = run(function (): string {
76-
return 'Hello World!';
77-
});
70+
$eventLoopBridge = new EventLoopBridge();
7871

79-
$channel = new Channel(Channel::Infinite);
80-
$eventLoopBridge->await($future)->then(function (string $message) {
81-
echo $message, PHP_EOL;
82-
});
72+
Loop::futureTick(async(static function () use ($eventLoopBridge) {
73+
$future = run(function (): string {
74+
return 'Hello World!';
75+
});
8376

84-
$loop->run();
77+
echo $eventLoopBridge->await($future), PHP_EOL;
78+
}));
8579
```
8680

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

106100
## License ##
107101

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

110104
Permission is hereby granted, free of charge, to any person
111105
obtaining a copy of this software and associated documentation

examples/channel.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
use parallel\Channel;
4+
use React\EventLoop\Loop;
5+
use ReactParallel\EventLoop\EventLoopBridge;
6+
use function React\Async\async;
7+
use function React\Async\await;
8+
use function React\Promise\Timer\sleep;
9+
10+
require_once __DIR__ . '/../vendor/autoload.php';
11+
12+
$eventLoopBridge = new EventLoopBridge();
13+
14+
Loop::futureTick(async(static function () use ($eventLoopBridge) {
15+
/** @var Channel<string> */
16+
$channel = new Channel(Channel::Infinite);
17+
18+
Loop::futureTick(async(function () use ($channel): void {
19+
$channel->send('Hello World!');
20+
// Don't close the channel right after writing to it,
21+
// as it will be closed on both ends and the other
22+
// thread won't receive your message
23+
await(sleep(1));
24+
$channel->close();
25+
}));
26+
27+
foreach ($eventLoopBridge->observe($channel) as $message) {
28+
echo $message, PHP_EOL;
29+
}
30+
}));

examples/future.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
use React\EventLoop\Loop;
4+
use ReactParallel\EventLoop\EventLoopBridge;
5+
use function parallel\run;
6+
use function React\Async\async;
7+
8+
require_once __DIR__ . '/../vendor/autoload.php';
9+
10+
$eventLoopBridge = new EventLoopBridge();
11+
12+
Loop::futureTick(async(static function () use ($eventLoopBridge) {
13+
$future = run(function (): string {
14+
return 'Hello World!';
15+
});
16+
17+
echo $eventLoopBridge->await($future), PHP_EOL;
18+
}));

src/Stream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Stream implements StreamInterface
2424
public function __construct()
2525
{
2626
$this->queue = new SplQueue();
27-
$this->queue->setIteratorMode(SplQueue::IT_MODE_DELETE);
27+
$this->queue->setIteratorMode(SplQueue::IT_MODE_DELETE | SplQueue::IT_MODE_DELETE);
2828
/** @psalm-suppress MixedPropertyTypeCoercion */
2929
$this->wait = new Deferred();
3030
}

0 commit comments

Comments
 (0)