Skip to content

Enhance Connection Handling and Refactor Utility Functions #528

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 4 commits into from
Feb 27, 2025
Merged
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.13.0",
"version": "0.14.0",
"license": "GPLv3",
"description": "JS module providing Arduino Create IoT Cloud Connection",
"main": "./lib/index.js",
2 changes: 1 addition & 1 deletion src/builder/IArduinoIoTCloudFactory.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,6 @@ export type CloudFactoryOptions = (APIOptions | CredentialsOptions | BrowserOpti

export interface IArduinoIoTCloudFactory {
connect(options: APIOptions & Partial<CloudOptions>): Promise<IMultiPropertiesCloudClient>;
connect(options: CredentialsOptions & Partial<CloudOptions>): Promise<ISinglePropertyCloudClient>;
connect(options: BrowserOptions & Partial<CloudOptions>): Promise<IMultiPropertiesCloudClient>;
connect(options: CredentialsOptions & Partial<CloudOptions>): Promise<ISinglePropertyCloudClient>;
}
2 changes: 2 additions & 0 deletions src/client/BaseCloudClient.ts
Original file line number Diff line number Diff line change
@@ -63,12 +63,14 @@ export class BaseCloudClient<T extends IConnection = IConnection> implements ICl
if (err) throw new Error(`subscription failed: ${err.toString()}`);

subscription = this.connection.messages.pipe(filter((v) => v.topic === topic)).subscribe((v) => subject.next(v));
this.options.onConnected();
});

const originalMethod = subject.unsubscribe;
subject.unsubscribe = () => {
subscription.unsubscribe();
originalMethod();
this.options.onDisconnect();
};

return subject;
1 change: 0 additions & 1 deletion src/client/SinglePropertyCloudClient.ts
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@
export class SinglePropertyCloudClient extends BaseCloudClient implements ISinglePropertyCloudClient {
private thingId: string;
private subscription: Subscription;
private propertiesCbs: { [key: string]: OnMessageCallback<any>[] } = {};

Check warning on line 11 in src/client/SinglePropertyCloudClient.ts

GitHub Actions / test

Unexpected any. Specify a different type

private onThingRejection: (reason: any) => void;

Check warning on line 13 in src/client/SinglePropertyCloudClient.ts

GitHub Actions / test

Unexpected any. Specify a different type
private onThingResponse: (value: string | PromiseLike<string>) => void;

constructor(connection: IConnection, options: CloudOptions, private deviceTopic: string) {
@@ -31,7 +31,6 @@
return;
}

console.log('found association to thing:', thingId);
this.thingId = thingId;
if (this.onThingResponse) this.onThingResponse(thingId);
this.subscription = this.observe(`/a/t/${this.thingId}/e/i`).subscribe((v) => {
6 changes: 3 additions & 3 deletions src/senML/index.ts
Original file line number Diff line number Diff line change
@@ -102,9 +102,9 @@ export function format(value: CloudMessageValue, name: string, timestamp: number
parsed.bn = `urn:uuid:${deviceId}`;
}

if (Utils.isNumber(value)) parsed.v = value;
if (Utils.isString(value)) parsed.vs = value;
if (Utils.isBoolean(value)) parsed.vb = value;
if (Utils.isNumber(value)) parsed.v = Number(value);
if (Utils.isString(value)) parsed.vs = String(value);
if (Utils.isBoolean(value)) parsed.vb = Boolean(value);

return parsed;
}
14 changes: 9 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -11,24 +11,28 @@ export class ArduinoCloudError extends Error {
}
}

export function isNil(value: unknown): value is undefined | null {
return value === undefined || value == null;
}

export function isObject(value: CloudMessageValue): value is object {
return value && typeof value === 'object';
return !isNil(value) && typeof value === 'object';
}

export function isNumber(value: CloudMessageValue): value is number {
return value && typeof value === 'number';
return !isNil(value) && typeof value === 'number';
}

export function isString(value: CloudMessageValue): value is string {
return value && typeof value === 'string';
return !isNil(value) && typeof value === 'string';
}

export function isBoolean(value: CloudMessageValue): value is boolean {
return value && typeof value === 'boolean';
return !isNil(value) && typeof value === 'boolean';
}

export function isArray<T>(value: CloudMessageValue): value is T[] {
return value && Array.isArray(value);
return !isNil(value) && Array.isArray(value);
}

export function isNotAnEmptyObject(value: any): boolean {

Unchanged files with check annotations Beta

s?: number;
t?: number;
ut?: number;
[key: string]: any;

Check warning on line 46 in src/@arduino/cbor-js.d.ts

GitHub Actions / test

Unexpected any. Specify a different type
};
}
import { ITokenConnection } from '../connection/IConnection';
import { OnMessageCallback, CloudMessageValue, IMultiPropertiesCloudClient, ITokenCloudClient } from './ICloudClient';
export type PropertyCallbacks = { cb: OnMessageCallback<any>; name: string; thingId: string };

Check warning on line 8 in src/client/MultiPropertiesCloudClient.ts

GitHub Actions / test

Unexpected any. Specify a different type
// eslint-disable-next-line prettier/prettier
export class MultiPropertiesCloudClient
implements IMultiPropertiesCloudClient, ITokenCloudClient {
private subscriptions: { [key: string]: Subscription[] } = {};
private propertiesCbs: { [key: string]: PropertyCallbacks[] } = {};
private callbacks: { [key: string]: OnMessageCallback<any>[] } = {};

Check warning on line 16 in src/client/MultiPropertiesCloudClient.ts

GitHub Actions / test

Unexpected any. Specify a different type
public async disconnect(): Promise<void> {
await super.disconnect();
});
}
public end(force?: boolean, opts?: Record<string, any>, cb?: mqtt.CloseCallback): IConnection {

Check warning on line 57 in src/connection/Connection.ts

GitHub Actions / test

Unexpected any. Specify a different type
this.client.end(force, opts, cb);
return this;
}
return this;
}
public unsubscribe(topic: string | string[], opts?: any, callback?: any): IConnection {

Check warning on line 67 in src/connection/Connection.ts

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 67 in src/connection/Connection.ts

GitHub Actions / test

Unexpected any. Specify a different type
this.client.subscribe(topic, opts, callback);
return this;
}
public publish(topic: any, message: any, opts?: any, callback?: any): IConnection {

Check warning on line 72 in src/connection/Connection.ts

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 72 in src/connection/Connection.ts

GitHub Actions / test

Unexpected any. Specify a different type
this.client.publish(topic, message, opts, callback);
return this;
}