calgamo / event-loop
This package is abandoned and no longer maintained.
No replacement package was suggested.
Generic event loop implement
0.3.0
2018-05-17 14:47 UTC
Requires
- php: >=7.0.0
- calgamo/collection: ~0.3
- calgamo/exception: ~0.2
- calgamo/util: ~0.2
Requires (Dev)
- calgamo/test: ~0.2
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^6.3.0
This package is auto-updated.
Last update: 2019-12-09 11:08:41 UTC
README
Description
calgamo/event-loop is a library of processing event loop.
Feature
- Declarative programming
- High independency(workers have no need to know each other)
- Various kind of workers(callback, FilterWorker, Task and your own worker) can be accepted
- Generic purpose library(not limited to specific purpose. like middleware)
How to use
- Create event loop
- Add workers to event loop
- execute run method with start event.
Demo
Exsample 1: simple callback worker
(new EventLoop()) // clerk worker ->worker(function(EventContext $ctx){ $ctx->getEvent() ->when('Hi', function($e) use ($ctx){ echo "[$e]" . PHP_EOL; echo 'Clerk: Helo, Can I help you?' . PHP_EOL; $ctx->pushEvent('Can I help you'); }) ->when('Want to buy', function($e){ echo "[$e]" . PHP_EOL; echo 'Clerk: Ok. This way, please.' . PHP_EOL; }); }) // me worker ->worker(function(EventContext $ctx){ $ctx->getEvent() ->when('Can I help you', function($e) use ($ctx){ echo "[$e]" . PHP_EOL; echo 'You: Yes, I want to buy some clothes.' . PHP_EOL; $ctx->pushEvent('Want to buy some clothes'); }); }) // execute loop with start event ->run('You: Hi');
output:
[You: Hi]
Clerk: Helo, Can I help you?
[Can I help you]
You: Yes, I want to buy some clothes.
[Want to buy some clothes]
Clerk: Ok. This way, please.
Exsample 2: filter worker
(new EventLoop()) // clerk worker ->worker( (new FilterWorker()) ->filter('Hi', function(EventContext $ctx){ echo '[' . $ctx->getEvent() . ']' . PHP_EOL; echo 'Clerk: Helo, Can I help you?' . PHP_EOL; $ctx->pushEvent('Can I help you'); }) ->filter('Want to buy', function(EventContext $ctx){ echo '[' . $ctx->getEvent()->getName() . ']' . PHP_EOL; echo 'Clerk: Ok. This way, please.' . PHP_EOL; }) ) // me worker ->worker( (new FilterWorker()) ->filter('Can I help you', function(EventContext $ctx){ echo '[' . $ctx->getEvent() . ']' . PHP_EOL; echo 'You: Yes, I want to buy some clothes.' . PHP_EOL; $ctx->pushEvent('Want to buy some clothes'); }) ) // execute loop with start event ->run('You: Hi');
output: see exsample 1
Exsample 3: task & when
(new EventLoop()) // clerk worker ->worker( (new class extends Task { public function processEvent(EventContext $ctx) { $ctx->getEvent() ->when('Hi', function($e) use ($ctx){ echo "[$e]" . PHP_EOL; echo 'Clerk: Helo, Can I help you?' . PHP_EOL; $ctx->pushEvent('Can I help you'); }) ->when('Want to buy', function($e){ echo "[$e]" . PHP_EOL; echo 'Clerk: Ok. This way, please.' . PHP_EOL; }); } }) ) // me worker ->worker( (new class extends Task { public function processEvent(EventContext $ctx) { $ctx->getEvent() ->when('Can I help you', function($e) use ($ctx){ echo "[$e]" . PHP_EOL; echo 'You: Yes, I want to buy some clothes.' . PHP_EOL; $ctx->pushEvent('Want to buy some clothes'); }); } }) ) // execute loop with start event ->run('You: Hi');
output: see exsample 1
Exsample 4: task & no callbacks
(new EventLoop()) // clerk worker ->worker( (new class extends Task { public function processEvent(EventContext $ctx) { $e = $ctx->getEvent(); if ($this->match('Hi', $e)){ echo "[$e]" . PHP_EOL; echo 'Clerk: Helo, Can I help you?' . PHP_EOL; $ctx->pushEvent('Can I help you'); $e->consume(); } if ($this->match('Want to buy', $e)){ echo "[$e]" . PHP_EOL; echo 'Clerk: Ok. This way, please.' . PHP_EOL; $e->consume(); } } }) ) // me worker ->worker( (new class extends Task { public function processEvent(EventContext $ctx) { $e = $ctx->getEvent(); if ($this->match('Can I help you', $e)){ echo "[$e]" . PHP_EOL; echo 'You: Yes, I want to buy some clothes.' . PHP_EOL; $ctx->pushEvent('Want to buy some clothes'); $e->consume(); } } }) ) // execute loop with start event ->run('You: Hi');
output: see exsample 1
Installing calgamo/event-loop
The recommended way to install calgamo/event-loop is through Composer.
# Install Composer curl -sS https://getcomposer.org/installer | php
Next, run the Composer command to install the latest stable version of Grasshopper:
composer.phar require calgamo/event-loop
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
Dependencies
License
calgamo/event-loop is released under MIT License.
Author
Disclaimer
This software is no warranty.
We are not responsible for any results caused by the use of this software.
Please use the responsibility of the your self.