1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 05:11:42 +01:00

Add Pause; update for timer watcher changes in loop

This commit is contained in:
Aaron Piotrowski 2016-05-23 00:44:35 -05:00
parent 378990fe86
commit 28577cf6e6
2 changed files with 23 additions and 20 deletions

21
lib/Pause.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Amp\Awaitable;
use Interop\Async\Awaitable;
use Interop\Async\Loop;
class Pause implements Awaitable {
use Internal\Placeholder;
/**
* @param int $time Milliseconds before succeeding the awaitable.
* @param mixed $value Succeed the awaitable with this value.
*/
public function __construct($time, $value = null)
{
Loop::delay($time, function () use ($value) {
$this->resolve($value);
});
}
}

View File

@ -33,7 +33,6 @@ function coroutine(callable $worker) {
function rethrow(Awaitable $awaitable) {
$awaitable->when(function ($exception) {
if ($exception) {
/** @var \Throwable|\Exception $exception */
throw $exception;
}
});
@ -135,9 +134,9 @@ function capture(Awaitable $awaitable, callable $functor) {
function timeout(Awaitable $awaitable, $timeout) {
$deferred = new Deferred;
$watcher = Loop::delay(function () use ($deferred) {
$watcher = Loop::delay($timeout, function () use ($deferred) {
$deferred->fail(new Exception\TimeoutException);
}, $timeout);
});
$onResolved = function () use ($awaitable, $deferred, $watcher) {
Loop::cancel($watcher);
@ -149,23 +148,6 @@ function timeout(Awaitable $awaitable, $timeout) {
return $deferred->getAwaitable();
}
/**
* Returns an awaitable that succeeds after $time elapses.
*
* @param int $time
*
* @return \Interop\Async\Awaitable
*/
function pause($time) {
$deferred = new Deferred;
Loop::delay(function () use ($deferred) {
$deferred->resolve();
}, $time);
return $deferred->getAwaitable();
}
/**
* Returns a awaitable that calls $promisor only when the result of the awaitable is requested (e.g., then() or
* done() is called on the returned awaitable). $promisor can return a awaitable or any value. If $promisor throws