@@ -142,13 +142,13 @@ const connect = options => new Promise((resolve, reject) => {
142
142
// connection loss
143
143
Object . getOwnPropertySymbols ( subscribedTopics ) . forEach ( ( connectionId ) => {
144
144
Object . values ( subscribedTopics [ connectionId ] ) . forEach ( ( subscribeParams ) => {
145
- subscribe ( connectionId , subscribeParams . topic , subscribeParams . cb )
145
+ subscribe ( connectionId , subscribeParams . topic , subscribeParams . cb ) ;
146
146
} ) ;
147
147
} ) ;
148
148
}
149
149
150
150
if ( typeof opts . onConnected === 'function' ) {
151
- opts . onConnected ( reconnect )
151
+ opts . onConnected ( reconnect ) ;
152
152
}
153
153
} ;
154
154
@@ -198,7 +198,14 @@ const disconnect = id => new Promise((resolve, reject) => {
198
198
return reject ( new Error ( 'disconnection failed: client not found' ) ) ;
199
199
}
200
200
201
- client . disconnect ( ) ;
201
+ try {
202
+ client . disconnect ( ) ;
203
+ } catch ( error ) {
204
+ return reject ( error ) ;
205
+ }
206
+
207
+ // Remove the connection
208
+ delete connections [ id ] ;
202
209
203
210
// Remove property callbacks to allow resubscribing in a later connect()
204
211
Object . keys ( propertyCallback ) . forEach ( ( topic ) => {
@@ -208,19 +215,14 @@ const disconnect = id => new Promise((resolve, reject) => {
208
215
} ) ;
209
216
210
217
// Clean up subscribed topics - a new connection might not need the same topics
211
- Object . keys ( subscribedTopics ) . forEach ( ( topic ) => {
212
- if ( subscribedTopics [ topic ] ) {
213
- delete subscribedTopics [ topic ] ;
214
- }
215
- } ) ;
216
-
218
+ delete subscribedTopics [ id ] ;
217
219
return resolve ( ) ;
218
220
} ) ;
219
221
220
222
const subscribe = ( id , topic , cb ) => new Promise ( ( resolve , reject ) => {
221
223
const client = connections [ id ] ;
222
224
if ( ! client ) {
223
- return reject ( new Error ( 'disconnection failed: client not found' ) ) ;
225
+ return reject ( new Error ( 'subscription failed: client not found' ) ) ;
224
226
}
225
227
226
228
return client . subscribe ( topic , {
@@ -385,7 +387,7 @@ const onPropertyValue = (connectionId, thingId, name, cb) => {
385
387
386
388
subscribedTopics [ connectionId ] [ thingId ] = {
387
389
topic : propOutputTopic ,
388
- cb : cb ,
390
+ cb,
389
391
} ;
390
392
391
393
if ( ! propertyCallback [ propOutputTopic ] ) {
0 commit comments