Skip to content

Commit e4ff7ef

Browse files
committed
Resolve connect and disconnect with the result of the flow
1 parent 659e3e5 commit e4ff7ef

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/ReactMqttClient.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ public function connect($host, $port = 1883, Connection $connection = null, $tim
202202
$this->emit('open', [$connection, $this]);
203203

204204
$this->registerClient($connection, $timeout)
205-
->then(function (Connection $connection) use ($deferred) {
205+
->then(function ($result) use ($connection, $deferred) {
206206
$this->isConnecting = false;
207207
$this->isConnected = true;
208208
$this->connection = $connection;
209209

210210
$this->emit('connect', [$connection, $this]);
211-
$deferred->resolve($this->connection);
211+
$deferred->resolve($result ?: $connection);
212212
})
213-
->otherwise(function (\Exception $e) use ($deferred, $connection) {
213+
->otherwise(function (\Exception $e) use ($connection, $deferred) {
214214
$this->isConnecting = false;
215215

216216
$this->emitError($e);
@@ -251,20 +251,24 @@ public function disconnect($timeout = 5)
251251
$deferred = new Deferred();
252252

253253
$isResolved = false;
254+
$flowResult = null;
254255

255-
$this->onCloseCallback = function ($connection) use ($deferred, &$isResolved) {
256+
$this->onCloseCallback = function ($connection) use ($deferred, &$isResolved, &$flowResult) {
256257
if (!$isResolved) {
257258
$isResolved = true;
258259

259260
if ($connection) {
260261
$this->emit('disconnect', [$connection, $this]);
261-
$deferred->resolve($connection);
262262
}
263+
264+
$deferred->resolve($flowResult ?: $connection);
263265
}
264266
};
265267

266268
$this->startFlow($this->flowFactory->buildOutgoingDisconnectFlow($this->connection), true)
267-
->then(function () use ($timeout) {
269+
->then(function ($result) use ($timeout, &$flowResult) {
270+
$flowResult = $result;
271+
268272
$this->timer[] = $this->loop->addTimer(
269273
$timeout,
270274
function () {
@@ -274,11 +278,11 @@ function () {
274278
}
275279
);
276280
})
277-
->otherwise(function () use ($deferred, &$isResolved) {
281+
->otherwise(function ($exception) use ($deferred, &$isResolved) {
278282
if (!$isResolved) {
279283
$isResolved = true;
280284
$this->isDisconnecting = false;
281-
$deferred->reject($this->connection);
285+
$deferred->reject($exception);
282286
}
283287
});
284288

@@ -461,15 +465,15 @@ function () use ($deferred, $timeout) {
461465
$this->startFlow($this->flowFactory->buildOutgoingConnectFlow($connection), true)
462466
->always(function () use ($responseTimer) {
463467
$this->loop->cancelTimer($responseTimer);
464-
})->then(function (Connection $connection) use ($deferred) {
468+
})->then(function ($result) use ($connection, $deferred) {
465469
$this->timer[] = $this->loop->addPeriodicTimer(
466470
floor($connection->getKeepAlive() * 0.75),
467471
function () {
468472
$this->startFlow($this->flowFactory->buildOutgoingPingFlow());
469473
}
470474
);
471475

472-
$deferred->resolve($connection);
476+
$deferred->resolve($result ?: $connection);
473477
})->otherwise(function (\Exception $e) use ($deferred) {
474478
$deferred->reject($e);
475479
});

0 commit comments

Comments
 (0)