Created
June 30, 2022 20:34
-
-
Save Nek-/6ce9fca13c9a37a602ea06191bd83178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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