Skip to content

Commit

Permalink
Merge pull request #404 from mStirner/dev
Browse files Browse the repository at this point in the history
Fix #401
  • Loading branch information
mStirner authored Jan 10, 2024
2 parents f2953ad + 5dbe58f commit 322fa71
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
10 changes: 8 additions & 2 deletions components/endpoints/class.endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const _expose = require("../../helper/expose.js");
* @see InterfaceStream components/devices/class.interfaceStream.js
*/
module.exports = class Endpoint extends Item {
constructor(obj) {
constructor(obj, scope) {

super(obj);

Expand All @@ -44,7 +44,13 @@ module.exports = class Endpoint extends Item {
//this.states = new States(obj.states);

this.states = obj.states.map((item) => {
return new State(item);
return new State(item, async () => {

// trigger update on endpoint item
// otherwise ui is not rendered/refreshed on state changed
await scope.update(this._id, this);

});
});

this.commands = obj.commands.map((item) => {
Expand Down
14 changes: 12 additions & 2 deletions components/endpoints/class.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const mongodb = require("mongodb");
*/
module.exports = class State {


constructor(obj) {
constructor(obj, changed = () => { }) {

Object.assign(this, obj);
this._id = String(obj._id);
Expand All @@ -46,18 +45,29 @@ module.exports = class State {

// check for value type, but allow null value
if (((typeof value) !== this.type) && (value !== null)) {
// TODO: uncomment & make active
//throw new TypeError(`Invalid type "${typeof (value)}"`);
return;
}

// fix #251
if (this.type === "number" && !(value >= this.min && value <= this.max)) {
// TODO: uncomment & make active
//throw new RangeError(`Invalid value "${value}"`);
return;
}

// prevent useles set/update
if (value === obj.value) {
return;
}

obj.value = value;

this.timestamps.updated = Date.now();

process.nextTick(changed, this);

},
configurable: false,
enumerable: true
Expand Down
12 changes: 9 additions & 3 deletions components/endpoints/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class C_ENDPOINTS extends COMPONENT {


this.hooks.post("add", (data, next) => {
next(null, new Endpoint(data));
next(null, new Endpoint(data, this));
});


Expand All @@ -54,7 +54,13 @@ class C_ENDPOINTS extends COMPONENT {
// fix for #368
data.states.forEach((state, i, arr) => {
if (!(state instanceof State)) {
arr[i] = new State(state);
arr[i] = new State(state, async () => {

// trigger update on endpoint item
// otherwise ui is not rendered/refreshed on state changed
await this.update(this._id, this);

});
}
});

Expand Down Expand Up @@ -126,7 +132,7 @@ instance.init((scope, ready) => {
} else {

data = data.map((item) => {
return new Endpoint(item);
return new Endpoint(item, scope);
});

scope.items.push(...data);
Expand Down

0 comments on commit 322fa71

Please sign in to comment.