@@ -8,12 +8,12 @@ import { IConnection, CloudMessage } from '../connection/IConnection';
8
8
import { ICloudClient , CloudOptions , OnMessageCallback , CloudMessageValue } from './ICloudClient' ;
9
9
10
10
const NOOP = ( ) => null ;
11
- type PropertyCallbacks = { cb : OnMessageCallback < any > [ ] ; propertyName : string ; thingId } ;
11
+ type PropertyCallbacks = { cb : OnMessageCallback < any > ; name : string ; thingId : string } ;
12
12
export class CloudClient implements ICloudClient {
13
13
private connection : IConnection ;
14
14
private subscriptions : { [ key : string ] : Subscription [ ] } = { } ;
15
15
private callbacks : { [ key : string ] : OnMessageCallback < any > [ ] } = { } ;
16
- private propertyCallbacks : { [ key : string ] : PropertyCallbacks } = { } ;
16
+ private propertiesCbs : { [ key : string ] : PropertyCallbacks [ ] } = { } ;
17
17
18
18
private options : CloudOptions = {
19
19
ssl : false ,
@@ -73,7 +73,7 @@ export class CloudClient implements ICloudClient {
73
73
Object . values ( this . subscriptions ) . forEach ( ( subs , topic ) => {
74
74
subs . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
75
75
delete this . callbacks [ topic ] ;
76
- delete this . propertyCallbacks [ topic ] ;
76
+ delete this . propertiesCbs [ topic ] ;
77
77
delete this . subscriptions [ topic ] ;
78
78
} ) ;
79
79
@@ -99,13 +99,13 @@ export class CloudClient implements ICloudClient {
99
99
this . subscriptions [ topic ] . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
100
100
delete this . subscriptions [ topic ] ;
101
101
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
+
105
105
delete this . callbacks [ topic ] ;
106
- delete this . propertyCallbacks [ topic ] ;
106
+ delete this . propertiesCbs [ topic ] ;
107
107
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 ) ) ;
109
109
} ) ;
110
110
111
111
const { onConnected = NOOP } = this . options ;
@@ -173,10 +173,10 @@ export class CloudClient implements ICloudClient {
173
173
174
174
const topic = `/a/t/${ thingId } /e/o` ;
175
175
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 ] || [ ] ;
178
178
179
- this . propertyCallbacks [ topic ] . cb . push ( cb ) ;
179
+ this . propertiesCbs [ topic ] . push ( { thingId , name , cb } ) ;
180
180
this . subscriptions [ topic ] . push (
181
181
this . messagesFrom ( topic )
182
182
. pipe ( filter ( ( v ) => v . propertyName === name ) )
@@ -187,10 +187,10 @@ export class CloudClient implements ICloudClient {
187
187
private subscribe < T extends CloudMessageValue > ( topic : string , cb : OnMessageCallback < T > ) : Promise < void > {
188
188
return new Promise ( ( resolve , reject ) => {
189
189
try {
190
- this . callbacks [ topic ] = this . callbacks [ topic ] = [ ] ;
190
+ this . callbacks [ topic ] = this . callbacks [ topic ] || [ ] ;
191
191
this . callbacks [ topic ] . push ( cb ) ;
192
192
193
- this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
193
+ this . subscriptions [ topic ] = this . subscriptions [ topic ] || [ ] ;
194
194
this . subscriptions [ topic ] . push ( this . messagesFrom ( topic ) . subscribe ( ( v ) => cb ( v . value as T ) ) ) ;
195
195
196
196
return resolve ( ) ;
0 commit comments