@@ -23,39 +23,37 @@ event loop, that will work, but it adds additional overhead when you have more t
2323different major contexts. Share this bridge around so that other packages can use them, and only have one instance
2424checking 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
3828Channels often have a stream of messages going over them, as such the bridge will convert them into an observable.
3929
4030``` php
4131use parallel\Channel;
42- use React\EventLoop\Factory ;
32+ use React\EventLoop\Loop ;
4333use 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
6462available.
6563
6664``` php
67- use parallel\Channel;
68- use React\EventLoop\Factory;
65+ use React\EventLoop\Loop;
6966use ReactParallel\EventLoop\EventLoopBridge;
7067use 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
110104Permission is hereby granted, free of charge, to any person
111105obtaining a copy of this software and associated documentation
0 commit comments