2020-07-31 15:09:34 +02:00
# AsyncConstruct
![Build status ](https://github.com/danog/asyncConstruct/workflows/build/badge.svg )
[![codecov ](https://codecov.io/gh/danog/asyncConstruct/branch/master/graph/badge.svg )](https://codecov.io/gh/danog/asyncConstruct)
![License ](https://img.shields.io/badge/license-MIT-blue.svg )
`danog/asyncConstruct` provides async class constructor and wakeup functions for AMPHP.
## Installation
```bash
composer require danog/async-construct
```
## API
* Async
* [AsyncConstruct ](#AsyncConstruct )
* [AsyncWakeup ](#AsyncWakeup )
* Blocking
* [BlockingConstruct ](#BlockingConstruct )
* [BlockingWakeup ](#BlockingWakeup )
### AsyncConstruct
2020-07-31 15:43:00 +02:00
[Example ](https://github.com/danog/asyncConstruct/blob/master/examples/async.php )
2020-07-31 15:09:34 +02:00
Simply `use` this trait in your class, and define a `__construct_async` async method.
The trait will automatically run the async method on construction, and expose a set of APIs to wait on the result of the constructor for initialization.
2020-07-31 15:44:16 +02:00
#### `init()`
2020-07-31 15:09:34 +02:00
Will blockingly wait for execution of the async constructor, returning its return value.
2020-07-31 15:44:16 +02:00
#### `initAsynchronously()`
2020-07-31 15:09:34 +02:00
Will return a promise that, when `yield` ed, will wait for and return the return value of the constructor.
2020-07-31 15:44:16 +02:00
#### `inited()`
2020-07-31 15:09:34 +02:00
Returns a boolean, indicating whether the class was initialized or not.
### BlockingConstruct
2020-07-31 15:43:00 +02:00
[Example ](https://github.com/danog/asyncConstruct/blob/master/examples/blocking.php )
2020-07-31 15:09:34 +02:00
Exactly like [AsyncConstruct], except that the blocking `init()` function will be automatically called on construction.
### AsyncWakeup
2020-07-31 15:43:00 +02:00
[Example ](https://github.com/danog/asyncConstruct/blob/master/examples/asyncWakeup.php )
2020-07-31 15:09:34 +02:00
Simply `use` this trait in your class, and define a `__wakeup_async` async method.
The trait will automatically run the async method on deserialization, and expose a set of APIs to wait on the result of the wakeup function for re-initialization.
2020-07-31 15:44:16 +02:00
#### `wakeup()`
2020-07-31 15:09:34 +02:00
Will blockingly wait for execution of the async wakeup function, returning its return value.
2020-07-31 15:44:16 +02:00
#### `wakeupAsynchronously()`
2020-07-31 15:09:34 +02:00
Will return a promise that, when `yield` ed, will wait for and return the return value of the wakeup function.
2020-07-31 15:44:16 +02:00
#### `wokenUp()`
2020-07-31 15:09:34 +02:00
Returns a boolean, indicating whether the class was initialized or not.
### BlockingWakeup
2020-07-31 15:43:00 +02:00
[Example ](https://github.com/danog/asyncConstruct/blob/master/examples/blockingWakeup.php )
2020-07-31 15:09:34 +02:00
2020-07-31 15:10:53 +02:00
Exactly like [AsyncWakeup ](#AsyncWakeup ), except that the blocking `wakeup()` function will be automatically called on wakeup.