From c66e1fcbef803115ae05dfb162a2aefae54f1b26 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sun, 22 Sep 2019 00:06:14 +0200 Subject: [PATCH] Return coroutines, will implement object access in promises in amphp --- src/danog/MadelineProto/Coroutine.php | 36 +++++++++------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/danog/MadelineProto/Coroutine.php b/src/danog/MadelineProto/Coroutine.php index 1098234e..a82de6ec 100644 --- a/src/danog/MadelineProto/Coroutine.php +++ b/src/danog/MadelineProto/Coroutine.php @@ -203,14 +203,10 @@ final class Coroutine implements Promise, \ArrayAccess */ public function offsetGet($offset) { - $deferred = new Deferred; - $this->onResolve(static function ($e, $v) use ($deferred, $offset) { - if ($e) { - return $deferred->fail($e); - } - $deferred->resolve($v[$offset]); - }); - return $deferred->promise(); + return Tools::call((function () use ($offset) { + $result = yield $this; + return $result[$offset]; + })()); } public function offsetSet($offset, $value) { @@ -239,25 +235,17 @@ final class Coroutine implements Promise, \ArrayAccess */ public function __get(string $offset) { - $deferred = new Deferred; - $this->onResolve(static function ($e, $v) use ($deferred, $offset) { - if ($e) { - return $deferred->fail($e); - } - $deferred->resolve($v->{$offset}); - }); - return $deferred->promise(); + return Tools::call((function () use ($offset) { + $result = yield $this; + return $result->{$offset}; + })()); } public function __call(string $name, array $arguments) { - $deferred = new Deferred; - $this->onResolve(static function ($e, $v) use ($deferred, $name, $arguments) { - if ($e) { - return $deferred->fail($e); - } - $deferred->resolve($v->{$name}(...$arguments)); - }); - return $deferred->promise(); + return Tools::call((function () use ($name, $arguments) { + $result = yield $this; + return $result->{$name}($arguments); + })()); } /** * Get stacktrace from when the generator was started.