Get Class Instance from Within Decorator

Alexander Mattoni

I'm attempting to get my mind completely wrapped around decorators, and I had a couple neat ideas where I could use them in a reflux implementation I'm working on. I want to tag a store's class method with an action, and any time that action is generated, it knows to call any methods tagged with it.

First, I want to tag the Store method:

@Action
public setData(data: FakeData) {
    console.log(this);
    this.state.data = data;
}

then, in the action class, I want to register that method in an array:

class Action {
    private static methods: Method<FakeData>[] = [];

    public static register(method: Method<FakeData>) {
        this.methods.push(method);
    }

    constructor(payload: FakeData);
    constructor(store: Store, method: string);
    constructor(payload: any, method?: string, descriptor?: PropertyDescriptor) {
        if (method) {
            //TODO need actual instance of class here....
            Action.register(payload[method].bind(payload));
            return;
        }

        this.trigger(payload);
    }

    public get payload() {
        return Math.random();
    }

    private trigger(payload: FakeData) {
        Action.methods.forEach(m => m(payload));
    }
}

However, inside the constructor I do not have access to the actual instance of the store. I am able to get the constructor, but I'm not sure I am able to use that to achieve my goal. Perhaps instead of a constructor, I should make all stores strictly static?

I'm sure I'm not thinking about these decorators the right way, so any insight is appreciated!

Link to full code on Typescript Playground

Alexander Mattoni

It looks like decorators are called when the class is defined, not instantiated. This means that in order for this to work, I had to set all store methods to static, and that instances are not accessible:

@Action
public static setData(data: FakeData) {
    console.log(this);
    this.state.data = data;
}

Full working code here:

Typescript Playground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Hot to get the `this` from a class within it's decorator

Use an instance method as a decorator within another class

How to refer to the instance of a class from within the instance

Class Decorator (instance level)

How to pass an instance of of a class from within that class

How to access the instance of a class from an inner decorator class?

Access an injected service from within a Class decorator? Typescript / NestJS

Remove instance of a class from a vector within a vector

Referencing instance variables from within abstract class

How to define decorator within the class?

get class instance from require()

Use class instance variable to define a button label within the decorator (discord.py 2.0.0)

Class decorator instance int variables?

How to create a new class instance from within an instance?

Python: how to get create instance of a class within same class?

Python -- calling a function in same file as a class from within an instance of that class?

How to get the instance id from within an ec2 instance?

Get reference to object from base class. Decorator design pattern

Android get activity from within anonymous class

Getting a reference to a parent class instance from within an injected object

how to create a new instance of a class from within a method?

Creating an object from every instance of class and content within that element

Implementing a decorator within class "major flaw"?

Attaching a decorator to all functions within a class

How to use a decorator of a descriptor within a sub class

python decorator - registering class within base

How to declare instance of a class within a instance of class?

Java: Get instance of super class from subclass

Get TypeVars of custom Generic Class from instance