Skip to content

Fix properties callback bug on refresh #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "arduino-iot-js",
"version": "0.6.0",
"version": "0.7.0",
"license": "GPLv3",
"description": "JS module providing Arduino Create IoT Cloud Connection",
"main": "./lib/index.js",
Expand Down
19 changes: 13 additions & 6 deletions src/client/CloudClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { IConnection, CloudMessage } from '../connection/IConnection';
import { ICloudClient, CloudOptions, OnMessageCallback, CloudMessageValue } from './ICloudClient';

const NOOP = () => null;
type PropertyCallbacks = { cb: OnMessageCallback<any>; name: string; thingId: string };
export class CloudClient implements ICloudClient {
private connection: IConnection;
private subscriptions: { [key: string]: Subscription[] } = {};
private callbacks: { [key: string]: OnMessageCallback<any>[] } = {};
private propertiesCbs: { [key: string]: PropertyCallbacks[] } = {};

private options: CloudOptions = {
ssl: false,
Expand Down Expand Up @@ -71,6 +73,7 @@ export class CloudClient implements ICloudClient {
Object.values(this.subscriptions).forEach((subs, topic) => {
subs.forEach((sub) => sub.unsubscribe());
delete this.callbacks[topic];
delete this.propertiesCbs[topic];
delete this.subscriptions[topic];
});

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

const callbacks = [...this.callbacks[topic]];
const callbacks = this.callbacks[topic] ? [...this.callbacks[topic]] : [];
const properties = this.propertiesCbs[topic] ? [...this.propertiesCbs[topic]] : [];

delete this.callbacks[topic];
delete this.propertiesCbs[topic];
callbacks.forEach((cb) => this.subscribe(topic, cb));
properties.forEach(({ thingId, name, cb }) => this.onPropertyValue(thingId, name, cb));
});

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

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

this.callbacks[topic] = this.callbacks[topic] = [];
this.subscriptions[topic] = this.subscriptions[topic] = [];
this.propertiesCbs[topic] = this.propertiesCbs[topic] || [];
this.subscriptions[topic] = this.subscriptions[topic] || [];

this.callbacks[topic].push(cb);
this.propertiesCbs[topic].push({ thingId, name, cb });
this.subscriptions[topic].push(
this.messagesFrom(topic)
.pipe(filter((v) => v.propertyName === name))
Expand All @@ -180,10 +187,10 @@ export class CloudClient implements ICloudClient {
private subscribe<T extends CloudMessageValue>(topic: string, cb: OnMessageCallback<T>): Promise<void> {
return new Promise((resolve, reject) => {
try {
this.callbacks[topic] = this.callbacks[topic] = [];
this.callbacks[topic] = this.callbacks[topic] || [];
this.callbacks[topic].push(cb);

this.subscriptions[topic] = this.subscriptions[topic] = [];
this.subscriptions[topic] = this.subscriptions[topic] || [];
this.subscriptions[topic].push(this.messagesFrom(topic).subscribe((v) => cb(v.value as T)));

return resolve();
Expand Down
Loading