Wrap remaining amphp helper methods

This commit is contained in:
Daniil Gentili 2019-06-01 16:22:44 +02:00
parent 692bcbcb1d
commit 660be7ae70
4 changed files with 28 additions and 29 deletions

View File

@ -40,7 +40,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
if ($res == '') {
$res = var_export($update, true);
}
//yield $this->sleep_async(3);
//yield $this->sleep(3);
try {
yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); //'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {

View File

@ -42,7 +42,7 @@ class EventHandler extends \danog\MadelineProto\CombinedEventHandler
if ($res == '') {
$res = var_export($update, true);
}
yield $MadelineProto->sleep_async(3);
yield $MadelineProto->sleep(3);
try {
yield $MadelineProto->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>\n\nDopo 3 secondi, in modo asincrono", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); //'entities' => [['_' => 'messageEntityPre', 'offset' => 0, 'length' => strlen($res), 'language' => 'json']]]);

View File

@ -78,7 +78,7 @@ trait UpdateHandler
if (!$params['timeout']) {
$params['timeout'] = 0.001;
}
yield $this->any([$this->waitUpdate(), new Delayed($params['timeout'] * 1000)]);
yield $this->first([$this->waitUpdate(), $this->sleep($params['timeout'])]);
}
if (empty($this->updates)) {

View File

@ -25,7 +25,10 @@ use Amp\Promise;
use Amp\Success;
use function Amp\Promise\all;
use function Amp\Promise\any;
use function Amp\Promise\some;
use function Amp\Promise\wait;
use function Amp\Promise\first;
use function Amp\Promise\timeout;
/**
* Some tools.
@ -95,23 +98,6 @@ trait Tools
return $resto < 0 ? $resto + abs($b) : $resto;
}
public function array_cast_recursive($array, $force = false)
{
if (!\danog\MadelineProto\Magic::$has_thread && !$force) {
return $array;
}
if (is_array($array)) {
if (!is_array($array)) {
$array = (array) $array;
}
foreach ($array as $key => $value) {
$array[$key] = $this->array_cast_recursive($value, $force);
}
}
return $array;
}
public function unpack_signed_int($value)
{
if (strlen($value) !== 4) {
@ -187,13 +173,6 @@ trait Tools
return unpack('d', \danog\MadelineProto\Magic::$BIG_ENDIAN ? strrev($value) : $value)[1];
}
public function infloop()
{
while (true) {
Loop::loop();
}
}
public function wait($promise)
{
if ($promise instanceof \Generator) {
@ -202,6 +181,8 @@ trait Tools
return $promise;
}
$exception = null;
$value = null;
$resolved = false;
do {
try {
@ -238,6 +219,24 @@ trait Tools
}
return any($promises);
}
public function some($promises)
{
foreach ($promises as &$promise) {
$promise = $this->call($promise);
}
return some($promises);
}
public function first($promises)
{
foreach ($promises as &$promise) {
$promise = $this->call($promise);
}
return first($promises);
}
public function timeout($promise, $timeout)
{
return timeout($this->call($promise), $timeout);
}
public function call($promise)
{
if ($promise instanceof \Generator) {
@ -296,7 +295,7 @@ trait Tools
return $deferred->promise();
}
public function sleep_async($time)
public function sleep($time)
{
return new \Amp\Delayed($time * 1000);
}