Simpler async API

This commit is contained in:
Daniil Gentili 2019-05-12 12:06:58 +02:00
parent d44e35f514
commit bdd04ff7b3
10 changed files with 22 additions and 56 deletions

View File

@ -18,8 +18,6 @@
namespace danog\MadelineProto\Async; namespace danog\MadelineProto\Async;
use Amp\Success;
/** /**
* Async parameters class. * Async parameters class.
* *
@ -53,17 +51,9 @@ class AsyncParameters extends Parameters
return $this->refetchable; return $this->refetchable;
} }
public function getParameters(): \Generator public function getParameters()
{ {
$callable = $this->callable; $callable = $this->callable;
$params = $callable(); return yield $callable();
if ($params instanceof \Generator) {
$params = yield coroutine($params);
} else {
$params = yield new Success($params);
}
return $params;
} }
} }

View File

@ -19,7 +19,6 @@
namespace danog\MadelineProto\Async; namespace danog\MadelineProto\Async;
use Amp\Promise; use Amp\Promise;
use function Amp\call;
/** /**
* Parameters module. * Parameters module.
@ -37,23 +36,13 @@ abstract class Parameters
* *
* @return Promise * @return Promise
*/ */
public function fetchParameters(): Promise public function fetchParameters()
{
return call([$this, 'fetchParametersAsync']);
}
/**
* Fetch parameters asynchronously.
*
* @return \Generator
*/
public function fetchParametersAsync(): \Generator
{ {
$refetchable = $this->isRefetchable(); $refetchable = $this->isRefetchable();
if ($this->params && !$refetchable) { if ($this->params && !$refetchable) {
return $this->params; return $this->params;
} }
$params = yield call([$this, 'getParameters']); $params = yield $this->getParameters();
if (!$refetchable) { if (!$refetchable) {
$this->params = $params; $this->params = $params;
@ -74,5 +63,5 @@ abstract class Parameters
* *
* @return \Generator * @return \Generator
*/ */
abstract public function getParameters(): \Generator; abstract public function getParameters();
} }

View File

@ -21,7 +21,6 @@ namespace danog\MadelineProto\Loop\Connection;
use Amp\Deferred; use Amp\Deferred;
use danog\MadelineProto\Logger; use danog\MadelineProto\Logger;
use danog\MadelineProto\Loop\Impl\ResumableSignalLoop; use danog\MadelineProto\Loop\Impl\ResumableSignalLoop;
use function Amp\call;
/** /**
* RPC call status check loop. * RPC call status check loop.
@ -30,7 +29,7 @@ use function Amp\call;
*/ */
class CheckLoop extends ResumableSignalLoop class CheckLoop extends ResumableSignalLoop
{ {
public function loop(): \Generator public function loop()
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;

View File

@ -31,7 +31,7 @@ use danog\MadelineProto\Stream\MTProtoTransport\HttpStream;
*/ */
class HttpWaitLoop extends ResumableSignalLoop class HttpWaitLoop extends ResumableSignalLoop
{ {
public function loop(): \Generator public function loop()
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;

View File

@ -26,7 +26,6 @@ use danog\MadelineProto\Loop\Impl\SignalLoop;
use danog\MadelineProto\MTProtoTools\Crypt; use danog\MadelineProto\MTProtoTools\Crypt;
use danog\MadelineProto\NothingInTheSocketException; use danog\MadelineProto\NothingInTheSocketException;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
use function Amp\call;
/** /**
* Socket read loop. * Socket read loop.
@ -38,7 +37,7 @@ class ReadLoop extends SignalLoop
use Tools; use Tools;
use Crypt; use Crypt;
public function loop(): \Generator public function loop()
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;
@ -46,7 +45,7 @@ class ReadLoop extends SignalLoop
$this->startedLoop(); $this->startedLoop();
$API->logger->logger("Entered read loop in DC {$datacenter}", Logger::ULTRA_VERBOSE); $API->logger->logger("Entered read loop in DC {$datacenter}", Logger::ULTRA_VERBOSE);
$timeout = $API->settings['connection_settings'][isset($API->settings['connection_settings'][$datacenter]) ? $datacenter : 'all']['timeout']; //$timeout = $API->settings['connection_settings'][isset($API->settings['connection_settings'][$datacenter]) ? $datacenter : 'all']['timeout'];
while (true) { while (true) {
try { try {
@ -110,12 +109,7 @@ class ReadLoop extends SignalLoop
} }
} }
public function readMessage(): Promise public function readMessage()
{
return call([$this, 'readMessageAsync']);
}
public function readMessageAsync(): \Generator
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;

View File

@ -31,7 +31,7 @@ class UpdateLoop extends ResumableSignalLoop
{ {
use \danog\MadelineProto\Tools; use \danog\MadelineProto\Tools;
public function loop(): \Generator public function loop()
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;
@ -57,7 +57,7 @@ class UpdateLoop extends ResumableSignalLoop
} }
} }
if (time() - $API->last_getdifference > $timeout) { if (time() - $API->last_getdifference > $timeout) {
if (!yield $this->call($API->get_updates_difference_async())) { if (!yield $API->get_updates_difference_async()) {
return false; return false;
} }
} }

View File

@ -72,7 +72,7 @@ class WriteLoop extends ResumableSignalLoop
} }
} }
public function unencryptedWriteLoopAsync(): \Generator public function unencryptedWriteLoopAsync()
{ {
$API = $this->API; $API = $this->API;
$datacenter = $this->datacenter; $datacenter = $this->datacenter;

View File

@ -64,9 +64,10 @@ abstract class ResumableSignalLoop extends SignalLoop implements ResumableLoopIn
return; return;
} }
} }
/*
if ($expected) { if ($expected) {
//var_dump("=======", "resume $watcherId ".get_class($this)." DC {$this->datacenter} diff ".(microtime(true) - $expected).": expected $expected, actual ".microtime(true)); //var_dump("=======", "resume $watcherId ".get_class($this)." DC {$this->datacenter} diff ".(microtime(true) - $expected).": expected $expected, actual ".microtime(true));
} }*/
if ($this->resume) { if ($this->resume) {
$resume = $this->resume; $resume = $this->resume;
$this->resume = null; $this->resume = null;

View File

@ -37,5 +37,5 @@ interface LoopInterface
* *
* @return void * @return void
*/ */
public function loop(): \Generator; public function loop();
} }

View File

@ -25,7 +25,6 @@ use Amp\Success;
use danog\MadelineProto\Exception; use danog\MadelineProto\Exception;
use danog\MadelineProto\TL\TLCallback; use danog\MadelineProto\TL\TLCallback;
use danog\MadelineProto\Tools; use danog\MadelineProto\Tools;
use function Amp\call;
/** /**
* Manages upload and download of files. * Manages upload and download of files.
@ -460,19 +459,14 @@ class ReferenceDatabase implements TLCallback
return $this->refreshReferenceInternal($this->serializeLocation($locationType, $location)); return $this->refreshReferenceInternal($this->serializeLocation($locationType, $location));
} }
public function refreshReferenceInternal(string $location): Promise public function refreshReferenceInternal(string $location)
{ {
if (isset($this->refreshed[$location])) { if (isset($this->refreshed[$location])) {
$this->API->logger->logger('Reference already refreshed!', \danog\MadelineProto\Logger::VERBOSE); $this->API->logger->logger('Reference already refreshed!', \danog\MadelineProto\Logger::VERBOSE);
return new Success($this->db[$location]['reference']); return $this->db[$location]['reference'];
} }
return call([$this, 'refreshReferenceInternalGenerator'], $location);
}
public function refreshReferenceInternalGenerator(string $location): \Generator
{
ksort($this->db[$location]['origins']); ksort($this->db[$location]['origins']);
$count = 0; $count = 0;
@ -497,7 +491,6 @@ class ReferenceDatabase implements TLCallback
$this->API->full_chats[$origin['peer']]['last_update'] = 0; $this->API->full_chats[$origin['peer']]['last_update'] = 0;
} }
$this->API->get_full_info($origin['peer']); $this->API->get_full_info($origin['peer']);
yield new Success(0);
break; break;
// Peer (default photo ID) // Peer (default photo ID)
case self::USER_PHOTO_ORIGIN: case self::USER_PHOTO_ORIGIN:
@ -546,14 +539,14 @@ class ReferenceDatabase implements TLCallback
return $deferred->promise(); return $deferred->promise();
} }
public function getReference(int $locationType, array $location): Promise public function getReference(int $locationType, array $location)
{ {
$locationString = $this->serializeLocation($locationType, $location); $locationString = $this->serializeLocation($locationType, $location);
if (!isset($this->db[$locationString]['reference'])) { if (!isset($this->db[$locationString]['reference'])) {
if (isset($location['file_reference'])) { if (isset($location['file_reference'])) {
$this->API->logger->logger("Using outdated file reference for location of type $locationType object {$location['_']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE); $this->API->logger->logger("Using outdated file reference for location of type $locationType object {$location['_']}", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
return new Success($location['file_reference']); return $location['file_reference'];
} }
throw new \danog\MadelineProto\Exception("Could not find file reference for location of type $locationType object {$location['_']}"); throw new \danog\MadelineProto\Exception("Could not find file reference for location of type $locationType object {$location['_']}");
@ -564,7 +557,7 @@ class ReferenceDatabase implements TLCallback
return $this->refreshReferenceInternal($locationString); return $this->refreshReferenceInternal($locationString);
} }
return new Success($this->db[$locationString]['reference']); return $this->db[$locationString]['reference'];
} }
private function serializeLocation(int $locationType, array $location) private function serializeLocation(int $locationType, array $location)