@@ -23,39 +23,37 @@ event loop, that will work, but it adds additional overhead when you have more t
23
23
different major contexts. Share this bridge around so that other packages can use them, and only have one instance
24
24
checking for events.
25
25
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
-
36
26
## Channels
37
27
38
28
Channels often have a stream of messages going over them, as such the bridge will convert them into an observable.
39
29
40
30
``` php
41
31
use parallel\Channel;
42
- use React\EventLoop\Factory ;
32
+ use React\EventLoop\Loop ;
43
33
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
+ }));
59
57
```
60
58
61
59
## Futures
@@ -64,24 +62,20 @@ Where promises are push, futures are pull, as such the event loop will poll and
64
62
available.
65
63
66
64
``` php
67
- use parallel\Channel;
68
- use React\EventLoop\Factory;
65
+ use React\EventLoop\Loop;
69
66
use ReactParallel\EventLoop\EventLoopBridge;
70
67
use function parallel\run;
68
+ use function React\Async\async;
71
69
72
- $loop = Factory::create();
73
- $eventLoopBridge = new EventLoopBridge($loop);
74
-
75
- $future = run(function (): string {
76
- return 'Hello World!';
77
- });
70
+ $eventLoopBridge = new EventLoopBridge();
78
71
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
+ });
83
76
84
- $loop->run();
77
+ echo $eventLoopBridge->await($future), PHP_EOL;
78
+ }));
85
79
```
86
80
87
81
## Metrics
@@ -105,7 +99,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
105
99
106
100
## License ##
107
101
108
- Copyright 2024 [ Cees-Jan Kiewiet] ( http://wyrihaximus.net/ )
102
+ Copyright 2025 [ Cees-Jan Kiewiet] ( http://wyrihaximus.net/ )
109
103
110
104
Permission is hereby granted, free of charge, to any person
111
105
obtaining a copy of this software and associated documentation
0 commit comments