Skip to content

Commit 4be4cd3

Browse files
committed
[2.x] Add Examples
1 parent 0d2261f commit 4be4cd3

File tree

3 files changed

+77
-36
lines changed

3 files changed

+77
-36
lines changed

README.md

+24-36
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,31 @@ 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;
34+
use function React\Async\async;
4435

45-
$loop = Factory::create();
46-
$eventLoopBridge = new EventLoopBridge($loop);
36+
$eventLoopBridge = new EventLoopBridge();
4737

48-
$channel = new Channel(Channel::Infinite);
49-
$eventLoopBridge->observe($channel)->subscribe(function (string $message) {
50-
echo $message, PHP_EOL;
51-
});
38+
Loop::futureTick(async(static function () use ($eventLoopBridge) {
39+
/** @var Channel<string> */
40+
$channel = new Channel(Channel::Infinite);
5241

53-
$loop->futureTick(function () use ($channel): void {
54-
$channel->send('Hello World!');
55-
$channel->close();
56-
});
42+
Loop::futureTick(function () use ($channel): void {
43+
$channel->send('Hello World!');
44+
$channel->close();
45+
});
5746

58-
$loop->run();
47+
foreach ($eventLoopBridge->observe($channel) as $message) {
48+
echo $message, PHP_EOL;
49+
}
50+
}));
5951
```
6052

6153
## Futures
@@ -64,24 +56,20 @@ Where promises are push, futures are pull, as such the event loop will poll and
6456
available.
6557

6658
```php
67-
use parallel\Channel;
68-
use React\EventLoop\Factory;
59+
use React\EventLoop\Loop;
6960
use ReactParallel\EventLoop\EventLoopBridge;
7061
use function parallel\run;
62+
use function React\Async\async;
7163

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

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

84-
$loop->run();
71+
echo $eventLoopBridge->await($future), PHP_EOL;
72+
}));
8573
```
8674

8775
## Metrics
@@ -105,7 +93,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
10593

10694
## License ##
10795

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

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

examples/channel.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
use parallel\Channel;
4+
use React\EventLoop\Loop;
5+
use ReactParallel\EventLoop\EventLoopBridge;
6+
use function parallel\run;
7+
use function React\Async\async;
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
10+
11+
$eventLoopBridge = new EventLoopBridge();
12+
/** @var Channel<string> */
13+
$channel = Channel::make('strings', Channel::Infinite);
14+
15+
16+
Loop::futureTick(async(static function () use ($eventLoopBridge, $channel) {
17+
echo __LINE__, PHP_EOL;
18+
foreach ($eventLoopBridge->observe($channel) as $message) {
19+
echo __LINE__, PHP_EOL;
20+
echo $message, PHP_EOL;
21+
echo __LINE__, PHP_EOL;
22+
}
23+
echo __LINE__, PHP_EOL;
24+
}));
25+
26+
Loop::addTimer(1, function () use ($channel): void {
27+
run(function () use ($channel): void {
28+
echo __LINE__, PHP_EOL;
29+
$channel->send('Hello World!');
30+
echo __LINE__, PHP_EOL;
31+
$channel->close();
32+
echo __LINE__, PHP_EOL;
33+
});
34+
});
35+

examples/future.php

+18
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+
}));

0 commit comments

Comments
 (0)