Skip to content

Commit

Permalink
Refactoring to fully promis-based API
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jan 23, 2018
1 parent 97843e2 commit 5121e61
Show file tree
Hide file tree
Showing 69 changed files with 506 additions and 305 deletions.
8 changes: 6 additions & 2 deletions climate/adjustable-fan-speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(FanSpeed) {
setFanSpeed(speed) {
speed = percentage(speed, true);

return this.changeFanSpeed(speed)
.then(() => super.fanSpeed());
try {
return Promise.resolve(this.changeFanSpeed(speed))
.then(() => super.fanSpeed());
} catch(ex) {
return Promise.reject(ex);
}
}

changeFanSpeed(speed) {
Expand Down
8 changes: 6 additions & 2 deletions climate/adjustable-target-humidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(TargetHumidity)
setTargetHumidity(humidity) {
humidity = percentage(humidity, true);

return this.changeTargetHumidity(humidity)
.then(() => super.targetHumidity());
try {
return Promise.resolve(this.changeTargetHumidity(humidity))
.then(() => super.targetHumidity());
} catch(ex) {
return Promise.reject(ex);
}
}

changeTargetHumidity(humidity) {
Expand Down
16 changes: 12 additions & 4 deletions climate/autonomous-cleaning.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Cleaning
}

clean() {
return Promise.resolve(this.activateCleaning())
.then(() => null);
try {
return Promise.resolve(this.activateCleaning())
.then(() => null);
} catch(ex) {
return Promise.reject(ex);
}
}

stop() {
return Promise.resolve(this.deactivateCleaning())
.then(() => null);
try {
return Promise.resolve(this.deactivateCleaning())
.then(() => null);
} catch(ex) {
return Promise.reject(ex);
}
}

activateCleaning() {
Expand Down
4 changes: 2 additions & 2 deletions climate/cleaning-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(ErrorState, Sta
/**
* Get if thing is cleaning.
*/
get cleaning() {
return this.getState('cleaning');
cleaning() {
return Promise.resolve(this.getState('cleaning'));
}

updateCleaning(cleaning) {
Expand Down
2 changes: 1 addition & 1 deletion climate/fan-speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
}

fanSpeed() {
return this.getState('fanSpeed');
return Promise.resolve(this.getState('fanSpeed'));
}

updateFanSpeed(fanSpeed) {
Expand Down
8 changes: 6 additions & 2 deletions climate/spot-cleaning.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Cleaning
}

spotClean() {
return Promise.resolve(this.activateSpotClean())
.then(() => null);
try {
return Promise.resolve(this.activateSpotClean())
.then(() => null);
} catch(ex) {
return Promise.reject(ex);
}
}

activateSpotClean() {
Expand Down
2 changes: 1 addition & 1 deletion climate/target-humidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
}

targetHumidity() {
return this.getState('targetHumidity');
return Promise.resolve(this.getState('targetHumidity'));
}

updateTargetHumidity(target) {
Expand Down
8 changes: 6 additions & 2 deletions common/autonomous-charging.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State, Charging
}

charge() {
return Promise.resolve(this.activateCharging())
.then(() => null);
try {
return Promise.resolve(this.activateCharging())
.then(() => null);
} catch(ex) {
return Promise.reject(ex);
}
}

activateCharging() {
Expand Down
4 changes: 2 additions & 2 deletions common/battery-level.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
this.updateState('batteryLevel', -1);
}

get batteryLevel() {
return this.getState('batteryLevel');
batteryLevel() {
return Promise.resolve(this.getState('batteryLevel'));
}

updateBatteryLevel(level) {
Expand Down
2 changes: 1 addition & 1 deletion common/charging-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
* Get if thing is charging.
*/
get charging() {
return this.getState('charging');
return Promise.resolve(this.getState('charging'));
}

updateCharging(charging) {
Expand Down
13 changes: 1 addition & 12 deletions common/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,8 @@ module.exports = Thing.mixin(Parent => class extends Parent {
/**
* Get all of the children that are registered.
*/
get children() {
children() {
return this[childrenSymbol].values();
}

/**
* Synchronize the children based on the given definitions. This will remove
* any child that is not in the list of definitions and create new ones
* for new definitions.
*
* @param {Iterable} defs
* @param {Function} func
*/
syncChildren(defs, func) {

}
});
7 changes: 1 addition & 6 deletions common/error-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
.description('Get the current error state')
.returns('code', 'The current error state or null if no error')
.done();

builder.action('hasError')
.description('Get if the thing is in an error state')
.returns('boolean')
.done();
}

static get capability() {
Expand All @@ -51,7 +46,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
* Get if thing has an error.
*/
get error() {
return this.getState('error');
return Promise.resolve(this.getState('error'));
}

updateError(error) {
Expand Down
10 changes: 4 additions & 6 deletions common/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,11 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
/**
* Get or switch the mode of the appliance.
*
* @param {string} mode
* optional mode to switch to
* @returns
* string indicating the mode
*/
mode(mode=undefined) {
return this.getState('mode');
mode() {
return Promise.resolve(this.getState('mode'));
}

/**
Expand All @@ -84,8 +82,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
/**
* Get the available modes of the device.
*/
get modes() {
return this.getState('modes');
modes() {
return Promise.resolve(this.getState('modes'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion common/power.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
* boolean indicating the power level
*/
power() {
return this.getState('power');
return Promise.resolve(this.getState('power'));
}

/**
Expand Down
10 changes: 7 additions & 3 deletions common/restorable-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
for(const property of this.restorableState) {
result[property] = this.state[property];
}
return result;
return Promise.resolve(result);
}

/**
Expand All @@ -60,8 +60,12 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
* @param {object} state
*/
setState(state) {
return Promise.resolve(this.changeState(state))
.then(() => this.state);
try {
return Promise.resolve(this.changeState(state))
.then(() => this.state);
} catch(ex) {
return Promise.reject(ex);
}
}

/**
Expand Down
18 changes: 12 additions & 6 deletions common/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const Thing = require('../thing');
const deepEqual = require('deep-equal');

const state = Symbol('state');

/**
* State capability for things. Exposes a property named `state` to clients.
*
Expand Down Expand Up @@ -35,7 +37,11 @@ module.exports = Thing.mixin(Parent => class extends Parent {
constructor(...args) {
super(...args);

this.state = {};
this[state] = {};
}

state() {
return Promise.resolve(this[state]);
}

/**
Expand All @@ -45,12 +51,12 @@ module.exports = Thing.mixin(Parent => class extends Parent {
* @param {*} value
*/
updateState(key, value) {
if(deepEqual(this.state[key], value)) {
if(deepEqual(this[state][key], value)) {
// If the value has not changed, skip updating and emitting event
return false;
} else {
// Value has changed, update and queue event emittal
this.state[key] = value;
this[state][key] = value;
const event = {
key: key,
value: value
Expand All @@ -69,8 +75,8 @@ module.exports = Thing.mixin(Parent => class extends Parent {
* @param {string} key
*/
removeState(key) {
const emitEvent = typeof this.state[key] !== 'undefined';
delete this.state[key];
const emitEvent = typeof this[state][key] !== 'undefined';
delete this[state][key];

if(emitEvent) {
const event = {
Expand All @@ -90,7 +96,7 @@ module.exports = Thing.mixin(Parent => class extends Parent {
* @param {*} defaultValue
*/
getState(key, defaultValue=null) {
const value = this.state[key];
const value = this[state][key];
return typeof value === 'undefined' ? defaultValue : value;
}
});
12 changes: 8 additions & 4 deletions common/switchable-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Mode) {
* @param {string} mode
*/
setMode(mode) {
if(typeof mode === 'undefined') throw new Error('Mode must be specified');
mode = String(mode);
try {
if(typeof mode === 'undefined') throw new Error('Mode must be specified');
mode = String(mode);

return Promise.resolve(this.changeMode(mode))
.then(() => this.getState('mode'));
return Promise.resolve(this.changeMode(mode))
.then(() => this.getState('mode'));
} catch(ex) {
return Promise.reject(ex);
}
}

/**
Expand Down
10 changes: 7 additions & 3 deletions common/switchable-power.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(Power, Restorab
* @param {boolean} power
*/
setPower(power) {
power = boolean(power);
try {
power = boolean(power);

return Promise.resolve(this.changePower(power))
.then(() => this.power());
return Promise.resolve(this.changePower(power))
.then(() => this.power());
} catch(ex) {
return Promise.reject(ex);
}
}

togglePower() {
Expand Down
4 changes: 2 additions & 2 deletions controllers/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ module.exports = Thing.mixin(Parent => class extends Parent.with(State) {
* Get the available actions for this controller. All of these actions
* can be emitted.
*/
get actions() {
return this.getState('actions');
actions() {
return Promise.resolve(this.getState('actions'));
}

/**
Expand Down
12 changes: 8 additions & 4 deletions docs/common/battery-level.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ being charged.
.. sourcecode:: js

if(thing.matches('cap:battery-level')) {
console.log('Current battery level:', thing.batteryLevel);
console.log('Current battery level:', await thing.batteryLevel());
}

API
---

.. js:attribute:: batteryLevel
.. js:function:: batteryLevel()

Get the current battery level as a :doc:`percentage </values/percentage>`
between 0 and 100.

:returns: The battery level in percent.
:returns: Promise that resolves to the battery level in percent.

Example:

.. sourcecode:: js

const level = thing.batteryLevel;
thing.batteryLevel()
.then(level => ...)
.catch(...);

const level = await thing.batteryLevel();

Events
------
Expand Down

0 comments on commit 5121e61

Please sign in to comment.