From 3564973cbf90729ae42f7af59b6fcff299eff064 Mon Sep 17 00:00:00 2001 From: mkr Date: Tue, 5 Oct 2021 10:32:39 +0200 Subject: [PATCH 1/6] make code compile with tsc 4.4.3 --- lib/server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/server.ts b/lib/server.ts index 7de2139..e41e070 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -225,7 +225,7 @@ class ThingHandler extends BaseHandler { data: Record; }; try { - message = JSON.parse(msg as string); + message = JSON.parse(msg as unknown as string); } catch (e1) { try { ws.send( @@ -274,7 +274,7 @@ class ThingHandler extends BaseHandler { messageType: 'error', data: { status: '400 Bad Request', - message: e.message, + message: e instanceof Error ? e.message : 'unknown reason', }, }) ); From 76598283fc1b4aad05b9e872640cb53f0b293df0 Mon Sep 17 00:00:00 2001 From: mkr Date: Tue, 5 Oct 2021 14:17:27 +0200 Subject: [PATCH 2/6] Promisified property handling. Add valueRequestor for the case when a value has to be requested asynchronously. --- lib/property.ts | 6 +++--- lib/server.ts | 22 ++++++++++++---------- lib/thing.ts | 19 +++++++++++-------- lib/value.ts | 28 ++++++++++++++++++---------- 4 files changed, 44 insertions(+), 31 deletions(-) diff --git a/lib/property.ts b/lib/property.ts index af448bf..89b75d0 100644 --- a/lib/property.ts +++ b/lib/property.ts @@ -118,7 +118,7 @@ class Property { * * @returns {*} The current value */ - getValue(): ValueType { + getValue(): Promise { return this.value.get(); } @@ -127,9 +127,9 @@ class Property { * * @param {*} value The value to set */ - setValue(value: ValueType): void { + setValue(value: ValueType): Promise { this.validateValue(value); - this.value.set(value); + return this.value.set(value); } /** diff --git a/lib/server.ts b/lib/server.ts index e41e070..2be6aae 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -378,7 +378,11 @@ class PropertyHandler extends BaseHandler { const propertyName = req.params.propertyName; if (thing.hasProperty(propertyName)) { - res.json({ [propertyName]: thing.getProperty(propertyName) }); + thing.getProperty(propertyName).then((value) => { + res.json({ [propertyName]: value }); + }).catch((e) => { + res.status(500).end(e.toString()); + }); } else { res.status(404).end(); } @@ -399,19 +403,17 @@ class PropertyHandler extends BaseHandler { const propertyName = req.params.propertyName; if (!req.body.hasOwnProperty(propertyName)) { - res.status(400).end(); + res.status(404).end(); return; } if (thing.hasProperty(propertyName)) { - try { - thing.setProperty(propertyName, req.body[propertyName]); - } catch (e) { - res.status(400).end(); - return; - } - - res.json({ [propertyName]: thing.getProperty(propertyName) }); + thing.setProperty(propertyName, req.body[propertyName]) + .then(() => thing.getProperty(propertyName)) + .then((value) => res.json({ [propertyName]: value })) + .catch((e) => { + res.status(500).end(e.toString()); + }); } else { res.status(404).end(); } diff --git a/lib/thing.ts b/lib/thing.ts index 34dad27..1829aec 100644 --- a/lib/thing.ts +++ b/lib/thing.ts @@ -337,13 +337,13 @@ class Thing { * * @returns {*} Current property value if found, else null */ - getProperty(propertyName: string): unknown | null { + getProperty(propertyName: string): Promise { const prop = this.findProperty(propertyName); if (prop) { return prop.getValue(); } - return null; + return Promise.reject(); } /** @@ -351,13 +351,16 @@ class Thing { * * Returns an object of propertyName -> value. */ - getProperties(): Record { + getProperties(): Promise> { const props: Record = {}; + const promises: Promise[] = []; for (const name in this.properties) { - props[name] = this.properties[name].getValue(); + promises.push(this.properties[name].getValue().then((value) => { + props[name] = value; + })); } - return props; + return Promise.all(promises).then(() => props); } /** @@ -377,13 +380,13 @@ class Thing { * @param {String} propertyName Name of the property to set * @param {*} value Value to set */ - setProperty(propertyName: string, value: AnyType): void { + setProperty(propertyName: string, value: AnyType): Promise { const prop = this.findProperty(propertyName); if (!prop) { - return; + return Promise.reject(); } - prop.setValue(value); + return prop.setValue(value); } /** diff --git a/lib/value.ts b/lib/value.ts index a0ba095..253fcd2 100644 --- a/lib/value.ts +++ b/lib/value.ts @@ -20,6 +20,8 @@ class Value extends EventEmitter { private valueForwarder: Value.Forwarder | null; + private valueRequestor: Value.Requestor | null; + /** * Initialize the object. * @@ -27,10 +29,11 @@ class Value extends EventEmitter { * @param {function?} valueForwarder The method that updates the actual value * on the thing */ - constructor(initialValue: ValueType, valueForwarder: Value.Forwarder | null = null) { + constructor(initialValue: ValueType, valueForwarder: Value.Forwarder | null = null, valueRequestor: Value.Requestor | null = null) { super(); this.lastValue = initialValue; this.valueForwarder = valueForwarder; + this.valueRequestor = valueRequestor; } /** @@ -38,12 +41,10 @@ class Value extends EventEmitter { * * @param {*} value Value to set */ - set(value: ValueType): void { - if (this.valueForwarder) { - this.valueForwarder(value); - } - - this.notifyOfExternalUpdate(value); + set(value: ValueType): Promise { + return Promise.resolve( + this.valueForwarder ? this.valueForwarder(value): undefined + ).then(() => this.notifyOfExternalUpdate(value)); } /** @@ -51,8 +52,14 @@ class Value extends EventEmitter { * * @returns the value. */ - get(): ValueType { - return this.lastValue; + get(): Promise { + if (this.valueRequestor) { + return Promise.resolve(this.valueRequestor()).then((newValue) => { + this.notifyOfExternalUpdate(newValue); + return newValue; + }); + } + return Promise.resolve(this.lastValue); } /** @@ -69,7 +76,8 @@ class Value extends EventEmitter { } declare namespace Value { - export type Forwarder = (value: T) => void; + export type Forwarder = (value: T) => void|Promise; + export type Requestor = () => T|Promise; } export = Value; From 658bd43fa6265ae6442a5ab4c16a628c3c6ed3cc Mon Sep 17 00:00:00 2001 From: mkr Date: Wed, 6 Oct 2021 08:15:23 +0200 Subject: [PATCH 3/6] Corrections for eslint --- lib/value.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/value.ts b/lib/value.ts index 253fcd2..519a3fb 100644 --- a/lib/value.ts +++ b/lib/value.ts @@ -29,7 +29,9 @@ class Value extends EventEmitter { * @param {function?} valueForwarder The method that updates the actual value * on the thing */ - constructor(initialValue: ValueType, valueForwarder: Value.Forwarder | null = null, valueRequestor: Value.Requestor | null = null) { + constructor(initialValue: ValueType, + valueForwarder: Value.Forwarder | null = null, + valueRequestor: Value.Requestor | null = null) { super(); this.lastValue = initialValue; this.valueForwarder = valueForwarder; @@ -43,7 +45,7 @@ class Value extends EventEmitter { */ set(value: ValueType): Promise { return Promise.resolve( - this.valueForwarder ? this.valueForwarder(value): undefined + this.valueForwarder ? this.valueForwarder(value) : undefined ).then(() => this.notifyOfExternalUpdate(value)); } @@ -76,8 +78,8 @@ class Value extends EventEmitter { } declare namespace Value { - export type Forwarder = (value: T) => void|Promise; - export type Requestor = () => T|Promise; + export type Forwarder = (value: T) => void | Promise; + export type Requestor = () => T | Promise; } export = Value; From 789bca77cebca58e5aab978e721ec7f16e38305c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 9 Oct 2021 20:54:01 +0200 Subject: [PATCH 4/6] bugfix: corrected propertyState to return the value instead of the promise --- lib/thing.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/thing.ts b/lib/thing.ts index 1829aec..814a1ed 100644 --- a/lib/thing.ts +++ b/lib/thing.ts @@ -589,10 +589,11 @@ class Thing { * @param {Object} property The property that changed */ propertyNotify(property: Property): void { + property.getValue().then((value) => { const message = JSON.stringify({ messageType: 'propertyStatus', data: { - [property.getName()]: property.getValue(), + [property.getName()]: value, }, }); @@ -603,6 +604,7 @@ class Thing { // do nothing } } + }); } /** From 58eb84f9b779365b18f06a0c87b7462166aca3be Mon Sep 17 00:00:00 2001 From: mkr Date: Wed, 13 Oct 2021 09:46:19 +0200 Subject: [PATCH 5/6] run prettier as requested --- lib/server.ts | 16 ++++++++++------ lib/thing.ts | 30 ++++++++++++++++-------------- lib/value.ts | 14 ++++++++------ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/server.ts b/lib/server.ts index 2be6aae..a492197 100644 --- a/lib/server.ts +++ b/lib/server.ts @@ -378,11 +378,14 @@ class PropertyHandler extends BaseHandler { const propertyName = req.params.propertyName; if (thing.hasProperty(propertyName)) { - thing.getProperty(propertyName).then((value) => { - res.json({ [propertyName]: value }); - }).catch((e) => { - res.status(500).end(e.toString()); - }); + thing + .getProperty(propertyName) + .then((value) => { + res.json({ [propertyName]: value }); + }) + .catch((e) => { + res.status(500).end(e.toString()); + }); } else { res.status(404).end(); } @@ -408,7 +411,8 @@ class PropertyHandler extends BaseHandler { } if (thing.hasProperty(propertyName)) { - thing.setProperty(propertyName, req.body[propertyName]) + thing + .setProperty(propertyName, req.body[propertyName]) .then(() => thing.getProperty(propertyName)) .then((value) => res.json({ [propertyName]: value })) .catch((e) => { diff --git a/lib/thing.ts b/lib/thing.ts index 814a1ed..831a5a1 100644 --- a/lib/thing.ts +++ b/lib/thing.ts @@ -355,9 +355,11 @@ class Thing { const props: Record = {}; const promises: Promise[] = []; for (const name in this.properties) { - promises.push(this.properties[name].getValue().then((value) => { - props[name] = value; - })); + promises.push( + this.properties[name].getValue().then((value) => { + props[name] = value; + }) + ); } return Promise.all(promises).then(() => props); @@ -590,20 +592,20 @@ class Thing { */ propertyNotify(property: Property): void { property.getValue().then((value) => { - const message = JSON.stringify({ - messageType: 'propertyStatus', - data: { + const message = JSON.stringify({ + messageType: 'propertyStatus', + data: { [property.getName()]: value, - }, - }); + }, + }); - for (const subscriber of this.subscribers) { - try { - subscriber.send(message); - } catch (e) { - // do nothing + for (const subscriber of this.subscribers) { + try { + subscriber.send(message); + } catch (e) { + // do nothing + } } - } }); } diff --git a/lib/value.ts b/lib/value.ts index 519a3fb..70c7b8b 100644 --- a/lib/value.ts +++ b/lib/value.ts @@ -29,9 +29,11 @@ class Value extends EventEmitter { * @param {function?} valueForwarder The method that updates the actual value * on the thing */ - constructor(initialValue: ValueType, - valueForwarder: Value.Forwarder | null = null, - valueRequestor: Value.Requestor | null = null) { + constructor( + initialValue: ValueType, + valueForwarder: Value.Forwarder | null = null, + valueRequestor: Value.Requestor | null = null + ) { super(); this.lastValue = initialValue; this.valueForwarder = valueForwarder; @@ -44,9 +46,9 @@ class Value extends EventEmitter { * @param {*} value Value to set */ set(value: ValueType): Promise { - return Promise.resolve( - this.valueForwarder ? this.valueForwarder(value) : undefined - ).then(() => this.notifyOfExternalUpdate(value)); + return Promise.resolve(this.valueForwarder ? this.valueForwarder(value) : undefined).then(() => + this.notifyOfExternalUpdate(value) + ); } /** From fa7c30e5e9692366aaca55a59732583a5833b788 Mon Sep 17 00:00:00 2001 From: mkr Date: Fri, 22 Oct 2021 12:46:25 +0200 Subject: [PATCH 6/6] run prettier again. Not sure why it did not prettify everything before --- src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.ts b/src/server.ts index a492197..d2ddbd4 100644 --- a/src/server.ts +++ b/src/server.ts @@ -225,7 +225,7 @@ class ThingHandler extends BaseHandler { data: Record; }; try { - message = JSON.parse(msg as unknown as string); + message = JSON.parse((msg as unknown) as string); } catch (e1) { try { ws.send(