Estoy tratando de llamar a una función que tiene un decorador adjunto. Lo que hace es ejecutarse cada x segundos, lo que funciona bien; sin embargo, cuando intento registrar las propiedades en la función, generan undefined , pero en el constructor muestra la clase adecuada.
Si llamo a otra función que está en la clase, se llama a la función, el problema parece ser al acceder a las propiedades.
Estas son algunas de las formas en que he intentado llamar al método:
export function Repeat(interval: number, delay: number = 0) { return (target: any, prop: any, descriptor: PropertyDescriptor) => { timer(delay * 1000, interval * 1000) .pipe( tap(i => descriptor.value.call(target, i)), tap(i => target[prop].call(target, i)), tap(i => target[prop].bind(target)(i)) ) .subscribe(); }; }Así es como estoy implementando el decorador:
export class MyClass { constructor(private readonly v: Item) { console.log(this.v); // outputs: typeof Item } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: undefined this.testFunc(); // runs the function } testFunc(){ console.log('test function'); } }Aquí hay un ejemplo de stackblitz
Después de jugar un poco, puedo ver que puedo hacer esto:
export function Repeat(interval: number, delay = 0) { return function (target: any, prop: string, descriptor: PropertyDescriptor) { const originalMethod = descriptor.value; descriptor.value = function (this: GameObject, ...args: any[]) { const t = timer(delay * 1000, interval * 1000) .pipe(map<number, boolean>(i => originalMethod.apply(this, [i]))) .subscribe(); }; return descriptor; }; }Luego, cuando lo llamo en la clase, el repetidor funciona:
export class MyClass { constructor(private readonly v: Item) { this.repeat(0); } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: typeof Item } }Sin embargo, esto no es a lo que quiero recurrir, me gustaría que la función se llamara automáticamente y no manualmente.
Entonces, para resolver mi problema, tuve que usar un decorador en el nivel de clase junto con el nivel de método. Luego, en el constructor, ejecuto el método en la clase principal.
Primero importa el módulo reflect-metadata :
import 'reflect-metadata'; export function Component() { return <T extends { new (...args: any[]): any }>(target: T): any => { return class extends target { constructor(...args: any[]) { super(...args); const methods = Reflect.ownKeys(target.prototype) as string[]; methods.forEach((method) => { // Get the saved metadata const r = Reflect.getMetadata('token', target.prototype, method); if (typeof r === 'function') { // Here is where we call the method. this[method](); } }); } }; }; }Aquí está la función de repetición:
export function Repeat(interval: number, delay = 0, times = Infinity) { return function (target: any, prop: string, descriptor: PropertyDescriptor) { // Define the metadata to be used for later Reflect.defineMetadata('token', target[prop], target, prop); const originalMethod = descriptor.value; descriptor.value = function (this: any, ...args: any[]) { timer(delay * 1000, interval * 1000) .pipe(map<number, boolean>((i) => originalMethod.apply(this, [i]))) .subscribe(); }; return descriptor; }; }Así es como se implementa en la clase:
@Component() export class MyClass { constructor(private readonly v: Item) { console.log(this.v); // outputs: typeof Item } @Repeat(1) repeat(count: number) { console.log(this.v); // outputs: typeof Item } }Aquí está el ejemplo de stackblitz .