Skip to content

Commit 9070799

Browse files
authored
Merge pull request #396 from arduino/pirropirro/fix-properties-bug
Fix properties callback bug on refresh
2 parents c63c9d7 + bd1a6ff commit 9070799

File tree

3 files changed

+5029
-7
lines changed

3 files changed

+5029
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arduino-iot-js",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"license": "GPLv3",
55
"description": "JS module providing Arduino Create IoT Cloud Connection",
66
"main": "./lib/index.js",

src/client/CloudClient.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { IConnection, CloudMessage } from '../connection/IConnection';
88
import { ICloudClient, CloudOptions, OnMessageCallback, CloudMessageValue } from './ICloudClient';
99

1010
const NOOP = () => null;
11+
type PropertyCallbacks = { cb: OnMessageCallback<any>; name: string; thingId: string };
1112
export class CloudClient implements ICloudClient {
1213
private connection: IConnection;
1314
private subscriptions: { [key: string]: Subscription[] } = {};
1415
private callbacks: { [key: string]: OnMessageCallback<any>[] } = {};
16+
private propertiesCbs: { [key: string]: PropertyCallbacks[] } = {};
1517

1618
private options: CloudOptions = {
1719
ssl: false,
@@ -71,6 +73,7 @@ export class CloudClient implements ICloudClient {
7173
Object.values(this.subscriptions).forEach((subs, topic) => {
7274
subs.forEach((sub) => sub.unsubscribe());
7375
delete this.callbacks[topic];
76+
delete this.propertiesCbs[topic];
7477
delete this.subscriptions[topic];
7578
});
7679

@@ -96,9 +99,13 @@ export class CloudClient implements ICloudClient {
9699
this.subscriptions[topic].forEach((sub) => sub.unsubscribe());
97100
delete this.subscriptions[topic];
98101

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+
100105
delete this.callbacks[topic];
106+
delete this.propertiesCbs[topic];
101107
callbacks.forEach((cb) => this.subscribe(topic, cb));
108+
properties.forEach(({ thingId, name, cb }) => this.onPropertyValue(thingId, name, cb));
102109
});
103110

104111
const { onConnected = NOOP } = this.options;
@@ -166,10 +173,10 @@ export class CloudClient implements ICloudClient {
166173

167174
const topic = `/a/t/${thingId}/e/o`;
168175

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] || [];
171178

172-
this.callbacks[topic].push(cb);
179+
this.propertiesCbs[topic].push({ thingId, name, cb });
173180
this.subscriptions[topic].push(
174181
this.messagesFrom(topic)
175182
.pipe(filter((v) => v.propertyName === name))
@@ -180,10 +187,10 @@ export class CloudClient implements ICloudClient {
180187
private subscribe<T extends CloudMessageValue>(topic: string, cb: OnMessageCallback<T>): Promise<void> {
181188
return new Promise((resolve, reject) => {
182189
try {
183-
this.callbacks[topic] = this.callbacks[topic] = [];
190+
this.callbacks[topic] = this.callbacks[topic] || [];
184191
this.callbacks[topic].push(cb);
185192

186-
this.subscriptions[topic] = this.subscriptions[topic] = [];
193+
this.subscriptions[topic] = this.subscriptions[topic] || [];
187194
this.subscriptions[topic].push(this.messagesFrom(topic).subscribe((v) => cb(v.value as T)));
188195

189196
return resolve();

0 commit comments

Comments
 (0)