2016-05-24 18:47:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an observable that emits values emitted from any observable in the array of observables. Values in the
|
|
|
|
* array are passed through the from() function, so they may be observables, arrays of values to emit, awaitables,
|
|
|
|
* or any other value.
|
|
|
|
*
|
|
|
|
* @param \Amp\Observable[] $observables
|
|
|
|
*
|
|
|
|
* @return \Amp\Observable
|
|
|
|
*/
|
|
|
|
function merge(array $observables) {
|
|
|
|
foreach ($observables as $observable) {
|
|
|
|
if (!$observable instanceof Observable) {
|
|
|
|
throw new \InvalidArgumentException("Non-observable provided");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
$postponed = new Postponed;
|
2016-05-24 18:47:14 +02:00
|
|
|
|
2016-05-27 22:44:01 +02:00
|
|
|
$subscriptions = [];
|
2016-05-24 18:47:14 +02:00
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
foreach ($observables as $observable) {
|
2016-05-27 22:44:01 +02:00
|
|
|
$subscriptions[] = $observable->subscribe([$postponed, 'emit']);
|
2016-05-27 01:20:05 +02:00
|
|
|
}
|
|
|
|
|
2016-05-27 22:44:01 +02:00
|
|
|
all($subscriptions)->when(function ($exception, $value) use ($postponed) {
|
2016-05-27 01:20:05 +02:00
|
|
|
if ($exception) {
|
|
|
|
$postponed->fail($exception);
|
|
|
|
return;
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 18:35:09 +02:00
|
|
|
$postponed->resolve($value);
|
2016-05-24 18:47:14 +02:00
|
|
|
});
|
2016-05-27 01:20:05 +02:00
|
|
|
|
|
|
|
return $postponed->getObservable();
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an observable that emits a value every $interval milliseconds after the previous value has been consumed
|
|
|
|
* (up to $count times (or indefinitely if $count is 0). 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 Use 0 to emit values indefinitely.
|
|
|
|
*
|
|
|
|
* @return \Amp\Observable
|
|
|
|
*/
|
|
|
|
function interval($interval, $count = 0) {
|
|
|
|
$count = (int) $count;
|
|
|
|
if (0 > $count) {
|
|
|
|
throw new \InvalidArgumentException("The number of times to emit must be a non-negative value");
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
$postponed = new Postponed;
|
|
|
|
|
|
|
|
Loop::repeat($interval, function ($watcher) use (&$i, $postponed, $count) {
|
|
|
|
$postponed->emit(++$i);
|
|
|
|
|
|
|
|
if ($i === $count) {
|
2016-05-24 18:47:14 +02:00
|
|
|
Loop::cancel($watcher);
|
2016-05-29 18:35:09 +02:00
|
|
|
$postponed->resolve();
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
|
|
|
});
|
2016-05-27 01:20:05 +02:00
|
|
|
|
|
|
|
return $postponed->getObservable();
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $start
|
|
|
|
* @param int $end
|
|
|
|
* @param int $step
|
|
|
|
*
|
|
|
|
* @return \Amp\Observable
|
|
|
|
*/
|
|
|
|
function range($start, $end, $step = 1) {
|
|
|
|
$start = (int) $start;
|
|
|
|
$end = (int) $end;
|
|
|
|
$step = (int) $step;
|
|
|
|
|
|
|
|
if (0 === $step) {
|
|
|
|
throw new \InvalidArgumentException("Step must be a non-zero integer");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((($end - $start) ^ $step) < 0) {
|
|
|
|
throw new \InvalidArgumentException("Step is not of the correct sign");
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:20:05 +02:00
|
|
|
$postponed = new Postponed;
|
|
|
|
|
|
|
|
$generator = function (Postponed $postponed, $start, $end, $step) {
|
2016-05-24 18:47:14 +02:00
|
|
|
for ($i = $start; $i <= $end; $i += $step) {
|
2016-05-27 01:20:05 +02:00
|
|
|
yield $postponed->emit($i);
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|
2016-05-27 01:20:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
$coroutine = new Coroutine($generator($postponed, $start, $end, $step));
|
|
|
|
$coroutine->when(function ($exception) use ($postponed) {
|
|
|
|
if ($exception) {
|
|
|
|
$postponed->fail($exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-29 18:35:09 +02:00
|
|
|
$postponed->resolve();
|
2016-05-24 18:47:14 +02:00
|
|
|
});
|
2016-05-27 01:20:05 +02:00
|
|
|
|
|
|
|
return $postponed->getObservable();
|
2016-05-24 18:47:14 +02:00
|
|
|
}
|