项目作者: JustBlackBird

项目描述 :
AMP Emitter with priority queue inside
高级语言: PHP
项目地址: git://github.com/JustBlackBird/amphp-priority-emitter.git
创建时间: 2020-07-19T20:51:55Z
项目社区:https://github.com/JustBlackBird/amphp-priority-emitter

开源协议:MIT License

下载


AMP priority emitter

Latest Stable Version Test

In-memory implementation of async emitter with prioritized messages

Why

Implementation of AMP Emitter is backed by a queue. It covers many cases but
sometimes a priority queue is needed.

For example, you’re building a bot for a social network or a messenger. Assume
that the bot can react for users’ commands and broadcast information to all its
subscribers. These two types of messages have different priorities. Commands’
responses must be sent as soon as possible to keep UX responsive but
broadcasting messages can wait for a while.

You can build the app around a message bus. There is some code that pushes
messages to the bus, and some code that pull them out and transfer to social
network API.

You cannot use Emitter shipped with AMP because in such case broadcasting
messages will block command ones because there is no way to set priority
with AMP Emitter.

This library adds and Emitter with API similar to AMP Emitter but with
priority support.

Installation

  1. composer require justblackbird/amphp-priority-emitter

Usage

  1. use Amp\Loop;
  2. use JustBlackBird\AmpPriorityEmitter\Emitter;
  3. // The following example will output:
  4. // - important message
  5. // - message one
  6. // - message two
  7. Loop::run(static function() {
  8. $emitter = new Emitter();
  9. $emitter->emit('message one', 0);
  10. $emitter->emit('message two', 0);
  11. $emitter->emit('important message', 5);
  12. $emitter->complete();
  13. $iterator = $emitter->iterate();
  14. while(yield $iterator->advance()) {
  15. echo "- " . $iterator->getCurrent() . "\n";
  16. }
  17. });

License

MIT (c) Dmitry Simushev

The implementation is based on AMP Emitter.