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