|
1 | | -# event-loop |
2 | | -Event Loop bridge to ext-parallel Events |
| 1 | +# Event Loop bridge to ext-parallel Events |
| 2 | + |
| 3 | + |
| 4 | +[](https://packagist.org/packages/React-parallel/event-loop) |
| 5 | +[](https://packagist.org/packages/React-parallel/event-loop) |
| 6 | +[](https://scrutinizer-ci.com/g/Reactphp-parallel/event-loop/?branch=master) |
| 7 | +[](https://shepherd.dev/github/Reactphp-parallel/event-loop) |
| 8 | +[](https://packagist.org/packages/React-parallel/event-loop) |
| 9 | + |
| 10 | +### Installation ### |
| 11 | + |
| 12 | +To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `~`. |
| 13 | + |
| 14 | +``` |
| 15 | +composer require react-parallel/event-loop |
| 16 | +``` |
| 17 | + |
| 18 | +# Usage |
| 19 | + |
| 20 | +## Set up |
| 21 | + |
| 22 | +Just like the ReactPHP event loop, you should only have one bridge. You can have multiple, and unlike the ReactPHP |
| 23 | +event loop, that will work, but it adds additional overhead when you have more than a few. Having a hand full for |
| 24 | +different major contexts. Share this bridge around so that other packages can use them, and only have one instance |
| 25 | +checking for events. |
| 26 | + |
| 27 | +```php |
| 28 | +use React\EventLoop\Factory; |
| 29 | +use ReactParallel\EventLoop\EventLoopBridge; |
| 30 | + |
| 31 | +$loop = Factory::create(); |
| 32 | +$eventLoopBridge = new EventLoopBridge($loop); |
| 33 | + |
| 34 | +$loop->run(); |
| 35 | +``` |
| 36 | + |
| 37 | +## Channels |
| 38 | + |
| 39 | +Channels often have a stream of messages going over them, as such the bridge will convert them into an observable. |
| 40 | + |
| 41 | +```php |
| 42 | +use parallel\Channel; |
| 43 | +use React\EventLoop\Factory; |
| 44 | +use ReactParallel\EventLoop\EventLoopBridge; |
| 45 | + |
| 46 | +$loop = Factory::create(); |
| 47 | +$eventLoopBridge = new EventLoopBridge($loop); |
| 48 | + |
| 49 | +$channel = new Channel(Channel::Infinite); |
| 50 | +$eventLoopBridge->observe($channel)->subscribe(function (string $message) { |
| 51 | + echo $message, PHP_EOL; |
| 52 | +}); |
| 53 | + |
| 54 | +$loop->futureTick(function () use ($channel): void { |
| 55 | + $channel->send('Hello World!'); |
| 56 | + $channel->close(); |
| 57 | +}); |
| 58 | + |
| 59 | +$loop->run(); |
| 60 | +``` |
| 61 | + |
| 62 | +## Futures |
| 63 | + |
| 64 | +Where promises are push, futures are pull, as such the event loop will poll and resolve the promise once a result is |
| 65 | +available. |
| 66 | + |
| 67 | +```php |
| 68 | +use parallel\Channel; |
| 69 | +use React\EventLoop\Factory; |
| 70 | +use ReactParallel\EventLoop\EventLoopBridge; |
| 71 | +use function parallel\run; |
| 72 | + |
| 73 | +$loop = Factory::create(); |
| 74 | +$eventLoopBridge = new EventLoopBridge($loop); |
| 75 | + |
| 76 | +$future = run(function (): string { |
| 77 | + return 'Hello World!'; |
| 78 | +}); |
| 79 | + |
| 80 | +$channel = new Channel(Channel::Infinite); |
| 81 | +$eventLoopBridge->await($future)->then(function (string $message) { |
| 82 | + echo $message, PHP_EOL; |
| 83 | +}); |
| 84 | + |
| 85 | +$loop->run(); |
| 86 | +``` |
| 87 | + |
| 88 | +## Contributing ## |
| 89 | + |
| 90 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 91 | + |
| 92 | +## License ## |
| 93 | + |
| 94 | +Copyright 2020 [Cees-Jan Kiewiet](http://wyrihaximus.net/) |
| 95 | + |
| 96 | +Permission is hereby granted, free of charge, to any person |
| 97 | +obtaining a copy of this software and associated documentation |
| 98 | +files (the "Software"), to deal in the Software without |
| 99 | +restriction, including without limitation the rights to use, |
| 100 | +copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 101 | +copies of the Software, and to permit persons to whom the |
| 102 | +Software is furnished to do so, subject to the following |
| 103 | +conditions: |
| 104 | + |
| 105 | +The above copyright notice and this permission notice shall be |
| 106 | +included in all copies or substantial portions of the Software. |
| 107 | + |
| 108 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 109 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 110 | +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 111 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 112 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 113 | +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 114 | +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 115 | +OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments