Skip to content

Commit

Permalink
Improve type definition for Amp\call
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Apr 19, 2020
1 parent 2053b42 commit 8ee469b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -41,7 +41,7 @@
"amphp/php-cs-fixer-config": "dev-master",
"react/promise": "^2",
"phpunit/phpunit": "^6.0.9 | ^7",
"vimeo/psalm": "^3.9@dev",
"vimeo/psalm": "^3.11@dev",
"jetbrains/phpstorm-stubs": "^2019.3"
},
"autoload": {
Expand Down
2 changes: 2 additions & 0 deletions lib/Loop/NativeDriver.php
Expand Up @@ -361,6 +361,8 @@ protected function deactivate(Watcher $watcher)
break;

case Watcher::SIGNAL:
\assert(\is_int($watcher->value));

if (isset($this->signalWatchers[$watcher->value])) {
unset($this->signalWatchers[$watcher->value][$watcher->id]);

Expand Down
9 changes: 9 additions & 0 deletions lib/Loop/UvDriver.php
Expand Up @@ -226,13 +226,16 @@ protected function activate(array $watchers)
switch ($watcher->type) {
case Watcher::READABLE:
case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));

$streamId = (int) $watcher->value;

if (isset($this->streams[$streamId])) {
$event = $this->streams[$streamId];
} elseif (isset($this->events[$id])) {
$event = $this->streams[$streamId] = $this->events[$id];
} else {
/** @psalm-suppress UndefinedFunction */
$event = $this->streams[$streamId] = \uv_poll_init_socket($this->handle, $watcher->value);
}

Expand All @@ -249,6 +252,8 @@ protected function activate(array $watchers)

case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));

if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
Expand All @@ -266,14 +271,18 @@ protected function activate(array $watchers)
break;

case Watcher::SIGNAL:
\assert(\is_int($watcher->value));

if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
/** @psalm-suppress UndefinedFunction */
$event = $this->events[$id] = \uv_signal_init($this->handle);
}

$this->watchers[(int) $event] = [$watcher];

/** @psalm-suppress UndefinedFunction */
\uv_signal_start($event, $this->signalCallback, $watcher->value);
break;

Expand Down
40 changes: 30 additions & 10 deletions lib/functions.php
Expand Up @@ -13,11 +13,14 @@
* Use this function to create a coroutine-aware callable for a promise-aware callback caller.
*
* @template TReturn
* @template TGeneratorReturn
*
* @param callable(...mixed):(\Generator<mixed,Promise|ReactPromise|array<array-key, Promise|ReactPromise>,mixed,TReturn>|TReturn) $callback
* @param callable(...mixed):mixed $callback
*
* @return callable
* @psalm-return (TReturn is Promise ? (callable(...mixed): TReturn) : (callable(...mixed):Promise<TReturn>))
* @psalm-return callable(...mixed):Promise
*
* @todo Fix return type once https://github.com/vimeo/psalm/issues/3071 has been fixed
*
* @see asyncCoroutine()
*/
Expand All @@ -36,11 +39,12 @@ function coroutine(callable $callback): callable
* Use this function to create a coroutine-aware callable for a non-promise-aware callback caller.
*
* @template TReturn
* @template TGeneratorReturn
*
* @param callable(...mixed):(\Generator<mixed,Promise|ReactPromise|array<array-key, Promise|ReactPromise>,mixed,TReturn>|TReturn) $callback
* @param callable(...mixed):mixed $callback
*
* @return callable
* @psalm-return callable(...mixed): void
* @psalm-return callable(...mixed):void
*
* @see coroutine()
*/
Expand All @@ -56,12 +60,19 @@ function asyncCoroutine(callable $callback): callable
* coroutine. If the function throws, a failed promise will be returned.
*
* @template TReturn
* @template TReturnPromise
* @template TGenerator
* @template TGeneratorPromise
*
* @formatter:off
*
* @param callable(...mixed):(\Generator<mixed,Promise|ReactPromise|array<array-key, Promise|ReactPromise>,mixed,TReturn>|TReturn) $callback
* @param callable(...mixed):(TReturn|Promise<TReturnPromise>|\Generator<mixed, mixed, mixed, TGenerator|Promise<TGeneratorPromise>>) $callback
* @param mixed ...$args Arguments to pass to the function.
*
* @return Promise
* @psalm-return (TReturn is Promise ? TReturn : Promise<TReturn>)
* @psalm-return (TReturn is Promise ? Promise<TReturnPromise> : (TReturn is \Generator ? (TGenerator is Promise ? Promise<TGeneratorPromise> : Promise<TGenerator>) : Promise<TReturn>))
*
* @formatter:on
*/
function call(callable $callback, ...$args): Promise
{
Expand Down Expand Up @@ -91,12 +102,19 @@ function call(callable $callback, ...$args): Promise
* throws or returns a failing promise, the failure is forwarded to the loop error handler.
*
* @template TReturn
* @template TReturnPromise
* @template TGenerator
* @template TGeneratorPromise
*
* @param callable(...mixed):(\Generator<mixed,Promise|ReactPromise|array<array-key, Promise|ReactPromise>,mixed,TReturn>|TReturn) $callback
* @formatter:off
*
* @param callable(...mixed):(TReturn|Promise<TReturnPromise>|\Generator<mixed, mixed, mixed, TGenerator|Promise<TGeneratorPromise>>) $callback
* @param mixed ...$args Arguments to pass to the function.
*
* @return void
*
* @formatter:on
*
* @throws \TypeError
*/
function asyncCall(callable $callback, ...$args)
Expand Down Expand Up @@ -345,13 +363,15 @@ function any(array $promises): Promise
*
* @param Promise[]|ReactPromise[] $promises Array of only promises.
*
* @psalm-param array<array-key, Promise|ReactPromise> $promises
*
* @return Promise
*
* @throws \Error If a non-Promise is in the array.
*
* @psalm-assert (Promise|ReactPromise)[] $promises
* @template TValue
*
* @psalm-param array<array-key, Promise<TValue>|ReactPromise> $promises
* @psalm-assert array<array-key, Promise<TValue>|ReactPromise> $promises $promises
* @psalm-return Promise<array<array-key, TValue>>
*/
function all(array $promises): Promise
{
Expand Down

0 comments on commit 8ee469b

Please sign in to comment.