Пример #1
0
 public function isEnabled(Context $context)
 {
     $all = all([$this->first->isEnabled($context), $this->second->isEnabled($context)]);
     return pipe($all, function ($res) {
         return (bool) ($res[0] ^ $res[1]);
     });
 }
Пример #2
0
 public function handleEvent(Event $event) : Promise
 {
     if (!isset($this->eventHandlers[$event->getTypeId()])) {
         return new Success();
     }
     return all(array_map(function (BuiltInEventHandler $handler) use($event) {
         return $handler->handleEvent($event);
     }, $this->eventHandlers[$event->getTypeId()]));
 }
Пример #3
0
 /**
  * @return Promise
  */
 public function close()
 {
     $promises = [];
     foreach ($this->promisors as $promisorGroup) {
         foreach ($promisorGroup as $promisor) {
             $promises[] = $promisor->promise();
         }
     }
     foreach ($this->patternPromisors as $promisorGroup) {
         foreach ($promisorGroup as $promisor) {
             $promises[] = $promisor->promise();
         }
     }
     /** @var Promise $promise */
     $promise = all($promises);
     $promise->when(function () {
         $this->connection->close();
     });
     $this->unsubscribe();
     $this->pUnsubscribe();
     return $promise;
 }
Пример #4
0
 /**
  * Shut down the mutex client.
  *
  * Be sure to release all locks you acquired before,
  * so other clients will be able to acquire them.
  *
  * @return Promise
  */
 public function shutdown() : Promise
 {
     cancel($this->watcher);
     $promises = [$this->std->close()];
     foreach ($this->busyConnections as $connection) {
         $promises[] = $connection->close();
     }
     foreach ($this->readyConnections as list($time, $connection)) {
         $promises[] = $connection->close();
     }
     return all($promises);
 }
Пример #5
0
 public function restoreRooms(array $permanentRoomIdentifiers) : Promise
 {
     return resolve(function () use($permanentRoomIdentifiers) {
         /** @var Identifier $identifier */
         $promises = [];
         foreach ($permanentRoomIdentifiers as $identifier) {
             $this->permanentRooms[$identifier->getIdentString()] = true;
             $promises[] = resolve($this->connectRoom($identifier));
         }
         $transientRoomIdentifiers = array_map(function ($ident) {
             return $this->identifierFactory->createFromIdentString($ident);
         }, (yield $this->storage->getAllRooms()));
         foreach ($transientRoomIdentifiers as $identifier) {
             $promises[] = resolve($this->restoreTransientRoom($identifier));
         }
         (yield all($promises));
     });
 }
Пример #6
0
 public function handleGlobalEvent(GlobalEvent $event) : Promise
 {
     return all($this->invokeHandlersForEvent($event));
 }
Пример #7
0
 /**
  * @param ChatRoom|ChatRoomIdentifier $room
  * @param int[] ...$ids
  * @return Promise
  */
 public function getMainSiteUsers($room, int ...$ids) : Promise
 {
     $identifier = $this->getIdentifierFromArg($room);
     $promises = [];
     foreach ($ids as $id) {
         $url = $this->urlResolver->getEndpointURL($room, ChatRoomEndpoint::MAINSITE_USER, $id);
         $promises[$id] = $this->httpClient->request($url);
     }
     return resolve(function () use($promises, $identifier) {
         /** @var HttpResponse[] $responses */
         $responses = (yield all($promises));
         $result = [];
         foreach ($responses as $id => $response) {
             $result[$id] = MainSiteUser::createFromDOMDocument(domdocument_load_html($response->getBody()));
         }
         return $result;
     });
 }
Пример #8
0
 private function getWebSocketUri(Identifier $identifier, string $fKey)
 {
     $authBody = (new FormBody())->addField("roomid", $identifier->getId())->addField("fkey", $fKey);
     $historyBody = (new FormBody())->addField('since', 0)->addField('mode', 'Messages')->addField("msgCount", 1)->addField("fkey", $fKey);
     $requests = ['auth' => (new HttpRequest())->setUri($this->urlResolver->getEndpointURL($identifier, Endpoint::CHATROOM_WEBSOCKET_AUTH))->setMethod("POST")->setBody($authBody), 'history' => (new HttpRequest())->setUri($this->urlResolver->getEndpointURL($identifier, Endpoint::CHATROOM_EVENT_HISTORY))->setMethod("POST")->setBody($historyBody)];
     /** @var HttpResponse[] $responses */
     $responses = (yield all($this->httpClient->requestMulti($requests)));
     $authInfo = json_try_decode($responses['auth']->getBody(), true);
     $historyInfo = json_try_decode($responses['history']->getBody(), true);
     if (!isset($authInfo['url'])) {
         throw new \RuntimeException("WebSocket auth did not return URL");
     }
     if (!isset($historyInfo['time'])) {
         throw new \RuntimeException("Could not get time for WebSocket URL");
     }
     return $authInfo['url'] . '?l=' . $historyInfo['time'];
 }
Пример #9
0
 /**
  * @return Promise
  */
 public function close()
 {
     /** @var Promise $promise */
     $promise = all(promises($this->promisors));
     $promise->when(function () {
         $this->connection->close();
     });
     return $promise;
 }
Пример #10
0
 public function onClose(int $clientId, int $code, string $reason)
 {
     $userId = $this->client2user[$clientId];
     $sessionId = $this->client2session[$clientId];
     $active = $this->clientStates[$clientId];
     unset($this->clientStates[$clientId], $this->client2user[$clientId], $this->client2session[$clientId], $this->session2clients[$sessionId][$clientId]);
     if (empty($this->session2clients[$sessionId])) {
         unset($this->sessions[$sessionId], $this->session2clients[$sessionId], $this->user2sessions[$userId][$sessionId]);
         if (empty($this->user2sessions[$userId])) {
             unset($this->user2sessions[$userId]);
         }
     }
     if ($userId) {
         $promises[] = $this->counter->update("ws:connected", $userId, -1);
         if ($active) {
             $promises[] = $this->counter->update("ws:active", $userId, -1);
         }
         (yield all($promises));
     }
 }