Skip to content

Commit

Permalink
Use \Closure instead of callable (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik authored Dec 1, 2021
1 parent 2683b2e commit 498362a
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 390 deletions.
59 changes: 29 additions & 30 deletions src/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,37 @@ protected function now(): float
/**
* Queue a microtask.
*
* The queued callable MUST be executed immediately once the event loop gains control. Order of queueing MUST be
* The queued callback MUST be executed immediately once the event loop gains control. Order of queueing MUST be
* preserved when executing the callbacks. Recursive scheduling can thus result in infinite loops, use with care.
*
* Does NOT create an event callback, thus CAN NOT be marked as disabled or unreferenced.
* Use {@see EventLoop::defer()} if you need these features.
*
* @param callable $callback The callback to queue.
* @param \Closure $closure The callback to queue.
* @param mixed ...$args The callback arguments.
*/
public static function queue(callable $callback, mixed ...$args): void
public static function queue(\Closure $closure, mixed ...$args): void
{
self::getDriver()->queue($callback, ...$args);
self::getDriver()->queue($closure, ...$args);
}

/**
* Defer the execution of a callback.
*
* The deferred callable MUST be executed before any other type of callback in a tick. Order of enabling MUST be
* The deferred callback MUST be executed before any other type of callback in a tick. Order of enabling MUST be
* preserved when executing the callbacks.
*
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Deferred callbacks MUST NOT be called in the tick they were enabled.
*
* @param callable(string) $callback The callback to defer. The `$callbackId` will be
* @param \Closure(string):void $closure The callback to defer. The `$callbackId` will be
* invalidated before the callback invocation.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function defer(callable $callback): string
public static function defer(\Closure $closure): string
{
return self::getDriver()->defer($callback);
return self::getDriver()->defer($closure);
}

/**
Expand All @@ -110,14 +110,14 @@ public static function defer(callable $callback): string
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param float $delay The amount of time, in seconds, to delay the execution for.
* @param callable(string) $callback The callback to delay. The `$callbackId` will be invalidated before
* the callback invocation.
* @param \Closure(string):void $closure The callback to delay. The `$callbackId` will be invalidated
* before the callback invocation.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function delay(float $delay, callable $callback): string
public static function delay(float $delay, \Closure $closure): string
{
return self::getDriver()->delay($delay, $callback);
return self::getDriver()->delay($delay, $closure);
}

/**
Expand All @@ -131,13 +131,13 @@ public static function delay(float $delay, callable $callback): string
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param float $interval The time interval, in seconds, to wait between executions.
* @param callable(string) $callback The callback to repeat.
* @param \Closure(string):void $closure The callback to repeat.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function repeat(float $interval, callable $callback): string
public static function repeat(float $interval, \Closure $closure): string
{
return self::getDriver()->repeat($interval, $callback);
return self::getDriver()->repeat($interval, $closure);
}

/**
Expand All @@ -154,13 +154,13 @@ public static function repeat(float $interval, callable $callback): string
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param resource|object $stream The stream to monitor.
* @param callable(string, resource|object) $callback The callback to execute.
* @param \Closure(string, resource|object):void $callback The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function onReadable(mixed $stream, callable $callback): string
public static function onReadable(mixed $stream, \Closure $closure): string
{
return self::getDriver()->onReadable($stream, $callback);
return self::getDriver()->onReadable($stream, $closure);
}

/**
Expand All @@ -177,13 +177,13 @@ public static function onReadable(mixed $stream, callable $callback): string
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param resource|object $stream The stream to monitor.
* @param callable(string, resource|object) $callback The callback to execute.
* @param \Closure(string, resource|object):void $closure The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public static function onWritable(mixed $stream, callable $callback): string
public static function onWritable(mixed $stream, \Closure $closure): string
{
return self::getDriver()->onWritable($stream, $callback);
return self::getDriver()->onWritable($stream, $closure);
}

/**
Expand All @@ -198,16 +198,16 @@ public static function onWritable(mixed $stream, callable $callback): string
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param int $signo The signal number to monitor.
* @param callable(string, int) $callback The callback to execute.
* @param int $signal The signal number to monitor.
* @param \Closure(string, int):void $closure The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @throws UnsupportedFeatureException If signal handling is not supported.
*/
public static function onSignal(int $signo, callable $callback): string
public static function onSignal(int $signal, \Closure $closure): string
{
return self::getDriver()->onSignal($signo, $callback);
return self::getDriver()->onSignal($signal, $closure);
}

/**
Expand Down Expand Up @@ -299,14 +299,13 @@ public static function unreference(string $callbackId): string
*
* Subsequent calls to this method will overwrite the previous handler.
*
* @param callable(\Throwable)|null $callback The callback to execute. `null` will clear the
* current handler.
* @param (\Closure(\Throwable):void)|null $closure The callback to execute. `null` will clear the current handler.
*
* @return callable(\Throwable)|null The previous handler, `null` if there was none.
* @return (\Closure(\Throwable):void)|null The previous handler, `null` if there was none.
*/
public static function setErrorHandler(callable $callback = null): ?callable
public static function setErrorHandler(\Closure $closure = null): ?\Closure
{
return self::getDriver()->setErrorHandler($callback);
return self::getDriver()->setErrorHandler($closure);
}

/**
Expand Down
53 changes: 26 additions & 27 deletions src/EventLoop/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,32 @@ public function isRunning(): bool;
/**
* Queue a microtask.
*
* The queued callable MUST be executed immediately once the event loop gains control. Order of queueing MUST be
* The queued callback MUST be executed immediately once the event loop gains control. Order of queueing MUST be
* preserved when executing the callbacks. Recursive scheduling can thus result in infinite loops, use with care.
*
* Does NOT create an event callback, thus CAN NOT be marked as disabled or unreferenced.
* Use {@see EventLoop::defer()} if you need these features.
*
* @param callable $callback The callback to queue.
* @param \Closure $closure The callback to queue.
* @param mixed ...$args The callback arguments.
*/
public function queue(callable $callback, mixed ...$args): void;
public function queue(\Closure $closure, mixed ...$args): void;

/**
* Defer the execution of a callback.
*
* The deferred callable MUST be executed before any other type of callback in a tick. Order of enabling MUST be
* The deferred callback MUST be executed before any other type of callback in a tick. Order of enabling MUST be
* preserved when executing the callbacks.
*
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param callable(string):void $callback The callback to defer. The `$callbackId` will be
* invalidated before the callback invocation.
* @param \Closure(string):void $closure The callback to defer. The `$callbackId` will be invalidated before the
* callback invocation.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public function defer(callable $callback): string;
public function defer(\Closure $closure): string;

/**
* Delay the execution of a callback.
Expand All @@ -80,13 +80,13 @@ public function defer(callable $callback): string;
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param float $delay The amount of time, in seconds, to delay the execution for.
* @param callable(string):void $callback The callback to delay. The `$callbackId` will be
* invalidated before the callback invocation.
* @param float $delay The amount of time, in seconds, to delay the execution for.
* @param \Closure(string):void $closure The callback to delay. The `$callbackId` will be invalidated before the
* callback invocation.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public function delay(float $delay, callable $callback): string;
public function delay(float $delay, \Closure $closure): string;

/**
* Repeatedly execute a callback.
Expand All @@ -98,12 +98,12 @@ public function delay(float $delay, callable $callback): string;
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param float $interval The time interval, in seconds, to wait between executions.
* @param callable(string):void $callback The callback to repeat.
* @param float $interval The time interval, in seconds, to wait between executions.
* @param \Closure(string):void $closure The callback to repeat.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public function repeat(float $interval, callable $callback): string;
public function repeat(float $interval, \Closure $closure): string;

/**
* Execute a callback when a stream resource becomes readable or is closed for reading.
Expand All @@ -119,11 +119,11 @@ public function repeat(float $interval, callable $callback): string;
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param resource|object $stream The stream to monitor.
* @param callable(string, resource|object):void $callback The callback to execute.
* @param \Closure(string, resource|object):void $closure The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public function onReadable(mixed $stream, callable $callback): string;
public function onReadable(mixed $stream, \Closure $closure): string;

/**
* Execute a callback when a stream resource becomes writable or is closed for writing.
Expand All @@ -139,11 +139,11 @@ public function onReadable(mixed $stream, callable $callback): string;
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param resource|object $stream The stream to monitor.
* @param callable(string, resource|object):void $callback The callback to execute.
* @param \Closure(string, resource|object):void $closure The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*/
public function onWritable(mixed $stream, callable $callback): string;
public function onWritable(mixed $stream, \Closure $closure): string;

/**
* Execute a callback when a signal is received.
Expand All @@ -157,20 +157,20 @@ public function onWritable(mixed $stream, callable $callback): string;
* The created callback MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param int $signo The signal number to monitor.
* @param callable(string, int):void $callback The callback to execute.
* @param int $signal The signal number to monitor.
* @param \Closure(string, int):void $closure The callback to execute.
*
* @return string A unique identifier that can be used to cancel, enable or disable the callback.
*
* @throws UnsupportedFeatureException If signal handling is not supported.
*/
public function onSignal(int $signo, callable $callback): string;
public function onSignal(int $signal, \Closure $closure): string;

/**
* Enable a callback to be active starting in the next tick.
*
* Callbacks MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
* the next tick. Callbacks MUST NOT be called in the tick they were enabled.
* Callbacks MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right
* before the next tick. Callbacks MUST NOT be called in the tick they were enabled.
*
* @param string $callbackId The callback identifier.
*
Expand Down Expand Up @@ -240,12 +240,11 @@ public function unreference(string $callbackId): string;
*
* Subsequent calls to this method will overwrite the previous handler.
*
* @param (callable(\Throwable):void)|null $callback The callback to execute. `null` will clear the
* current handler.
* @param (\Closure(\Throwable):void)|null $closure The callback to execute. `null` will clear the current handler.
*
* @return (callable(\Throwable):void)|null The previous handler, `null` if there was none.
* @return (\Closure(\Throwable):void)|null The previous handler, `null` if there was none.
*/
public function setErrorHandler(callable $callback = null): ?callable;
public function setErrorHandler(?\Closure $closure = null): ?callable;

/**
* Get the underlying loop handle.
Expand Down
4 changes: 2 additions & 2 deletions src/EventLoop/Driver/StreamSelectDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public function __destruct()
*
* @throws UnsupportedFeatureException If the pcntl extension is not available.
*/
public function onSignal(int $signo, callable $callback): string
public function onSignal(int $signal, \Closure $closure): string
{
if (!$this->signalHandling) {
throw new UnsupportedFeatureException("Signal handling requires the pcntl extension");
}

return parent::onSignal($signo, $callback);
return parent::onSignal($signal, $closure);
}

/**
Expand Down
Loading

0 comments on commit 498362a

Please sign in to comment.