Skip to content

Commit

Permalink
Change state object creation logic
Browse files Browse the repository at this point in the history
Also added object compare function `objectEquals`
  • Loading branch information
lasthead0 committed May 12, 2022
1 parent d05a7bd commit 42c5ca8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
34 changes: 33 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,42 @@ function fetchHashOfString(string) {
return string.split('').map(v=>v.charCodeAt(0)).reduce((a,v)=>a+((a<<7)+(a<<3))^v).toString(16);
};

/* */
function objectEquals(x, y) {
'use strict';

if (x === null || x === undefined || y === null || y === undefined) return x === y;

// after this just checking type of one would be enough
if (x.constructor !== y.constructor) return false;

// if they are functions, they should exactly refer to same one (because of closures)
if (x instanceof Function) return x === y;

// if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES)
if (x instanceof RegExp) return x === y;

if (x === y || x.valueOf() === y.valueOf()) return true;
if (Array.isArray(x) && x.length !== y.length) return false;

// if they are dates, they must had equal valueOf
if (x instanceof Date) return false;

// if they are strictly equal, they both need to be object at least
if (!(x instanceof Object)) return false;
if (!(y instanceof Object)) return false;

// recursive object equality check
var p = Object.keys(x);
return Object.keys(y).every(i => p.indexOf(i) !== -1) &&
p.every(i => objectEquals(x[i], y[i]));
}

/* */
module.exports = {
sleep,
reverseMac,
decodeMiioJson,
fetchHashOfString
fetchHashOfString,
objectEquals
};
16 changes: 10 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const format = require('date-fns/format');
/* */
const yaml = require('js-yaml');
/* */
const {fetchHashOfString} = require('./lib/utils');
const {fetchHashOfString, objectEquals} = require('./lib/utils');
/* */
const {MiioHelper, Gateway3Helper} = require('./lib/helpers');
const XiaomiCloud = require('./lib/xiaomiCloud');
Expand Down Expand Up @@ -606,11 +606,15 @@ class XiaomiGateway3 extends utils.Adapter {
}
}

/* set state object */
await this.setObjectAsync(_id, Object.assign({},
{'_id': `${this.namespace}.${_id}`},
stateObject
));
/* set state object if it is not exist or `custom` changed */
if (_stateObject == undefined
|| !objectEquals(stateObject.common.custom[this.namespace], _stateObject.common.custom[this.namespace])
) {
await this.setObjectAsync(_id, Object.assign({},
{'_id': `${this.namespace}.${_id}`},
stateObject
));
}

/* set init state value if it is exist */
const val = init[stateName];
Expand Down

0 comments on commit 42c5ca8

Please sign in to comment.