protected function __construct(Messenger $messenger, LoopInterface $loop)
 {
     $messenger->registerRpc('return', function (Payload $payload) {
         return \React\Promise\resolve($payload->getPayload());
     });
     $this->ran = true;
 }
 /**
  * Create child process and set up RPC endpoints.
  *
  * @param \WyriHaximus\React\ChildProcess\Messenger\Messenger $messenger
  * @param \React\EventLoop\LoopInterface $loop
  */
 public function __construct(Messenger $messenger, LoopInterface $loop)
 {
     $this->recipient = $messenger;
     $this->loop = $loop;
     $this->recipient->registerRpc('config', [$this, 'rpcConfig']);
     $this->recipient->registerRpc('modifyKarma', [$this, 'rpcModifyKarma']);
     $this->recipient->registerRpc('fetchKarma', [$this, 'rpcFetchKarma']);
 }
 /**
  * @param LoopInterface $loop
  * @param array $options
  * @param null $termiteCallable
  * @return Messenger
  */
 public static function child(LoopInterface $loop, array $options = [], $termiteCallable = null)
 {
     $messenger = new Messenger(new Stream(STDIN, $loop), new Stream(STDOUT, $loop), new Stream(STDERR, $loop), ['read' => 'stdin', 'write_err' => 'stderr', 'write' => 'stdout'] + $options);
     if ($termiteCallable === null) {
         $termiteCallable = function () use($loop) {
             $loop->addTimer(self::TERMINATE_TIMEOUT, [$loop, 'stop']);
         };
     }
     $messenger->registerRpc(Messenger::TERMINATE_RPC, function (Payload $payload, Messenger $messenger) use($loop, $termiteCallable) {
         $messenger->emit('terminate', [$messenger]);
         $termiteCallable($payload, $messenger);
         return new FulfilledPromise();
     });
     return $messenger;
 }
 /**
  * Process constructor.
  * @param Messenger $messenger
  */
 protected function __construct(Messenger $messenger)
 {
     $messenger->registerRpc('setFlysystem', [$this, 'setFlysystem']);
     $messenger->registerRpc('unlink', [$this, 'unlink']);
     $messenger->registerRpc('stat', [$this, 'stat']);
     $messenger->registerRpc('readdir', [$this, 'readdir']);
     $messenger->registerRpc('read', [$this, 'read']);
     $messenger->registerRpc('write', [$this, 'write']);
     $messenger->registerRpc('rename', [$this, 'rename']);
 }
 /**
  * Modifies a term's karma.
  *
  * @param string $term   Term to modify
  * @param string $action Karma action (either ++ or --)
  * @param \Phergie\Irc\Event\UserEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  * @return bool|\React\Promise\Promise
  */
 protected function modifyKarma($term, $action, UserEvent $event, Queue $queue)
 {
     $nick = $event->getNick();
     $canonicalTerm = $this->getCanonicalTerm($term, $nick);
     if ($canonicalTerm == strtolower($nick)) {
         $message = 'You can\'t give yourself karma.';
         $queue->ircPrivmsg($event->getSource(), $message);
         return false;
     }
     $karma = 0;
     return $this->messenger->rpc(MessageFactory::rpc('fetchKarma', ['term' => $canonicalTerm]))->then(function ($payload) use($event, $queue, $action, $term, $canonicalTerm, &$karma) {
         $this->logDebug('payload: ' . var_export($payload, true));
         $karma = $payload['karma'] + ($action == '++' ? 1 : -1);
         return MessageFactory::rpc('modifyKarma', ['term' => $canonicalTerm, 'karma' => $karma]);
     })->then([$this->messenger, 'rpc'])->then(function ($payload) use($event, $queue, $action, $term) {
         $this->logDebug('payload: ' . var_export($payload, true));
         $queue->ircPrivmsg($event->getSource(), $this->getUserMessage('karma' . $action, $term));
         return $payload['karma'];
     });
 }
 protected function deregisterRpc()
 {
     $this->loop->futureTick(function () {
         $this->messenger->deregisterRpc(MessengerFactory::PROCESS_REGISTER);
     });
 }
 public function terminate()
 {
     $this->busy = true;
     $this->emit('terminating', [$this]);
     return $this->messenger->softTerminate();
 }