Inheritance: implements React\Promise\PromiseInterface
Example #1
0
 /** @test */
 public function shouldReturnRejectedPromiseIfFactoryThrowsException()
 {
     $exception = new \Exception();
     $factory = $this->createCallableMock();
     $factory->expects($this->once())->method('__invoke')->will($this->throwException($exception));
     $onRejected = $this->createCallableMock();
     $onRejected->expects($this->once())->method('__invoke')->with($this->identicalTo($exception));
     $p = new LazyPromise($factory);
     $p->then($this->expectCallableNever(), $onRejected);
 }
Example #2
0
 /**
  * @param \Icicle\Awaitable\Awaitable $awaitable
  */
 public function __construct(Awaitable $awaitable)
 {
     parent::__construct(function () use($awaitable) {
         return new Promise(function ($resolve, $reject) use($awaitable) {
             $awaitable->done($resolve, $reject);
         }, function () use($awaitable) {
             $awaitable->cancel();
         });
     });
 }
Example #3
0
 /**
  * Bind the Teleport Endpoint listener to the specified MODX profile.
  *
  * @param string $profile A valid MODX profile describing the instance to
  * bind the Endpoint listener to.
  * @param array $options An array of options for the Endpoint.
  *
  * @throws \RuntimeException If an error occurs binding the listener to the
  * specified MODX instance.
  */
 public function run($profile, array $options = array())
 {
     try {
         $profile = self::loadProfile($profile);
         $modx = $this->getMODX($profile, array_merge(array('log_target' => 'ECHO'), $options));
         /** @var \modRegistry $registry */
         $registry = $modx->getService('registry', 'registry.modRegistry');
         $registerName = $this->getConfig()->get('endpoint.registerName', $options, 'endpoint');
         $registerClass = $this->getConfig()->get('endpoint.registerClass', $options, 'registry.modFileRegister');
         $registerOptions = $this->getConfig()->get('endpoint.registerOptions', $options, array('directory' => $registerName));
         $register = $registry->getRegister($registerName, $registerClass, $registerOptions);
         $register->connect();
     } catch (InvalidProfileException $e) {
         throw new \RuntimeException("Could not start Endpoint listener: {$e->getMessage()}", $e->getCode(), $e);
     } catch (\Exception $e) {
         throw new \RuntimeException("Could not start Endpoint listener: {$e->getMessage()}", $e->getCode(), $e);
     }
     $pollInterval = (double) $this->getConfig()->get('endpoint.pollInterval', $options, 1);
     /** @var \React\EventLoop\LibEventLoop $loop */
     $loop = \React\EventLoop\Factory::create();
     $self =& $this;
     /* poll MODX registry for Teleportation requests and act on them */
     $loop->addPeriodicTimer($pollInterval, function () use($self, $profile, $modx, $register) {
         $register->subscribe('/queue/');
         $msgs = $register->read(array('msg_limit' => 1, 'poll_limit' => 1, 'remove_read' => true, 'include_keys' => true));
         $register->unsubscribe('/queue/');
         if (!empty($msgs)) {
             $taskMsg = reset($msgs);
             $taskId = key($msgs);
             if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) {
                 echo "msg received: {$taskId} => " . print_r($taskMsg, true) . "\n";
             }
             $register->subscribe("/tasks/{$taskId}/");
             $task = Endpoint::createTaskFactory($self, $taskId, $taskMsg, $register);
             $promise = new LazyPromise($task);
             $promise->then(function ($value) use($self, $register, $taskId) {
                 if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) {
                     echo "{$taskId} resolved [value => " . print_r($value, true) . "]\n";
                 }
                 $register->send("/tasks/{$taskId}/", array(array('completed' => $value)));
             }, function ($reason) use($self, $register, $taskId) {
                 if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) {
                     echo "{$taskId} rejected [reason => " . print_r($reason, true) . "]\n";
                 }
                 $register->send("/tasks/{$taskId}/", array(array('failed' => $reason)));
             }, function ($update) use($self, $register, $taskId) {
                 if ($self->getConfig()->get('verbose') || $self->getConfig()->get('debug')) {
                     echo "{$taskId} progress [update => " . print_r($update, true) . "]\n";
                 }
                 $register->send("/tasks/{$taskId}/", array(array('progress' => $update)));
             });
             $register->unsubscribe("/tasks/{$taskId}/");
         }
     });
     $loop->run();
 }