From dbb560f13e9c15c7bfc96c7ae44bb3ae74c75fe7 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 6 Jun 2019 20:13:45 +0200 Subject: [PATCH] Update docs --- docs/docs/ASYNC.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++ docs/index.md | 1 + 2 files changed, 60 insertions(+) diff --git a/docs/docs/ASYNC.md b/docs/docs/ASYNC.md index 63bd41ec..dfa70c03 100644 --- a/docs/docs/ASYNC.md +++ b/docs/docs/ASYNC.md @@ -16,6 +16,7 @@ Powered by [amphp](https://amphp.org), MadelineProto wraps the AMPHP APIs to pro * [Async in callback handler](#async-in-callback-handler) * [Wrapped async](#wrapped-async) * [Multiple async](#multiple-async) + * [ArrayAccess async](#arrayaccess-async) * [Ignored async](#ignored-async) * [Blocking async](#blocking-async) * [MadelineProto and AMPHP async APIs](#madelineproto-and-amphp-async-apis) @@ -194,6 +195,64 @@ The result of this will be an array of results, whose type is determined by the The order of method calls can be guaranteed (server-side, not by MadelineProto) by using [call queues](USING_METHODS.html#queues). +### ArrayAccess async + +You can do async ArrayAccess on promises. + +Now instead of doing this: +```php +$id = (yield $MadelineProto->getPwrChat('danogentili'))['bot_api_id']; +``` + +You can simply do this: +```php +$id = yield $MadelineProto->getPwrChat('danogentili')['bot_api_id']; +``` + +Setting attributes asynchronously is also supported (it's kind of useless, but it's useful if you have some custom logic, like for example a method that returns a custom ArrayAccess class with a set method that does something magical). +```php +$Id = yield $MadelineProto->getPwrChat('danogentili')['bot_api_id'] = 'pony'; +``` + +isset and unset aren't supported due to the fact that in PHP, isset and unset aren't proper functions but language constructs (logically). +You have to do this, instead: +```php +$set = isset((yield $MadelineProto->getPwrChat('danogentili'))['bot_api_id']); +``` + +or +```php +$result = yield $MadelineProto->getPwrChat('danogentili'); +$set = isset($result['bot_api_id']); +``` + +Also, `ArrayAccess` on raw generators still isn't supported unless you wrap them in a coroutine using `$MadelineProto->call`: +```php +public function ponyAsync() +{ + return yield $MadelineProto->get_info('danogentili'); +} +// public function onUpdateNewMessage.... + +// WILL NOT WORK +$set = yield $this->ponyAsync()['id']; + +// Will work +$set = (yield $this->ponyAsync())['id']; + +// Will work +$set = $MadelineProto->call(yield $this->ponyAsync())['id']; + +public function pony() +{ + return $MadelineProto->call($this->ponyAsync()); +} + +// Will work +$set = yield $this->pony()['id']; +``` + + ### Blocking async ```php $result = blocking_function(); diff --git a/docs/index.md b/docs/index.md index 812446ab..5bd955e5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -70,6 +70,7 @@ Tip: if you receive an error (or nothing), [send us](https://t.me/pwrtelegramgro * [Async in callback handler](https://docs.madelineproto.xyz/docs/ASYNC.html#async-in-callback-handler) * [Wrapped async](https://docs.madelineproto.xyz/docs/ASYNC.html#wrapped-async) * [Multiple async](https://docs.madelineproto.xyz/docs/ASYNC.html#multiple-async) + * [ArrayAccess async](https://docs.madelineproto.xyz/docs/ASYNC.html#arrayaccess-async) * [Ignored async](https://docs.madelineproto.xyz/docs/ASYNC.html#ignored-async) * [Blocking async](https://docs.madelineproto.xyz/docs/ASYNC.html#blocking-async) * [MadelineProto and AMPHP async APIs](https://docs.madelineproto.xyz/docs/ASYNC.html#madelineproto-and-amphp-async-apis)