Пример #1
0
/**
 * Execute a callback when a signal is received.
 * Returned Generators are run as coroutines. Failures of the coroutine are forwarded to the loop error handler.
 *
 * @see \Interop\Async\Loop::onSignal()
 *
 * @param int $signo The signal number to monitor.
 * @param callable(string $watcherId, int $signo, mixed $data) $callback The callback to execute.
 * @param mixed $data
 *
 * @return string Watcher identifier.
 */
function onSignal(int $signo, callable $callback, $data = null) : string
{
    return Loop::onSignal($signo, function ($watcherId, $signo, $data) use($callback) {
        $result = $callback($watcherId, $signo, $data);
        if ($result instanceof \Generator) {
            rethrow(new Coroutine($result));
        }
    }, $data);
}