then() публичный Метод

public then ( callable $onFulfilled = null, callable $onRejected = null )
$onFulfilled callable
$onRejected callable
Пример #1
0
 /**
  * Handle on session start
  * 
  * @param \Thruway\AbstractSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $this->thePromise->then(function () use($session) {
         $this->getCaller()->call($session, 'com.example.thefunction0', [])->then(function ($res) {
             var_dump($res);
         });
     });
 }
Пример #2
0
 /**
  * Handle on session start
  *
  * @param \Thruway\ClientSession $session
  * @param \Thruway\Transport\TransportInterface $transport
  */
 public function onSessionStart($session, $transport)
 {
     $this->thePromise->then(function () use($session) {
         $session->call('com.example.thefunction0', [])->then(function ($res) {
             echo "Done.\n";
             exit;
         });
     });
 }
Пример #3
0
 /** @test */
 public function shouldRejectIfResolverThrowsException()
 {
     $exception = new \Exception('foo');
     $promise = new Promise(function () use($exception) {
         throw $exception;
     });
     $mock = $this->createCallableMock();
     $mock->expects($this->once())->method('__invoke')->with($this->identicalTo($exception));
     $promise->then($this->expectCallableNever(), $mock);
 }
Пример #4
0
 /**
  *  Process a result as a promise
  *
  * @param \React\Promise\Promise $promise
  * @param \Thruway\Message\InvocationMessage $msg
  * @param \Thruway\ClientSession $session
  * @param array $registration
  */
 private function processResultAsPromise(Promise $promise, InvocationMessage $msg, ClientSession $session, $registration)
 {
     $promise->then(function ($promiseResults) use($msg, $session) {
         $options = new \stdClass();
         if ($promiseResults instanceof Result) {
             $yieldMsg = new YieldMessage($msg->getRequestId(), $options, $promiseResults->getArguments(), $promiseResults->getArgumentsKw());
         } else {
             $promiseResults = is_array($promiseResults) ? $promiseResults : [$promiseResults];
             $promiseResults = !$this::is_list($promiseResults) ? [$promiseResults] : $promiseResults;
             $yieldMsg = new YieldMessage($msg->getRequestId(), $options, $promiseResults);
         }
         $session->sendMessage($yieldMsg);
     }, function () use($msg, $session, $registration) {
         $errorMsg = ErrorMessage::createErrorMessageFromMessage($msg);
         $errorMsg->setErrorURI($registration['procedure_name'] . '.error');
         $session->sendMessage($errorMsg);
     }, function ($results) use($msg, $session, $registration) {
         $options = ["progress" => true];
         if ($results instanceof Result) {
             $yieldMsg = new YieldMessage($msg->getRequestId(), $options, $results->getArguments(), $results->getArgumentsKw());
         } else {
             $results = is_array($results) ? $results : [$results];
             $results = !$this::is_list($results) ? [$results] : $results;
             $yieldMsg = new YieldMessage($msg->getRequestId(), $options, $results);
         }
         $session->sendMessage($yieldMsg);
     });
 }
Пример #5
0
 /**
  * Add a tile to the base image.
  *
  * @param Promise $promise Promise from the get tile.
  * @param Point   $point   Point where to place the tile.
  *
  * @return void
  */
 protected function addTile(Promise $promise, Point $point)
 {
     $promise->then(function ($fileName) use($point) {
         return $this->loader->addImage($fileName);
     })->then(function ($image) use($point) {
         $this->drawImage($image, $point);
     });
 }
Пример #6
0
 private function assertRejected($expected, Promise $promise)
 {
     $rejected = null;
     $promise->then(null, function ($data) use(&$rejected) {
         $rejected = $data;
     });
     $this->assertEquals($expected, $rejected);
 }
Пример #7
0
/**
 * Creates a `Promise` which resolves with an array of all the event data
 *
 * @param ReadableStreamInterface|WritableStreamInterface $stream
 * @param string                                          $event
 * @return CancellablePromiseInterface Promise<string, Exception>
 */
function all(EventEmitterInterface $stream, $event = 'data')
{
    // stream already ended => resolve with empty buffer
    if ($stream instanceof ReadableStreamInterface) {
        // readable or duplex stream not readable => already closed
        // a half-open duplex stream is considered closed if its readable side is closed
        if (!$stream->isReadable()) {
            return Promise\resolve(array());
        }
    } elseif ($stream instanceof WritableStreamInterface) {
        // writable-only stream (not duplex) not writable => already closed
        if (!$stream->isWritable()) {
            return Promise\resolve(array());
        }
    }
    $buffer = array();
    $bufferer = function ($data) use(&$buffer) {
        $buffer[] = $data;
    };
    $stream->on($event, $bufferer);
    $promise = new Promise\Promise(function ($resolve, $reject) use($stream, &$buffer) {
        $stream->on('error', function ($error) use($reject) {
            $reject(new \RuntimeException('An error occured on the underlying stream while buffering', 0, $error));
        });
        $stream->on('close', function () use($resolve, &$buffer) {
            $resolve($buffer);
        });
    }, function ($_, $reject) {
        $reject(new \RuntimeException('Cancelled buffering'));
    });
    return $promise->then(null, function ($error) use(&$buffer, $bufferer, $stream, $event) {
        // promise rejected => clear buffer and buffering
        $buffer = array();
        $stream->removeListener($event, $bufferer);
        throw $error;
    });
}