From 2f5dcf5daa6f7f48a89852af0fc3bb664232c12d Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 18 Jul 2019 22:02:18 +0000 Subject: [PATCH] Create perioc loop --- .../Loop/Generic/PeriodicLoop.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php diff --git a/src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php b/src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php new file mode 100644 index 00000000..192e3686 --- /dev/null +++ b/src/danog/MadelineProto/Loop/Generic/PeriodicLoop.php @@ -0,0 +1,67 @@ +. + * + * @author Daniil Gentili + * @copyright 2016-2019 Daniil Gentili + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * + * @link https://docs.madelineproto.xyz MadelineProto documentation + */ + +namespace danog\MadelineProto\Loop\Generic; + +use danog\MadelineProto\Loop\Impl\ResumableSignalLoop; + +/** + * Periodic loop. + * + * @author Daniil Gentili + */ +class PeriodicLoop extends ResumableSignalLoop +{ + private $callback; + private $name; + private $timeout; + + /** + * Constructor. + * + * + * @param \danog\MadelineProto\MTProto $API Instance of MTProto class + */ + public function __construct($API, $callback, $name, $timeout) + { + $this->API = $API; + $this->callback = $callback; + $this->name = $name; + $this->timeout = $timeout; + } + + public function loop() + { + $callback = $this->callback; + $logger = $this->API->logger; + + while (true) { + $result = yield $this->waitSignal($this->pause($this->timeout)); + if ($result) { + $logger->logger("Got signal in $this, exiting"); + return; + } + yield $callback(); + } + } + + public function __toString(): string + { + return $this->name; + } +}