Skip to content
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

Feature/promisify-properties #150

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions lib/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Property<ValueType = AnyType> {
*
* @returns {*} The current value
*/
getValue(): ValueType {
getValue(): Promise<ValueType> {
return this.value.get();
}

Expand All @@ -127,9 +127,9 @@ class Property<ValueType = AnyType> {
*
* @param {*} value The value to set
*/
setValue(value: ValueType): void {
setValue(value: ValueType): Promise<void> {
this.validateValue(value);
this.value.set(value);
return this.value.set(value);
}

/**
Expand Down
26 changes: 14 additions & 12 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class ThingHandler extends BaseHandler {
data: Record<string, unknown>;
};
try {
message = JSON.parse(msg as string);
message = JSON.parse(msg as unknown as string);
} catch (e1) {
try {
ws.send(
Expand Down Expand Up @@ -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',
},
})
);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
23 changes: 14 additions & 9 deletions lib/thing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,27 +337,30 @@ class Thing {
*
* @returns {*} Current property value if found, else null
*/
getProperty(propertyName: string): unknown | null {
getProperty(propertyName: string): Promise<unknown | null> {
const prop = this.findProperty(propertyName);
if (prop) {
return prop.getValue();
}

return null;
return Promise.reject();
}

/**
* Get a mapping of all properties and their values.
*
* Returns an object of propertyName -> value.
*/
getProperties(): Record<string, unknown> {
getProperties(): Promise<Record<string, unknown>> {
const props: Record<string, unknown> = {};
const promises: Promise<void>[] = [];
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);
}

/**
Expand All @@ -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<void> {
const prop = this.findProperty(propertyName);
if (!prop) {
return;
return Promise.reject();
}

prop.setValue(value);
return prop.setValue(value);
}

/**
Expand Down Expand Up @@ -586,10 +589,11 @@ class Thing {
* @param {Object} property The property that changed
*/
propertyNotify(property: Property<AnyType>): void {
property.getValue().then((value) => {
const message = JSON.stringify({
messageType: 'propertyStatus',
data: {
[property.getName()]: property.getValue(),
[property.getName()]: value,
},
});

Expand All @@ -600,6 +604,7 @@ class Thing {
// do nothing
}
}
});
}

/**
Expand Down
30 changes: 20 additions & 10 deletions lib/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,48 @@ class Value<ValueType = AnyType> extends EventEmitter {

private valueForwarder: Value.Forwarder<ValueType> | null;

private valueRequestor: Value.Requestor<ValueType> | null;

/**
* Initialize the object.
*
* @param {*} initialValue The initial value
* @param {function?} valueForwarder The method that updates the actual value
* on the thing
*/
constructor(initialValue: ValueType, valueForwarder: Value.Forwarder<ValueType> | null = null) {
constructor(initialValue: ValueType,
valueForwarder: Value.Forwarder<ValueType> | null = null,
valueRequestor: Value.Requestor<ValueType> | null = null) {
super();
this.lastValue = initialValue;
this.valueForwarder = valueForwarder;
this.valueRequestor = valueRequestor;
}

/**
* Set a new value for this thing.
*
* @param {*} value Value to set
*/
set(value: ValueType): void {
if (this.valueForwarder) {
this.valueForwarder(value);
}

this.notifyOfExternalUpdate(value);
set(value: ValueType): Promise<void> {
return Promise.resolve(
this.valueForwarder ? this.valueForwarder(value) : undefined
).then(() => this.notifyOfExternalUpdate(value));
}

/**
* Return the last known value from the underlying thing.
*
* @returns the value.
*/
get(): ValueType {
return this.lastValue;
get(): Promise<ValueType> {
if (this.valueRequestor) {
return Promise.resolve(this.valueRequestor()).then((newValue) => {
this.notifyOfExternalUpdate(newValue);
return newValue;
});
}
return Promise.resolve(this.lastValue);
}

/**
Expand All @@ -69,7 +78,8 @@ class Value<ValueType = AnyType> extends EventEmitter {
}

declare namespace Value {
export type Forwarder<T> = (value: T) => void;
export type Forwarder<T> = (value: T) => void | Promise<void>;
export type Requestor<T> = () => T | Promise<T>;
}

export = Value;