Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Export - Instantiate Variables/Objects

I am trying to modify the homebridge-wink3 code to add a variable so I can track the state in. I have 5 shades in my house, so each instance of the variable needs to be unique.

In the shade.js file, it has;

exports.default = ({ Characteristic, Service }) => {
  return {
    type: "shade",
    group: "shades",
    services: [{
      service: Service.WindowCovering,
      characteristics: [{
        characteristic: Characteristic.TargetPosition,
        get: (state, desired_state) => desired_state.position * 100,

I'd like to change the get (and set elsewhere in the code) so it uses a local variable lastState to track state.

    get: (state, desired_state) => { 
                if (desired_state.position != null) {
                        lastState = desired_state.position * 100;
                }
                else if (lastState != undefined) {
                        desired_state.position = lastState / 100;
                }
                return lastState;

I've spent hours trying to work out how to have the code maintain individual variables per shade (object instance), but they always seem to be sharing the same instance of the lastState variable.

What do I need to do here?

See https://github.com/sibartlett/homebridge-wink3/blob/master/src/devices/shade.js for the code.

like image 658
mriksman Avatar asked Oct 30 '22 00:10

mriksman


2 Answers

Important: What I understand to your question is that you want to clone an object (lastState or the object with the get and set method).

Suppose I have an object A like this:

var A = {
      aVariable: "Panem et circencem",
      aMethod: function () {
        return (["Veni", "vidi", "vici"]);
      }
    };

Now, suppose that I want to clone the object A to an object B.

function clone(obj) {
  if (null == obj || "object" != typeof obj) return obj;
  var copy = obj.constructor();
  for (var attr in obj) {
    if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  }
  return copy;
}

var B = clone(A);

This is a sample example:

    
var A = {
  aVariable: "Panem et circencem",
  aMethod: function () {
    return (["Veni", "vidi", "vici"]);
  }
};
function clone(obj) {
  if (null == obj || "object" != typeof obj) return obj;
  var copy = obj.constructor();
  for (var attr in obj) {
    if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  }
  return copy;
}

var B = clone(A);
B.aVariable = "Ad gloriam";
console.log(B);
console.log (A);

Then, you can clone/copy all your object in order to have some distinctives properties in your objects or clone the lastState in your code. I do not understood this part of your question, excuse me.

Note: this question try to answer the question. If I do not understood the question, please tell me a comment.

Also notice: If I do not answer the question, your are free to use the code poste above and copy my post in order to answer the question.

Likewise note: If you have a question, tell me a comment.

like image 79
SphynxTech Avatar answered Nov 15 '22 05:11

SphynxTech


You can declare lastState just above the return statement,

let lastState;
return {
  type: "shade",
  group: "shades",

or above the export statement,

let lastState;
export default ({ Characteristic, Service }) => {

if you declare lastState in the same scope as where you create the 5 instances then they will all share the same lastState.

like image 20
Pratheep Avatar answered Nov 15 '22 06:11

Pratheep