@@ -8,12 +8,12 @@ import { IConnection, CloudMessage } from '../connection/IConnection';
88import { ICloudClient , CloudOptions , OnMessageCallback , CloudMessageValue } from './ICloudClient' ;
99
1010const NOOP = ( ) => null ;
11- type PropertyCallbacks = { cb : OnMessageCallback < any > [ ] ; propertyName : string ; thingId } ;
11+ type PropertyCallbacks = { cb : OnMessageCallback < any > ; name : string ; thingId : string } ;
1212export class CloudClient implements ICloudClient {
1313 private connection : IConnection ;
1414 private subscriptions : { [ key : string ] : Subscription [ ] } = { } ;
1515 private callbacks : { [ key : string ] : OnMessageCallback < any > [ ] } = { } ;
16- private propertyCallbacks : { [ key : string ] : PropertyCallbacks } = { } ;
16+ private propertiesCbs : { [ key : string ] : PropertyCallbacks [ ] } = { } ;
1717
1818 private options : CloudOptions = {
1919 ssl : false ,
@@ -73,7 +73,7 @@ export class CloudClient implements ICloudClient {
7373 Object . values ( this . subscriptions ) . forEach ( ( subs , topic ) => {
7474 subs . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
7575 delete this . callbacks [ topic ] ;
76- delete this . propertyCallbacks [ topic ] ;
76+ delete this . propertiesCbs [ topic ] ;
7777 delete this . subscriptions [ topic ] ;
7878 } ) ;
7979
@@ -99,13 +99,13 @@ export class CloudClient implements ICloudClient {
9999 this . subscriptions [ topic ] . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
100100 delete this . subscriptions [ topic ] ;
101101
102- const callbacks = [ ...this . callbacks [ topic ] ] ;
103- const { thingId , propertyName , ...others } = this . propertyCallbacks [ topic ] || { cb : [ ] } ;
104- const propertiesCallbacks = [ ... others . cb ] ;
102+ const callbacks = this . callbacks [ topic ] ? [ ...this . callbacks [ topic ] ] : [ ] ;
103+ const properties = this . propertiesCbs [ topic ] ? [ ...this . propertiesCbs [ topic ] ] : [ ] ;
104+
105105 delete this . callbacks [ topic ] ;
106- delete this . propertyCallbacks [ topic ] ;
106+ delete this . propertiesCbs [ topic ] ;
107107 callbacks . forEach ( ( cb ) => this . subscribe ( topic , cb ) ) ;
108- propertiesCallbacks . forEach ( ( cb ) => this . onPropertyValue ( thingId , propertyName , cb ) ) ;
108+ properties . forEach ( ( { thingId , name , cb } ) => this . onPropertyValue ( thingId , name , cb ) ) ;
109109 } ) ;
110110
111111 const { onConnected = NOOP } = this . options ;
@@ -173,10 +173,10 @@ export class CloudClient implements ICloudClient {
173173
174174 const topic = `/a/t/${ thingId } /e/o` ;
175175
176- this . propertyCallbacks [ topic ] = this . propertyCallbacks [ topic ] = { thingId , propertyName : name , cb : [ ] } ;
177- this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
176+ this . propertiesCbs [ topic ] = this . propertiesCbs [ topic ] || [ ] ;
177+ this . subscriptions [ topic ] = this . subscriptions [ topic ] || [ ] ;
178178
179- this . propertyCallbacks [ topic ] . cb . push ( cb ) ;
179+ this . propertiesCbs [ topic ] . push ( { thingId , name , cb } ) ;
180180 this . subscriptions [ topic ] . push (
181181 this . messagesFrom ( topic )
182182 . pipe ( filter ( ( v ) => v . propertyName === name ) )
@@ -187,10 +187,10 @@ export class CloudClient implements ICloudClient {
187187 private subscribe < T extends CloudMessageValue > ( topic : string , cb : OnMessageCallback < T > ) : Promise < void > {
188188 return new Promise ( ( resolve , reject ) => {
189189 try {
190- this . callbacks [ topic ] = this . callbacks [ topic ] = [ ] ;
190+ this . callbacks [ topic ] = this . callbacks [ topic ] || [ ] ;
191191 this . callbacks [ topic ] . push ( cb ) ;
192192
193- this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
193+ this . subscriptions [ topic ] = this . subscriptions [ topic ] || [ ] ;
194194 this . subscriptions [ topic ] . push ( this . messagesFrom ( topic ) . subscribe ( ( v ) => cb ( v . value as T ) ) ) ;
195195
196196 return resolve ( ) ;
0 commit comments