Exemple #1
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     $app = new Container();
     $app->instance('config', new Repository());
     (new EventServiceProvider($app))->register();
     (new AmiServiceProvider($app))->register();
     $this->loop = $app[LoopInterface::class];
     $this->loop->nextTick(function () {
         if (!$this->running) {
             $this->loop->stop();
         }
     });
     $this->stream = $app[Stream::class];
     $this->events = $app['events'];
     $this->app = $app;
 }
 public function testMapEndWithPendingItems()
 {
     $map = map(function ($n, callable $callback) {
         $this->eventLoop->nextTick(function () use($callback, $n) {
             $callback(null, 2 * $n);
         });
     }, ['concurrency' => 5]);
     $items = [];
     $ended = false;
     $map->on('data', function ($n) use(&$items) {
         $items[] = $n;
     });
     $map->on('end', function () use(&$ended) {
         $ended = true;
     });
     for ($i = 0; $i < 10; $i++) {
         $map->write($i);
     }
     $map->end();
     $this->assertFalse($ended);
     $this->assertEmpty($items);
     $this->eventLoop->tick();
     $this->assertTrue($ended);
     $this->assertSame(range(0, 18, 2), $items);
 }
Exemple #3
0
 /**
  * Start the event reactor and assume program flow control
  *
  * @param callable $onStart Optional callback to invoke immediately upon reactor start
  */
 public function run(callable $onStart = null)
 {
     if ($onStart) {
         $this->reactor->nextTick($onStart);
     }
     $this->reactor->run();
 }
 /**
  * Schedule a callback to be invoked on the next tick of the event loop.
  *
  * Callbacks are guaranteed to be executed in the order they are enqueued,
  * before any timer or stream events.
  *
  * @param callable $listener The callback to invoke.
  */
 public function nextTick(callable $listener)
 {
     $this->emit('nextTick', [$listener]);
     return $this->loop->nextTick(function (LoopInterface $loop) use($listener) {
         $this->emit('nextTickTick', [$listener]);
         $listener($this);
     });
 }
 public function testFutureTickEventGeneratedByNextTick()
 {
     $stream = $this->createStream();
     $this->loop->nextTick(function () {
         $this->loop->futureTick(function () {
             echo 'future-tick' . PHP_EOL;
         });
     });
     $this->expectOutputString('future-tick' . PHP_EOL);
     $this->loop->run();
 }
 /**
  * Continues the given flow.
  *
  * @param ReactFlow $flow
  * @param Packet    $packet
  */
 private function continueFlow(ReactFlow $flow, Packet $packet)
 {
     try {
         $response = $flow->next($packet);
     } catch (\Exception $e) {
         $this->emitError($e);
         return;
     }
     if ($response !== null) {
         if ($this->stream->getBuffer()->listening) {
             $this->sendingFlows[] = $flow;
         } else {
             $this->stream->write($response);
             $this->writtenFlow = $flow;
         }
     } elseif ($flow->isFinished()) {
         $this->loop->nextTick(function () use($flow) {
             $this->finishFlow($flow);
         });
     }
 }
Exemple #7
0
 /**
  * @return \React\Promise\Promise|PromiseInterface
  */
 public function computeId()
 {
     $deferred = new Deferred();
     $this->loop->nextTick($this->doCompute($deferred));
     return $deferred->promise();
 }