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

create/add a _class.shadow-property.js_ file #453

Open
mStirner opened this issue May 4, 2024 · 2 comments
Open

create/add a _class.shadow-property.js_ file #453

mStirner opened this issue May 4, 2024 · 2 comments

Comments

@mStirner
Copy link
Member

mStirner commented May 4, 2024

See OpenHausIO/documentation#35

Add a file to "system/component/class.shadow-property.js":

class ShadowProperty {

    constructor(value) {

        this[ShadowProperty.kValue] = value;

        /*
        // this or above?!
        Object.defineProperty(this, ShadowProperty.kValue, {
            value,
            enumerable: false,
            configurable: false,
            writable: true
        });
        */

    }

    static kValue = Symbol("kValue")

    static defineShadowProperty(obj, prop, value, options = {}) {

        let shadow = new ShadowProperty(value);

        Object.defineProperty(obj, prop, {
            set(val){
                //console.log(`set shadowProperty ${prop}=${val}`);
                shadow[ShadowProperty.kValue] = val;
            },
            get(){
                //console.log(`get shadowProperty ${prop}`);
                return shadow[ShadowProperty.kValue];
            },
            ...options,
            enumerable: false,
            configurable: false
        });

    }

};

/*
ShadowProperty.prototype.toString = function(){
    //return this[ShadowProperty.kValue];
    console.log(this[ShadowProperty.kValue])
    return "ShadowProperty";
};
*/

module.exports = ShadowProperty;
const { defineShadowProperty } = require("./class.shadow-property.js");


module.exports = class Parent {
    constructor() {


        defineShadowProperty(this, "shadow1", true);
        defineShadowProperty(this, "shadow2", Date.now());

        this.boolean = true;

    }
}
const Parent = require("./class.parent.js");
const ShadowProperty = require("./class.shadow-property.js");


const parent = new Parent();

console.log("parent shadow#1", parent.shadow1);
console.log("parent shadow#2", parent.shadow2);

console.log(parent);


parent.shadow1 = false;
parent.boolean = false;



console.log(parent, parent.shadow1);


/*
const shadow = new ShadowProperty({}, "shadow", true);

console.log(String(shadow));
*/
@mStirner
Copy link
Member Author

mStirner commented May 23, 2024

Make shadowProperties injectable.
So that the can be injeceted from outside the scope.

class Item {

    constructor() {
        defineShadowProperty(this, "prop1", { injectable: true });
        defineShadowProperty(this, "prop2", { injectable: true });
    }

}

const item = new Item();

// set from outside, where new instance is created
// with this, you dont need to have passed n properties into the constructor.
injectShadowPropertyValue(item, "prop1", "foo");
injectShadowPropertyValue(item, "prop2", false);

console.log(item.prop1); // foo
console.log(item.prop2); // false

No clue how to do that.
Implement a more complex logic, which may be a anti pattern/bad practice.

If implemented with getter/setter, make shadow property a special object?

@mStirner mStirner added this to the v4.0.0 release milestone May 23, 2024
@mStirner
Copy link
Member Author

mStirner commented May 23, 2024

Note

Should shadow properties setteble from outiside? (Without injectable)

If they are settable from outside, whats the difference to "injectable"?
injectable=true would only make sense, when the are not settable from outside.

Why not a "dependency injection" mechanism?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant