2026-02-14 17:38:02 +00:00

72 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-2022 David Cole <david.cole1340@gmail.com>
* Copyright (c) 2020-present Valithor Obsidion <valithor@discordphp.org>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
use Discord\Discord;
use Psr\Log\NullLogger;
const TIMEOUT = 10;
function wait(callable $callback, float $timeout = TIMEOUT, ?callable $timeoutFn = null)
{
$discord = DiscordSingleton::get();
$result = null;
$finally = null;
$timedOut = false;
$discord->getLoop()->futureTick(function () use ($callback, $discord, &$result, &$finally) {
$resolve = function ($x = null) use ($discord, &$result) {
$result = $x;
$discord->getLoop()->stop();
};
try {
$finally = $callback($discord, $resolve);
} catch (\Throwable $e) {
$resolve($e);
}
});
$timeout = $discord->getLoop()->addTimer($timeout, function () use ($discord, &$timedOut) {
$timedOut = true;
$discord->getLoop()->stop();
});
$discord->getLoop()->run();
$discord->getLoop()->cancelTimer($timeout);
if ($result instanceof Exception) {
throw $result;
}
if (is_callable($finally)) {
$finally();
}
if ($timedOut) {
if ($timeoutFn !== null) {
$timeoutFn();
} else {
throw new \Exception('Timed out');
}
}
return $result;
}
function getMockDiscord(): Discord
{
return new Discord(['token' => '', 'logger' => new NullLogger()]);
}