7

Hi all I am wonder if that is possible get all classes that have given decorator javascript/typescript.

I have class for example

@mydecorator
class SomeClass1{
}

and somewere else in module

@mydecorator
class SomeClass2{
}

then in other module in runtime i would like to get all classes or constructors that have decorator @mydecorator ... I am afraid that this is not possible :(

export function getaAllClassesByDecorator(decorator:string){
... return constructor or something that  i am able to call static method 
}
Ivan Mjartan
  • 1,125
  • 1
  • 12
  • 23
  • What exactly do you need them for? – Bergi Sep 21 '16 at 20:46
  • I was look for elegant way how to globally register class and than create. Something like dependency pattern. – Ivan Mjartan Sep 22 '16 at 06:42
  • 1
    Doesn't the ES6 module system already give you that? – Bergi Sep 22 '16 at 06:59
  • Yes, but i am developing mobile web i was looking for some general hamburger menu for each view. So my idea is add label PATH to each view/component than in hamburger menu component get all classes with attribute Path and generate menu tree structure. – Ivan Mjartan Sep 22 '16 at 07:04
  • Maybe this helps: https://stackoverflow.com/questions/31618212/find-all-classes-in-a-javascript-application-that-extend-a-base-class/68470454#68470454 – Denis Giffeler Jul 26 '21 at 07:03

2 Answers2

12

You need to "register" all classes that uses @mydecorator somewhere. This register logic should be implemented in @mydecorator. For example:

export const registeredClasses = [];
export function mydecorator() {
     return function(target: Function) {
          registeredClasses.push(target);
     };
}

@mydecorator()
class SomeClass1{
}

@mydecorator()
class SomeClass2{
}

Now you can get all registered classes in the registeredClasses array

pleerock
  • 18,322
  • 16
  • 103
  • 128
  • This approach is what i am looking for ... if that will work ... you made my day! Tomorrow i am going to check it :-) thx – Ivan Mjartan Sep 21 '16 at 19:18
  • take a look on my repos on github there are more complex examples how to use decorators – pleerock Sep 22 '16 at 04:56
  • 2
    Will that work? If the classes decorated by this decorator are never referenced directly they will not be registered, because the decorators will never be executed. Am I wrong? – BrightShadow Oct 07 '21 at 08:40
  • @BrightShadow I am going through this right now and I think I am in the safe boat. The decorator function is not triggering if the classes are in separate modules. In which case I have to import the classes with side effects only at which point we might use plain old registration. I guess I was looking to implement a Java Spring like setup with decorators. – Sayak Mukhopadhyay Oct 10 '21 at 16:20
  • 2
    not work if the class not be called . – viola Dec 09 '21 at 11:28
0

You can't do that "automatically", but you can create a registry with said classes.
Then inside your decorator function you add these classes:

const REGISTRY: { [name: string]: any } = {};

function mydecorator(constructor) {
    REGISTRY[constructor.name] = constructor;
}

Then you can query this REGISTRY and know who used this decorator.

Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299