Skip to content

Commit 1235043

Browse files
authored
Enhance Connection Handling and Refactor Utility Functions (#528)
* chore: add onConnect/onDisconnect hook inside baseCloudClient * chore: remove useless log * bugfix: fix boolean minor bug * Bump version
1 parent 23d26ea commit 1235043

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
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.13.0",
3+
"version": "0.14.0",
44
"license": "GPLv3",
55
"description": "JS module providing Arduino Create IoT Cloud Connection",
66
"main": "./lib/index.js",

src/builder/IArduinoIoTCloudFactory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ export type CloudFactoryOptions = (APIOptions | CredentialsOptions | BrowserOpti
88

99
export interface IArduinoIoTCloudFactory {
1010
connect(options: APIOptions & Partial<CloudOptions>): Promise<IMultiPropertiesCloudClient>;
11-
connect(options: CredentialsOptions & Partial<CloudOptions>): Promise<ISinglePropertyCloudClient>;
1211
connect(options: BrowserOptions & Partial<CloudOptions>): Promise<IMultiPropertiesCloudClient>;
12+
connect(options: CredentialsOptions & Partial<CloudOptions>): Promise<ISinglePropertyCloudClient>;
1313
}

src/client/BaseCloudClient.ts

+2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@ export class BaseCloudClient<T extends IConnection = IConnection> implements ICl
6363
if (err) throw new Error(`subscription failed: ${err.toString()}`);
6464

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

6869
const originalMethod = subject.unsubscribe;
6970
subject.unsubscribe = () => {
7071
subscription.unsubscribe();
7172
originalMethod();
73+
this.options.onDisconnect();
7274
};
7375

7476
return subject;

src/client/SinglePropertyCloudClient.ts

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export class SinglePropertyCloudClient extends BaseCloudClient implements ISingl
3131
return;
3232
}
3333

34-
console.log('found association to thing:', thingId);
3534
this.thingId = thingId;
3635
if (this.onThingResponse) this.onThingResponse(thingId);
3736
this.subscription = this.observe(`/a/t/${this.thingId}/e/i`).subscribe((v) => {

src/senML/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ export function format(value: CloudMessageValue, name: string, timestamp: number
102102
parsed.bn = `urn:uuid:${deviceId}`;
103103
}
104104

105-
if (Utils.isNumber(value)) parsed.v = value;
106-
if (Utils.isString(value)) parsed.vs = value;
107-
if (Utils.isBoolean(value)) parsed.vb = value;
105+
if (Utils.isNumber(value)) parsed.v = Number(value);
106+
if (Utils.isString(value)) parsed.vs = String(value);
107+
if (Utils.isBoolean(value)) parsed.vb = Boolean(value);
108108

109109
return parsed;
110110
}

src/utils/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,28 @@ export class ArduinoCloudError extends Error {
1111
}
1212
}
1313

14+
export function isNil(value: unknown): value is undefined | null {
15+
return value === undefined || value == null;
16+
}
17+
1418
export function isObject(value: CloudMessageValue): value is object {
15-
return value && typeof value === 'object';
19+
return !isNil(value) && typeof value === 'object';
1620
}
1721

1822
export function isNumber(value: CloudMessageValue): value is number {
19-
return value && typeof value === 'number';
23+
return !isNil(value) && typeof value === 'number';
2024
}
2125

2226
export function isString(value: CloudMessageValue): value is string {
23-
return value && typeof value === 'string';
27+
return !isNil(value) && typeof value === 'string';
2428
}
2529

2630
export function isBoolean(value: CloudMessageValue): value is boolean {
27-
return value && typeof value === 'boolean';
31+
return !isNil(value) && typeof value === 'boolean';
2832
}
2933

3034
export function isArray<T>(value: CloudMessageValue): value is T[] {
31-
return value && Array.isArray(value);
35+
return !isNil(value) && Array.isArray(value);
3236
}
3337

3438
export function isNotAnEmptyObject(value: any): boolean {

0 commit comments

Comments
 (0)