@@ -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 > [ ] ; propertyName : string ; thingId } ;
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 propertyCallbacks : { [ 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 . propertyCallbacks [ topic ] ;
74
77
delete this . subscriptions [ topic ] ;
75
78
} ) ;
76
79
@@ -97,8 +100,12 @@ export class CloudClient implements ICloudClient {
97
100
delete this . subscriptions [ topic ] ;
98
101
99
102
const callbacks = [ ...this . callbacks [ topic ] ] ;
103
+ const { thingId, propertyName, ...others } = this . propertyCallbacks [ topic ] || { cb : [ ] } ;
104
+ const propertiesCallbacks = [ ...others . cb ] ;
100
105
delete this . callbacks [ topic ] ;
106
+ delete this . propertyCallbacks [ topic ] ;
101
107
callbacks . forEach ( ( cb ) => this . subscribe ( topic , cb ) ) ;
108
+ propertiesCallbacks . forEach ( ( cb ) => this . onPropertyValue ( thingId , propertyName , 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 ] = [ ] ;
176
+ this . propertyCallbacks [ topic ] = this . propertyCallbacks [ topic ] = { thingId , propertyName : name , cb : [ ] } ;
170
177
this . subscriptions [ topic ] = this . subscriptions [ topic ] = [ ] ;
171
178
172
- this . callbacks [ topic ] . push ( cb ) ;
179
+ this . propertyCallbacks [ topic ] . cb . push ( cb ) ;
173
180
this . subscriptions [ topic ] . push (
174
181
this . messagesFrom ( topic )
175
182
. pipe ( filter ( ( v ) => v . propertyName === name ) )
0 commit comments