1
0
mirror of https://github.com/danog/amp.git synced 2024-12-14 18:37:30 +01:00
amp/lib/functions.php

215 lines
7.2 KiB
PHP
Raw Normal View History

2014-08-01 21:37:02 +02:00
<?php
namespace Alert;
/**
* Schedule a callback for immediate invocation in the next event loop iteration
*
* Watchers registered using this function will be automatically garbage collected after execution.
*
* @param callable $func Any valid PHP callable
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
2014-08-01 21:37:02 +02:00
function immediately(callable $func) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->select()->immediately($func);
2014-08-01 21:37:02 +02:00
}
/**
* Schedule a callback to execute once
*
* Watchers registered using this function will be automatically garbage collected after execution.
*
* @param callable $func Any valid PHP callable
* @param int $msDelay The delay in milliseconds before the callback will trigger (may be zero)
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
2014-08-01 21:37:02 +02:00
function once(callable $func, $msDelay) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->once($func, $msDelay);
2014-08-01 21:37:02 +02:00
}
/**
* Schedule a recurring callback to execute every $interval seconds until cancelled
*
* IMPORTANT: Watchers registered using this function must be manually cleared using cancel() to
* free the associated memory. Failure to cancel repeating watchers (even if disable() is used)
* will lead to memory leaks.
*
* @param callable $func Any valid PHP callable
* @param int $msDelay The delay in milliseconds in-between callback invocations (may be zero)
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
2014-08-01 21:37:02 +02:00
function repeat(callable $func, $msDelay) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->repeat($func, $msDelay);
2014-08-01 21:37:02 +02:00
}
/**
* Schedule an event to trigger once at the specified time
*
* Watchers registered using this function will be automatically garbage collected after execution.
*
* @param callable $func Any valid PHP callable
* @param string $timeString Any string that can be parsed by strtotime() and is in the future
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
2014-08-01 21:37:02 +02:00
function at(callable $func, $timeString) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->at($func, $timeString);
2014-08-01 21:37:02 +02:00
}
/**
* Enable a disabled timer or stream IO watcher
*
* Calling enable() on an already-enabled watcher will have no effect.
*
* @param int $watcherId
* @return void
*/
2014-08-01 21:37:02 +02:00
function enable($watcherId) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->enable($watcherId);
2014-08-01 21:37:02 +02:00
}
/**
* Temporarily disable (but don't cancel) an existing timer/stream watcher
*
* Calling disable() on a nonexistent or previously-disabled watcher will have no effect.
*
* NOTE: Disabling a repeating or stream watcher is not sufficient to free associated resources.
* When the watcher is no longer needed applications must still use cancel() to clear related
* memory and avoid leaks.
*
* @param int $watcherId
* @return void
*/
2014-08-01 21:37:02 +02:00
function disable($watcherId) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->disable($watcherId);
2014-08-01 21:37:02 +02:00
}
/**
* Cancel an existing timer/stream watcher
*
* Calling cancel() on a non-existent watcher will have no effect.
*
* @param int $watcherId
* @return void
*/
2014-08-01 21:37:02 +02:00
function cancel($watcherId) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->cancel($watcherId);
2014-08-01 21:37:02 +02:00
}
/**
* Watch a stream IO resource for readable data and trigger the specified callback when actionable
*
* IMPORTANT: Watchers registered using this function must be manually cleared using cancel() to
* free the associated memory. Failure to cancel repeating watchers (even if disable() is used)
* will lead to memory leaks.
*
* @param resource $stream A stream resource to watch for readable data
* @param callable $func Any valid PHP callable
* @param bool $enableNow Should the watcher be enabled now or held for later use?
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
function onReadable($stream, callable $func, $enableNow = true) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->onReadable($stream, $func, $enableNow);
2014-08-01 21:37:02 +02:00
}
/**
* Watch a stream IO resource for writability and trigger the specified callback when actionable
*
* NOTE: Sockets are essentially "always writable" (as long as their write buffer is not full).
* Therefore, it's critical that applications disable or cancel write watchers as soon as all data
* is written or the watcher will trigger endlessly and hammer the CPU.
*
* IMPORTANT: Watchers registered using this function must be manually cleared using cancel() to
* free the associated memory. Failure to cancel repeating watchers (even if disable() is used)
* will lead to memory leaks.
*
* @param resource $stream A stream resource to watch for writable data
* @param callable $func Any valid PHP callable
* @param bool $enableNow Should the watcher be enabled now or held for later use?
* @return int Returns the unique watcher ID for disable/enable/cancel
*/
function onWritable($stream, callable $func, $enableNow = true) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->onWritable($stream, $func, $enableNow);
2014-08-01 21:37:02 +02:00
}
/**
* Similar to onReadable/onWritable but uses a flag bitmask for extended option assignment
*
* IMPORTANT: Watchers registered using this function must be manually cleared using cancel() to
* free the associated memory. Failure to cancel repeating watchers (even if disable() is used)
* will lead to memory leaks.
*
* @param resource $stream A stream resource to watch for IO capability
* @param int $flags Option bitmask (Reactor::POLL_READ, Reactor::POLL_WRITE, etc)
* @param callable $func Any valid PHP callable
*/
2014-08-01 21:37:02 +02:00
function watchStream($stream, $flags, callable $func) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
return $reactor->watchStream($stream, $flags, $func);
2014-08-01 21:37:02 +02:00
}
/**
* Execute a single event loop iteration
*
* @return void
*/
2014-08-01 21:37:02 +02:00
function tick() {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->tick();
2014-08-01 21:37:02 +02:00
}
/**
* Start the event reactor and assume program flow control
*
* @param callable $onStart Optional callback to invoke immediately upon reactor start
* @return void
*/
2014-08-01 21:37:02 +02:00
function run(callable $onStart = null) {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->run($onStart);
2014-08-01 21:37:02 +02:00
}
/**
* Stop the event reactor
*
* @return void
*/
2014-08-01 21:37:02 +02:00
function stop() {
static $reactor;
$reactor = $reactor ?: ReactorFactory::select();
$reactor->stop();
2014-08-01 21:37:02 +02:00
}
/**
* Get the global event reactor
*
* Note that the $factory callable is only invoked if no global reactor has yet been initialized.
*
* @param callable $factory Optional factory callable for initializing a reactor
* @return \Alert\Reactor
*/
function reactor(callable $factory = null) {
static $reactor;
return ($reactor = $reactor ?: ReactorFactory::select($factory));
2014-08-01 21:37:02 +02:00
}