2015-03-18 14:04:18 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
2015-07-29 23:23:53 -04:00
|
|
|
/**
|
|
|
|
* Coroutines can yield Pause objects to suspend execution until the specified timeout elapses
|
|
|
|
*/
|
2015-05-19 14:10:53 -04:00
|
|
|
class Pause implements Promise {
|
|
|
|
use Placeholder;
|
|
|
|
|
2015-07-20 22:24:51 -04:00
|
|
|
/**
|
|
|
|
* @param int $timeout The timeout in milliseconds after which the promise will resolve
|
2015-07-29 01:15:43 -04:00
|
|
|
* @throws \DomainException On invalid timeout value
|
2015-07-20 22:24:51 -04:00
|
|
|
*/
|
2015-07-29 01:15:43 -04:00
|
|
|
public function __construct($timeout) {
|
2015-07-20 22:24:51 -04:00
|
|
|
$timeout = (int) $timeout;
|
|
|
|
if ($timeout < 1) {
|
|
|
|
throw new \DomainException(
|
|
|
|
"Pause timeout must be greater than or equal to 1 millisecond"
|
|
|
|
);
|
2015-03-18 14:04:18 -04:00
|
|
|
}
|
2015-07-29 01:15:43 -04:00
|
|
|
once(function () {
|
2015-07-20 22:24:51 -04:00
|
|
|
$this->resolve();
|
|
|
|
}, $timeout);
|
2015-03-18 14:04:18 -04:00
|
|
|
}
|
|
|
|
}
|