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
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.13.0",
"version": "0.14.0",
"license": "GPLv3",
"description": "JS module providing Arduino Create IoT Cloud Connection",
"main": "./lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/builder/IArduinoIoTCloudFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/client/SinglePropertyCloudClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

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

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

View workflow job for this annotation

GitHub Actions / test

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

constructor(connection: IConnection, options: CloudOptions, private deviceTopic: string) {
Expand All @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/senML/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 9 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading