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

Inject component scope into class? #484

Open
mStirner opened this issue Jul 14, 2024 · 1 comment
Open

Inject component scope into class? #484

mStirner opened this issue Jul 14, 2024 · 1 comment

Comments

@mStirner
Copy link
Member

mStirner commented Jul 14, 2024

Usally a compponent definition looks like this:

class C_SSDP extends COMPONENT {

    constructor() {

        // inject logger, collection and schema object
        super("ssdp", SSDP.schema(), module);

        ...

    }

}

Instead of passing for every item class instance (in this case SSDP) the component scope (

constructor(obj, scope) {
), change the construction of components.

  • Remove passing the schema definition only (Pass whole class instead: super("ssdp", SSDP, module, this);)
  • Monkey patch/inject/define component scope on item class

In class.component.js then:

module.exports = class COMPONENT extends COMMON {

    constructor(name, cls, parent, scope) {

        ...

        Object.defineProperty(cls.prototype, "scope", {
          value: scope,
          writable: false,
          enumarable: false,
        });

}

This would be smart and "dumb" both ways.
Smart because no explitiz definition/passing is needed, dumb for the exact same reason, which could make it hard for documentation/figure out whats going on. Maybe anti pattern.

Same cathegory as "ShadowProperties", see #453

@mStirner
Copy link
Member Author

mStirner commented Jul 16, 2024

If done, extending the ITEM class, to pass the component instance down to other sub-classes?

Example:

module.exports = class Endpoint extends Item {
    constructor(obj, scope) {

        super(obj, [
            Command,
            State,
            Item
        ], scope);

(Pseudo-) Implementation:

module.exports = class Item {

    constructor(obj, classess, scope) {

        Object.assign(this, obj);
        
        classsess.forEach((cls) => {
//            InjectProperty();
//            DefineShaodwProperty();
               Object.defineProperty(cls.prototype, "scope", scope);
//             Object.defineProperty(cls, "scope", scope);
        });

mStirner added a commit to mStirner/backend that referenced this issue Sep 23, 2024
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