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 a injectMethod() method #463

Closed
2 tasks done
mStirner opened this issue May 24, 2024 · 1 comment
Closed
2 tasks done

Create a injectMethod() method #463

mStirner opened this issue May 24, 2024 · 1 comment
Labels

Comments

@mStirner
Copy link
Member

mStirner commented May 24, 2024

Due to #460 a .bridge method cant be directly added to class.interface.js.
Doing so, would create a circular reference because the device item (or at leas the _id property) needs to be passed into the interface instances. (The _id i used to tell the connector on which device + interface a connection needs to be established).

It would be better to inject/add the .bridge method for the interface instance in the file class.devcie.js:

let iface = new Interface(obj, stream);

For that a injectMethod needs to be created, that add a property to the prototype of the given object.
(Which is absolutely the same as if the method was defined in the class body).

function injectMethod(obj, prop, value, options = {}){
    Object.defineProperty(Object.getPrototypeOf(obj), prop, {
        value,
        writable: true,
        enumerable: false,
        configurable: true,
        ...options
    });
}

This method could also be used in the system/component method definition:

_defineMethod(name, executor) {
Object.defineProperty(this, name, {
value: (...args) => {

Instead of add a method to this, wrap it in the injectMethod method:

_defineMethod(name, executor) {
    injectMethod(this, name, (...args) => {
        ...
    });
});
@mStirner mStirner added this to the v3.0.0 release milestone May 24, 2024
mStirner pushed a commit to mStirner/backend that referenced this issue May 24, 2024
@mStirner
Copy link
Member Author

A test breaks when used for injecting the .bridge method into a interface instance.
Wihtout Object.getPrototype... it works. Otherwise only the last interface this method is used, create bridge requests.

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

No branches or pull requests

1 participant