Esempio n. 1
0
/**
 * Returns an observable that emits a value every $interval milliseconds after (up to $count times). The value emitted
 * is an integer of the number of times the observable emitted a value.
 *
 * @param int $interval Time interval between emitted values in milliseconds.
 * @param int $count Number of values to emit. PHP_INT_MAX by default.
 *
 * @return \Amp\Observable
 *
 * @throws \Error If the number of times to emit is not a positive value.
 */
function interval(int $interval, int $count = PHP_INT_MAX) : Observable
{
    if (0 >= $count) {
        throw new \Error("The number of times to emit must be a positive value");
    }
    $postponed = new Postponed();
    Loop::repeat($interval, function ($watcher) use(&$i, $postponed, $count) {
        $postponed->emit(++$i);
        if ($i === $count) {
            Loop::cancel($watcher);
            $postponed->resolve();
        }
    });
    return $postponed->observe();
}