1
0
mirror of https://github.com/danog/amp.git synced 2024-12-03 09:57:51 +01:00
amp/lib/Reactor.php

122 lines
4.5 KiB
PHP
Raw Normal View History

2013-08-05 22:05:08 +02:00
<?php
2014-09-23 04:38:32 +02:00
namespace Amp;
2013-08-05 22:05:08 +02:00
interface Reactor {
/**
* Start the event reactor and assume program flow control
2014-06-11 18:21:46 +02:00
*
2015-03-19 16:14:21 +01:00
* @param callable $onStart An optional callback to invoke upon event loop start
* @return void
2013-08-05 22:05:08 +02:00
*/
2014-08-02 08:10:31 +02:00
public function run(callable $onStart = null);
2013-08-05 22:05:08 +02:00
/**
* Execute a single event loop iteration
2014-11-26 19:42:36 +01:00
*
2015-03-19 16:14:21 +01:00
* @param bool $noWait Should tick return immediately if no watchers are ready to trigger?
* @return void
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function tick($noWait = false);
2013-08-05 22:05:08 +02:00
/**
* Stop the event reactor
2015-03-19 16:14:21 +01:00
*
* @return void
2013-08-05 22:05:08 +02:00
*/
2014-02-23 22:26:28 +01:00
public function stop();
2013-08-05 22:05:08 +02:00
/**
* Schedule a callback for immediate invocation in the next event loop iteration
*
* @param callable $callback A callback to invoke in the next iteration of the event loop
* @param array $options Watcher options
2015-03-19 16:14:21 +01:00
* @return string Returns unique (to the process) string watcher ID
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function immediately(callable $callback, array $options = []);
2013-08-05 22:05:08 +02:00
/**
* Schedule a callback to execute once
*
* @param callable $callback A callback to invoke after the specified millisecond delay
* @param int $msDelay the number of milliseconds to wait before invoking $callback
* @param array $options Watcher options
2015-03-19 16:14:21 +01:00
* @return string Returns unique (to the process) string watcher ID
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function once(callable $callback, $msDelay, array $options = []);
2013-08-05 22:05:08 +02:00
/**
* Schedule a recurring callback to execute every $interval seconds until cancelled
*
* @param callable $callback A callback to invoke at the $msDelay interval until cancelled
* @param int $msInterval The interval at which to repeat $callback invocations
* @param array $options Watcher options
2015-03-19 16:14:21 +01:00
* @return string Returns unique (to the process) string watcher ID
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function repeat(callable $callback, $msInterval, array $options = []);
2013-11-27 17:56:29 +01:00
2013-08-05 22:05:08 +02:00
/**
* Watch a stream resource for readable data and trigger the callback when actionable
2013-08-05 22:05:08 +02:00
*
2015-03-19 16:14:21 +01:00
* @param resource $stream The stream resource to watch for readability
* @param callable $callback A callback to invoke when the stream reports as readable
* @param array $options Watcher options
2015-03-19 16:14:21 +01:00
* @return string Returns unique (to the process) string watcher ID
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function onReadable($stream, callable $callback, array $options = []);
2013-08-05 22:05:08 +02:00
/**
* Watch a stream resource to become writable and trigger the callback when actionable
*
2015-03-19 16:14:21 +01:00
* @param resource $stream The stream resource to watch for writability
* @param callable $callback A callback to invoke when the stream reports as writable
* @param array $options Watcher options
2015-03-19 16:14:21 +01:00
* @return string Returns unique (to the process) string watcher ID
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function onWritable($stream, callable $callback, array $options = []);
2013-08-05 22:05:08 +02:00
/**
* Cancel an existing timer/stream watcher
*
2015-03-19 16:14:21 +01:00
* @param string $watcherId The watcher ID to be canceled
* @return void
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function cancel($watcherId);
2013-08-05 22:05:08 +02:00
/**
* Temporarily disable (but don't cancel) an existing timer/stream watcher
*
2015-03-19 16:14:21 +01:00
* @param string $watcherId The watcher ID to be disabled
* @return void
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function disable($watcherId);
2013-08-05 22:05:08 +02:00
/**
* Enable a disabled timer/stream watcher
*
2015-03-19 16:14:21 +01:00
* @param string $watcherId The watcher ID to be enabled
* @return void
2013-08-05 22:05:08 +02:00
*/
2015-04-30 19:41:14 +02:00
public function enable($watcherId);
/**
* An optional "last-chance" exception handler for errors resulting during callback invocation
*
2015-03-19 16:14:21 +01:00
* If an application throws inside the event loop and no onError callback is specified the
* exception bubbles up and the event loop is stopped. This is undesirable in long-running
* applications (like servers) where stopping the event loop for an application error is
* problematic. Amp applications can instead specify the onError callback to handle uncaught
* exceptions without stopping the event loop.
2015-03-19 16:14:21 +01:00
*
2015-04-03 18:52:17 +02:00
* Additionally, generator callbacks which are auto-resolved by the event reactor may fail.
* Coroutine resolution failures are treated like uncaught exceptions and stop the event reactor
* if no onError callback is specified to handle these situations.
*
2015-03-19 16:14:21 +01:00
* onError callback functions are passed a single parameter: the uncaught exception.
*
* @param callable $callback A callback to invoke when an exception occurs inside the event loop
2015-03-19 16:14:21 +01:00
* @return void
*/
public function onError(callable $callback);
2013-08-05 22:05:08 +02:00
}