Skip to content

Instantly share code, notes, and snippets.

@Nek-
Created June 30, 2022 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nek-/6ce9fca13c9a37a602ea06191bd83178 to your computer and use it in GitHub Desktop.
Save Nek-/6ce9fca13c9a37a602ea06191bd83178 to your computer and use it in GitHub Desktop.
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Amp\Loop;
Loop::run(function () {
$messages = [];
function receive(&$messages) {
// static $reader;
if (empty($messages)) {
yield new \Amp\Success();
}
foreach ($messages as $message) {
yield new \Amp\Success($message);
}
// $reader = new \Amp\Deferred();
// return $reader->promise();
$messages = [];
}
$message = yield receive($messages);
if (null !== $message) {
echo "New message $message\n";
}
Loop::delay(1000, function () use (&$messages) {
$messages[] = 'Hello';
$messages[] = 'the';
$messages[] = 'world';
});
Loop::delay(5000, function () use (&$messages) {
$messages[] = 'More';
$messages[] = 'new';
$messages[] = 'messages';
});
Loop::delay(5500, function () use (&$messages) {
$messages[] = 'ang';
$messages[] = 'again';
$messages[] = 'me';
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment