1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 02:17:54 +01:00
amp/lib/Reactor.php

129 lines
4.4 KiB
PHP
Raw Normal View History

2013-08-05 22:05:08 +02:00
<?php
namespace Alert;
interface Reactor {
2014-06-11 18:21:46 +02:00
const POLL_READ = 1;
const POLL_WRITE = 2;
const POLL_SOCK = 4;
const ENABLE_NOW = 8;
2013-08-05 22:05:08 +02:00
/**
* Start the event reactor and assume program flow control
2014-06-11 18:21:46 +02:00
*
* @param $onStart Optional callback to invoke immediately upon reactor start
2013-08-05 22:05:08 +02:00
*/
2014-02-23 22:26:28 +01:00
public function run(callable $onStart = NULL);
2013-08-05 22:05:08 +02:00
/**
* Execute a single event loop iteration
*/
2014-02-23 22:26:28 +01:00
public function tick();
2013-08-05 22:05:08 +02:00
/**
* Stop the event reactor
*/
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
*
2013-11-27 18:05:08 +01:00
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param callable $callback Any valid PHP callable
*/
2014-02-23 22:26:28 +01:00
public function immediately(callable $callback);
2013-08-05 22:05:08 +02:00
/**
* Schedule a callback to execute once
*
2014-06-11 18:21:46 +02:00
* Time intervals are measured in milliseconds.
2013-11-27 18:05:08 +01:00
*
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param callable $callback Any valid PHP callable
2014-06-11 18:21:46 +02:00
* @param int $msDelay The delay in milliseconds before the callback will trigger (may be zero)
2013-08-05 22:05:08 +02:00
*/
2014-06-11 18:21:46 +02:00
public function once(callable $callback, $msDelay);
2013-08-05 22:05:08 +02:00
/**
* Schedule a recurring callback to execute every $interval seconds until cancelled
*
2014-06-11 18:21:46 +02:00
* Time intervals are measured in milliseconds.
2013-11-27 18:05:08 +01:00
*
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param callable $callback Any valid PHP callable
2014-06-11 18:21:46 +02:00
* @param int $msDelay The delay in milliseconds before the callback will trigger (may be zero)
2013-08-05 22:05:08 +02:00
*/
2014-06-11 18:21:46 +02:00
public function repeat(callable $callback, $msDelay);
2013-11-27 17:56:29 +01:00
2013-08-05 22:05:08 +02:00
/**
* Schedule an event to trigger once at the specified time
2013-11-27 17:56:29 +01:00
*
2013-11-27 18:05:08 +01:00
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param callable $callback Any valid PHP callable
* @param string $timeString Any string that can be parsed by strtotime() and is in the future
*/
2014-02-23 22:26:28 +01:00
public function at(callable $callback, $timeString);
2013-08-05 22:05:08 +02:00
/**
* Watch a stream resource for IO readable data and trigger the callback when actionable
*
2013-11-27 18:05:08 +01:00
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param resource $stream A stream resource to watch for readable data
* @param callable $callback Any valid PHP callable
* @param bool $enableNow Should the watcher be enabled now or held for later use?
*/
2014-02-23 22:26:28 +01:00
public function onReadable($stream, callable $callback, $enableNow = TRUE);
2013-08-05 22:05:08 +02:00
/**
* Watch a stream resource to become writable and trigger the callback when actionable
*
2013-11-27 18:05:08 +01:00
* Though it can't be enforced at the interface level all timer/stream scheduling methods
* should return a unique integer identifying the relevant watcher.
*
2013-08-05 22:05:08 +02:00
* @param resource $stream A stream resource to watch for writability
* @param callable $callback Any valid PHP callable
* @param bool $enableNow Should the watcher be enabled now or held for later use?
*/
2014-02-23 22:26:28 +01:00
public function onWritable($stream, callable $callback, $enableNow = TRUE);
2013-08-05 22:05:08 +02:00
2014-06-11 18:21:46 +02:00
/**
* Similar to onReadable/onWritable but uses a flag bitmask for extended option assignment
*
* @param resource $stream A stream resource to watch for writability
* @param callable $callback Any valid PHP callable
* @param int $flags Option bitmask (Reactor::POLL_READ, Reactor::POLL_WRITE, etc)
*/
public function watchStream($stream, $flags, callable $callback);
2013-08-05 22:05:08 +02:00
/**
* Cancel an existing timer/stream watcher
*
* @param int $watcherId
*/
2014-02-23 22:26:28 +01:00
public function cancel($watcherId);
2013-08-05 22:05:08 +02:00
/**
* Temporarily disable (but don't cancel) an existing timer/stream watcher
*
* @param int $watcherId
*/
2014-02-23 22:26:28 +01:00
public function disable($watcherId);
2013-08-05 22:05:08 +02:00
/**
* Enable a disabled timer/stream watcher
*
* @param int $watcherId
*/
2014-02-23 22:26:28 +01:00
public function enable($watcherId);
2013-08-05 22:05:08 +02:00
}