diff --git a/bot.php b/bot.php index 9b1285f0..7b2152fa 100755 --- a/bot.php +++ b/bot.php @@ -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' => "$res", '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') { diff --git a/combined_bot.php b/combined_bot.php index a3b425da..7b8574d2 100755 --- a/combined_bot.php +++ b/combined_bot.php @@ -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' => "$res\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']]]); diff --git a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php index 06ac0c78..c3ef9194 100644 --- a/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php +++ b/src/danog/MadelineProto/MTProtoTools/UpdateHandler.php @@ -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)) { diff --git a/src/danog/MadelineProto/Tools.php b/src/danog/MadelineProto/Tools.php index 2ceef8c5..47f7c6ea 100644 --- a/src/danog/MadelineProto/Tools.php +++ b/src/danog/MadelineProto/Tools.php @@ -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) { @@ -186,14 +172,7 @@ 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); }