From b0d512b77dd66cb144efce7ab3772738f02648a5 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Wed, 26 Feb 2020 15:15:37 +0100 Subject: [PATCH] Reduce boilerplate --- docs | 2 +- examples/combinedBot.php | 4 +-- src/danog/MadelineProto/API.php | 50 +++++++++++++++++++++-------- src/danog/MadelineProto/MTProto.php | 7 ++-- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/docs b/docs index e44490c9d..648b0a125 160000 --- a/docs +++ b/docs @@ -1 +1 @@ -Subproject commit e44490c9dce88994398746b579c0debabd0f1e4a +Subproject commit 648b0a1250e3949c631421b8f1666371567f74c7 diff --git a/examples/combinedBot.php b/examples/combinedBot.php index b38524ee9..43822a119 100755 --- a/examples/combinedBot.php +++ b/examples/combinedBot.php @@ -103,7 +103,7 @@ foreach ([ 'user2.madeline' => 'Userbot login (2)' ] as $session => $message) { Logger::log($message, Logger::WARNING); - $MadelineProtos []= (new API($session))->startAndLoopBackground(MyEventHandler::class); + $MadelineProtos []= new API($session); } -Tools::wait(Tools::all($MadelineProtos)); +API::startAndLoopMulti($MadelineProtos, MyEventHandler::class); \ No newline at end of file diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index e8f9e3214..b05f1b90e 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -225,21 +225,45 @@ class API extends InternalDoc */ public function startAndLoop(string $eventHandler): void { - Tools::wait($this->startAndLoopAsync($eventHandler)); + while (true) { + try { + Tools::wait($this->startAndLoopAsync($eventHandler)); + return; + } catch (\Throwable $e) { + $this->logger((string) $e, Logger::FATAL_ERROR); + $this->report("Surfaced: $e"); + } + } } /** - * Start MadelineProto and the event handler in background (enables async). + * Start multiple instances of MadelineProto and the event handlers (enables async). * - * Also initializes error reporting, catching and reporting all errors surfacing from the event loop. + * @param API[] $instances Instances of madeline + * @param string[]|string $eventHandler Event handler(s) * - * @param string $eventHandler Event handler class name - * - * @return void + * @return Promise */ - public function startAndLoopBackground(string $eventHandler): Promise + public static function startAndLoopMulti(array $instances, $eventHandler): void { - $this->start(['async' => false]); - return Tools::call($this->startAndLoopAsync($eventHandler)); + if (\is_string($eventHandler)) { + $eventHandler = \array_fill_keys(\array_keys($instances), $eventHandler); + } + + $instanceOne = array_values($instances)[0]; + while (true) { + try { + $promises = []; + foreach ($instances as $k => $instance) { + $instance->start(['async' => false]); + $promises []= Tools::call($instance->startAndLoopAsync($eventHandler[$k])); + } + Tools::wait(Tools::all($promises)); + return; + } catch (\Throwable $e) { + $instanceOne->logger((string) $e, Logger::FATAL_ERROR); + $instanceOne->report("Surfaced: $e"); + } + } } /** * Start MadelineProto and the event handler (enables async). @@ -253,17 +277,15 @@ class API extends InternalDoc private function startAndLoopAsync(string $eventHandler): \Generator { $this->async(true); - do { - $thrown = false; + while (true) { try { yield $this->start(); yield $this->setEventHandler($eventHandler); - yield from $this->API->loop(); + return yield from $this->API->loop(); } catch (\Throwable $e) { - $thrown = true; $this->logger((string) $e, Logger::FATAL_ERROR); $this->report("Surfaced: $e"); } - } while ($thrown); + } } } diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index fe89c9d07..25f34b787 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -1730,8 +1730,7 @@ class MTProto extends AsyncConstruct implements TLCallback return; } $file = null; - if ($this->settings['logger']['logger'] === Logger::FILE_LOGGER) { - $path = $this->settings['logger']['logger_param']; + if ($this->settings['logger']['logger'] === Logger::FILE_LOGGER && $path = $this->settings['logger']['logger_param']) { StatCache::clear($path); if (!yield exists($path)) { $message = "!!! WARNING !!!\nThe logfile does not exist, please DO NOT delete the logfile to avoid errors in MadelineProto!\n\n$message"; @@ -1757,7 +1756,9 @@ class MTProto extends AsyncConstruct implements TLCallback foreach ($this->reportDest as $id) { try { yield from $this->methodCallAsyncRead('messages.sendMessage', ['peer' => $id, 'message' => $message]); - yield from $this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]); + if ($file) { + yield from $this->methodCallAsyncRead('messages.sendMedia', ['peer' => $id, 'media' => $file]); + } $sent &= true; } catch (\Throwable $e) { $sent &= false;