Merges an observable sequence of observables into an observable sequence.
public static mergeAll ( rx\ObservableInterface $sources ) : Rx\Observable\AnonymousObservable | ||
$sources | rx\ObservableInterface | |
Résultat | Rx\Observable\AnonymousObservable |
/** * @test */ public function it_passes_on_completed_from_sources() { $ys = $this->createHotObservable(array(onCompleted(250))); $results = $this->scheduler->startWithCreate(function () use($ys) { return Observable::mergeAll($ys); }); $this->assertMessages(array(onCompleted(250)), $results->getMessages()); $this->assertSubscriptions(array(subscribe(200, 250)), $ys->getSubscriptions()); }
/** * Squirt the metrics over UDP * * @param array $data Incoming Data * @param float|1 $sampleRate the rate (0-1) for sampling. * @param array|string $tags Key Value array of Tag => Value, or single tag as string * * @return Observable **/ public function send($data, $sampleRate = 1, array $tags = null) { // sampling $sampledData = array(); if ($sampleRate < 1) { foreach ($data as $stat => $value) { if (mt_rand() / mt_getrandmax() <= $sampleRate) { $sampledData[] = [$stat, "{$value}|@{$sampleRate}"]; } } } else { foreach ($data as $stat => $value) { $sampledData[] = [$stat, $value]; } } if (empty($sampledData)) { return Observable::emptyObservable(); } $observableSequence = Observable::fromArray($sampledData)->map(function ($d) use($tags) { $stat = $d[0]; $value = $d[1]; if ($tags !== null && is_array($tags) && count($tags) > 0) { $value .= '|'; foreach ($tags as $tag_key => $tag_val) { if (is_array($tag_val)) { $flattenTagVal = array(); array_walk_recursive($array, function ($a) use(&$return) { $flattenTagVal[] = $a; }); $tag_val = implode("\n", $flattenTagVal); } $value .= '#' . $tag_key . ':' . $tag_val . ','; } $value = substr($value, 0, -1); } elseif (isset($tags) && !empty($tags)) { $value .= '|#' . $tags; } return $this->reportMetric("{$stat}:{$value}"); }); return Observable::mergeAll($observableSequence); }
<?php require_once __DIR__ . '/../bootstrap.php'; $loop = React\EventLoop\Factory::create(); $scheduler = new Rx\Scheduler\EventLoopScheduler($loop); $sources = Rx\Observable::range(0, 3)->map(function ($x) { return Rx\Observable::range($x, 3); }); $merged = \Rx\Observable::mergeAll($sources); $disposable = $merged->subscribe($stdoutObserver, $scheduler); $loop->run();
<?php use EventLoop\EventLoop; use Rx\Observable; use Rx\Scheduler\EventLoopScheduler; use Rxnet\Observer\StdOutObserver; require __DIR__ . "/../../vendor/autoload.php"; $loop = EventLoop::getLoop(); /* * If you have no statsd server, you can run the following command to listen for UDP traffic on port 8125 : * $> socat - udp4-listen:8125,reuseaddr,fork */ $statsd = new \Rxnet\Statsd\Statsd("localhost", 8125); $req1 = $statsd->gauge("database.connections", 42); $req2 = $statsd->increment("database.query.count"); $req3 = $statsd->histogram("database.query.time", 0.42); Observable::mergeAll(Observable::fromArray([$req1, $req2, $req3]))->subscribe(new StdOutObserver(), new EventLoopScheduler($loop)); $loop->run();