[WAMP] Added autoDelete to Topics

This commit is contained in:
Chris Boden 2014-06-05 08:13:35 -04:00
parent 87de418446
commit a0d858a638
3 changed files with 25 additions and 9 deletions

View File

@ -6,6 +6,13 @@ use Ratchet\ConnectionInterface;
* A topic/channel containing connections that have subscribed to it * A topic/channel containing connections that have subscribed to it
*/ */
class Topic implements \IteratorAggregate, \Countable { class Topic implements \IteratorAggregate, \Countable {
/**
* If true the TopicManager will destroy this object if it's ever empty of connections
* @deprecated in v0.4
* @type bool
*/
public $autoDelete = false;
private $id; private $id;
private $subscribers; private $subscribers;

View File

@ -54,13 +54,12 @@ class TopicManager implements WsServerInterface, WampServerInterface {
public function onUnsubscribe(ConnectionInterface $conn, $topic) { public function onUnsubscribe(ConnectionInterface $conn, $topic) {
$topicObj = $this->getTopic($topic); $topicObj = $this->getTopic($topic);
if ($conn->WAMP->subscriptions->contains($topicObj)) { if (!$conn->WAMP->subscriptions->contains($topicObj)) {
$conn->WAMP->subscriptions->detach($topicObj);
} else {
return; return;
} }
$this->topicLookup[$topic]->remove($conn); $this->cleanTopic($topicObj, $conn);
$this->app->onUnsubscribe($conn, $topicObj); $this->app->onUnsubscribe($conn, $topicObj);
} }
@ -77,11 +76,8 @@ class TopicManager implements WsServerInterface, WampServerInterface {
public function onClose(ConnectionInterface $conn) { public function onClose(ConnectionInterface $conn) {
$this->app->onClose($conn); $this->app->onClose($conn);
foreach ($this->topicLookup as $topic => $storage) { foreach ($this->topicLookup as $topic) {
$storage->remove($conn); $this->cleanTopic($topic, $conn);
if (0 === $storage->count()) {
unset($this->topicLookup[$topic]);
}
} }
} }
@ -114,4 +110,16 @@ class TopicManager implements WsServerInterface, WampServerInterface {
return $this->topicLookup[$topic]; return $this->topicLookup[$topic];
} }
protected function cleanTopic(Topic $topic, ConnectionInterface $conn) {
if ($conn->WAMP->subscriptions->contains($topic)) {
$conn->WAMP->subscriptions->detach($topic);
}
$this->topicLookup[$topic->getId()]->remove($conn);
if ($topic->autoDelete && 0 === $topic->count()) {
unset($this->topicLookup[$topic->getId()]);
}
}
} }

View File

@ -170,6 +170,7 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase {
$attribute->setAccessible(true); $attribute->setAccessible(true);
$topic = $method->invokeArgs($this->mngr, array($name)); $topic = $method->invokeArgs($this->mngr, array($name));
$topic->autoDelete = true;
$this->assertCount(1, $attribute->getValue($this->mngr)); $this->assertCount(1, $attribute->getValue($this->mngr));