Skip to content

Commit d58a7a7

Browse files
committed
Make connect and disconnect flows silent
1 parent aeca353 commit d58a7a7

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

src/ReactFlow.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ class ReactFlow implements Flow
1717
private $deferred;
1818
/** @var Packet */
1919
private $packet;
20+
/** @var bool */
21+
private $isSilent;
2022

2123
/**
2224
* Constructs an instance of this class.
2325
*
2426
* @param Flow $decorated
2527
* @param Deferred $deferred
2628
* @param Packet $packet
29+
* @param bool $isSilent
2730
*/
28-
public function __construct(Flow $decorated, Deferred $deferred, Packet $packet = null)
31+
public function __construct(Flow $decorated, Deferred $deferred, Packet $packet = null, $isSilent = false)
2932
{
3033
$this->decorated = $decorated;
3134
$this->deferred = $deferred;
3235
$this->packet = $packet;
36+
$this->isSilent = $isSilent;
3337
}
3438

3539
public function getCode()
@@ -95,4 +99,14 @@ public function getPacket()
9599
{
96100
return $this->packet;
97101
}
102+
103+
/**
104+
* Indicates if the flow should emit events.
105+
*
106+
* @return bool
107+
*/
108+
public function isSilent()
109+
{
110+
return $this->isSilent;
111+
}
98112
}

src/ReactMqttClient.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
198198
$this->isConnected = true;
199199
$this->connection = $connection;
200200

201+
$this->emit('connect', [$connection, $this]);
201202
$deferred->resolve($this->connection);
202203
})
203204
->otherwise(function (\Exception $e) use ($deferred, $connection) {
@@ -235,14 +236,15 @@ public function disconnect()
235236

236237
$deferred = new Deferred();
237238

238-
$this->startFlow(new OutgoingDisconnectFlow($this->connection))
239+
$this->startFlow(new OutgoingDisconnectFlow($this->connection), true)
239240
->then(function (Connection $connection) use ($deferred) {
240241
$this->isDisconnecting = false;
241242
$this->isConnected = false;
242243

243-
$this->stream->close();
244-
244+
$this->emit('disconnect', [$connection, $this]);
245245
$deferred->resolve($connection);
246+
247+
$this->stream->close();
246248
})
247249
->otherwise(function () use ($deferred) {
248250
$this->isDisconnecting = false;
@@ -425,7 +427,7 @@ function () use ($deferred, $timeout) {
425427
}
426428
);
427429

428-
$this->startFlow(new OutgoingConnectFlow($connection, $this->identifierGenerator))
430+
$this->startFlow(new OutgoingConnectFlow($connection, $this->identifierGenerator), true)
429431
->always(function () use ($responseTimer) {
430432
$this->loop->cancelTimer($responseTimer);
431433
})->then(function (Connection $connection) use ($deferred) {
@@ -583,10 +585,11 @@ private function handleError(\Exception $e)
583585
* Starts the given flow.
584586
*
585587
* @param Flow $flow
588+
* @param bool $isSilent
586589
*
587590
* @return ExtendedPromiseInterface
588591
*/
589-
private function startFlow(Flow $flow)
592+
private function startFlow(Flow $flow, $isSilent = false)
590593
{
591594
try {
592595
$packet = $flow->start();
@@ -597,7 +600,7 @@ private function startFlow(Flow $flow)
597600
}
598601

599602
$deferred = new Deferred();
600-
$internalFlow = new ReactFlow($flow, $deferred, $packet);
603+
$internalFlow = new ReactFlow($flow, $deferred, $packet, $isSilent);
601604

602605
if ($packet !== null) {
603606
if ($this->stream->getBuffer()->listening) {
@@ -653,7 +656,9 @@ private function continueFlow(ReactFlow $flow, Packet $packet)
653656
private function finishFlow(ReactFlow $flow)
654657
{
655658
if ($flow->isSuccess()) {
656-
$this->emit($flow->getCode(), [$flow->getResult(), $this]);
659+
if (!$flow->isSilent()) {
660+
$this->emit($flow->getCode(), [$flow->getResult(), $this]);
661+
}
657662

658663
$flow->getDeferred()->resolve($flow->getResult());
659664
} else {

tests/Integration/ReactMqttClientTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public function setUp()
103103
$this->loop->stop();
104104
$this->fail('Test timeout');
105105
});
106+
107+
echo 'Test: '.str_replace(['test_', '_'], ['', ' '], $this->getName()).PHP_EOL.PHP_EOL;
106108
}
107109

108110
/**
@@ -112,7 +114,7 @@ public function tearDown()
112114
{
113115
$this->loop->stop();
114116

115-
$this->log(str_repeat('- - ', 25));
117+
echo str_repeat('- - ', 25).PHP_EOL;
116118
}
117119

118120
/*******************************************************
@@ -335,25 +337,51 @@ public function test_connect_failure()
335337
}
336338

337339
/**
338-
* Test that client's is-connected state is updated correctly
340+
* Test that client's connection state is updated correctly when connected.
339341
*
340342
* @depends test_connect_success
341343
*/
342344
public function test_is_connected_when_connect_event_emitted()
343345
{
344346
$client = $this->buildClient();
345347

346-
$client->on('connect', function(Connection $connection) use($client){
348+
$client->on('connect', function () use ($client) {
347349
$this->assertTrue($client->isConnected(), 'Client is should be connected');
348350
$this->stopLoop();
349351
});
350352

351-
$client->connect(self::HOSTNAME, self::PORT, null, 1)
352-
->then(function () use ($client) {
353-
$this->assertTrue($client->isConnected());
353+
$client->connect(self::HOSTNAME, self::PORT)
354+
->then(function () use ($client) {
355+
$this->assertTrue($client->isConnected());
356+
$this->stopLoop();
357+
});
358+
359+
$this->startLoop();
360+
}
361+
362+
/**
363+
* Test that client's connection state is updated correctly when disconnected.
364+
*
365+
* @depends test_connect_success
366+
*/
367+
public function test_is_disconnected_when_disconnect_event_emitted()
368+
{
369+
$client = $this->buildClient();
370+
371+
$client->on('disconnect', function () use ($client) {
372+
$this->assertFalse($client->isConnected(), 'Client is should be disconnected');
354373
$this->stopLoop();
355374
});
356375

376+
$client->connect(self::HOSTNAME, self::PORT)
377+
->then(function () use ($client) {
378+
$client->disconnect()
379+
->then(function () use ($client) {
380+
$this->assertFalse($client->isConnected());
381+
$this->stopLoop();
382+
});
383+
});
384+
357385
$this->startLoop();
358386
}
359387

0 commit comments

Comments
 (0)