From 1d16cf807b4470c8ef5747423fca649dffd54f3d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Tue, 6 Oct 2020 17:33:18 +0200 Subject: [PATCH] Various improvements --- src/danog/MadelineProto/API.php | 2 +- src/danog/MadelineProto/EventHandler.php | 21 +++++++++++++++++++++ src/danog/MadelineProto/Wrappers/Events.php | 5 ++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/danog/MadelineProto/API.php b/src/danog/MadelineProto/API.php index 53550699..8646da8a 100644 --- a/src/danog/MadelineProto/API.php +++ b/src/danog/MadelineProto/API.php @@ -258,7 +258,7 @@ class API extends InternalDoc } $this->API->unreference(); } - if (isset($this->wrapper) && !Magic::$signaled) { + if (isset($this->wrapper) && (!Magic::$signaled || $this->gettingApiId)) { $this->logger->logger('Prompting final serialization...'); Tools::wait($this->wrapper->serialize()); $this->logger->logger('Done final serialization!'); diff --git a/src/danog/MadelineProto/EventHandler.php b/src/danog/MadelineProto/EventHandler.php index 7f220831..842d583c 100644 --- a/src/danog/MadelineProto/EventHandler.php +++ b/src/danog/MadelineProto/EventHandler.php @@ -24,6 +24,10 @@ namespace danog\MadelineProto; */ abstract class EventHandler extends InternalDoc { + /** + * Whether the event handler was started. + */ + private bool $startedInternal = false; /** * Internal constructor. * @@ -41,6 +45,23 @@ abstract class EventHandler extends InternalDoc $this->{$namespace} = $this->exportNamespace($namespace); } } + /** + * Start method handler. + * + * @internal + * + * @return \Generator + */ + public function startInternal(): \Generator + { + if ($this->startedInternal) { + return; + } + if (\method_exists($this, 'onStart')) { + yield $this->onStart(); + } + $this->startedInternal = true; + } /** * Get peers where to send error reports. * diff --git a/src/danog/MadelineProto/Wrappers/Events.php b/src/danog/MadelineProto/Wrappers/Events.php index 80b4e0a6..4f4ed7a4 100644 --- a/src/danog/MadelineProto/Wrappers/Events.php +++ b/src/danog/MadelineProto/Wrappers/Events.php @@ -79,6 +79,7 @@ trait Events } $this->initEventHandler($eventHandler); $this->eventHandlerMethods = []; + $this->loop_callback = null; foreach (\get_class_methods($this->event_handler) as $method) { if ($method === 'onLoop') { $this->loop_callback = [$this->event_handler_instance, 'onLoop']; @@ -96,9 +97,7 @@ trait Events } } yield from $this->setReportPeers($this->event_handler_instance->getReportPeers()); - if (\method_exists($this->event_handler_instance, 'onStart')) { - Tools::callFork($this->event_handler_instance->onStart()); - } + Tools::callFork($this->event_handler_instance->startInternal()); $this->updateHandler = [$this, 'eventUpdateHandler']; if ($this->inited()) { $this->startUpdateSystem();