Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call typescript decorator method when the underlying function is executed

As per how typescript design works ,if i write a decorator like,

class foo{

   @fooDecorator
   public fooMethod(){

    }
}

it gives the transpiled javascript as

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
 };
var foo = (function () {
function foo() {
}
foo.prototype.fooMethod = function () {
};
__decorate([
    fooDecorator
], foo.prototype, "fooMethod", null);
return foo;
}());

as per there transpiled code whenever my app gets bootstrapped the decorator function get executed, but how i want is whenever the underlying function "fooMethod" is executed the decorator method "fooDecorator" should execute, how can i attain the same, please help me with the issue

like image 587
Pranay Dutta Avatar asked Apr 01 '16 06:04

Pranay Dutta


People also ask

When a decorator is applied on the class in TypeScript?

Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. Decorators are a stage 2 proposal for JavaScript and are available as an experimental feature of TypeScript. NOTE Decorators are an experimental feature that may change in future releases.

Are decorators still experimental in TypeScript?

As of writing this guide, decorators are still an experimental feature in TypeScript. To enable this feature, set the experimentalDecorators compiler flag either on the command line or in your tsconfig. json file.

What is target in decorator TypeScript?

target: Constructor function of the class if we used decorator on the static member, or prototype of the class if we used decorator on instance member. In our case it is firstMessage which is an instance member, so the target will refer to the prototype of the Greeter class. propertyKey: It is the name of the property.

Where can TypeScript decorators be applied to?

A Decorator is a special kind of declaration that can be applied to classes, methods, accessor, property, or parameter.


1 Answers

You can do it like this. In the decorator replace your original method with 'wrapper' that will first call the supplied method that should be called each time and then call the original one.

The code below illustrate this approach:

Your decorator:

export function Foo(funcToCallEveryTime: (...args: any[]) => void)
{
    return (target: any, key: string, descriptor: any) => 
    {
        var originalMethod = descriptor.value; 

        descriptor.value =  function (...args: any[]) {
            funcToCallEveryTime(...args);
            return originalMethod.apply(target, args);
        }

        return descriptor;
    }
}

And method you decorate with @Foo decorator like this:

@Foo((...args: any[]) => { console.log("Before method call:", args[0]); })
private TestMethod(param: string): void
{
    console.log("Method call");
    //...
}

Hope this helps.

like image 168
Amid Avatar answered Nov 12 '22 22:11

Amid