Skip to content
This repository has been archived by the owner on Jul 8, 2018. It is now read-only.

Latest commit

 

History

History
61 lines (45 loc) · 1.7 KB

before.md

File metadata and controls

61 lines (45 loc) · 1.7 KB

before

register a callback to be called before a class method is called. callback must return an arguments array.

API

arguments:

  • hook: function to run before method execution
  • methodName: name of method before which to hook
  • targetClass: class to modify

returns: the modified class

hook method

arguments:

  • instance: the target class instance whose method is about to be called
  • methodArguments: the arguments to be passed to the method

returns: modified/original arguments to be passed to the method

curry

The function is curried, so it can be used as a decorator after applying all arguments except for the class.

    function before<T extends object>(
        hook: <A extends Array<any>>(instance:T, methodArguments:A)=>A, 
        methodName: keyof T): ClassDecorator<T>;
 
    function before<T extends object>(
        hook: <A extends Array<any>>(instance:T, methodArguments:A)=>A, 
        methodName: keyof T, 
        target: Class<T>): Class<T>;

Example

function preMethod(instance:Logger, methodArguments:[string]){
   console.log('called before method with '+methodArguments[0]);
   return ['goodbye'];
}
 
@before(preMethod, 'printMessage')
class Logger{
 printMessage(text:string){
   console.log(text);
   return 'message printed: '+text;
 }
}
const logger = new Logger();

calling logger.printMessage('hello') will print (by order):

  • "called before method with hello"
  • "goodbye"

and to return "message printed: goodbye"

Inheriting decorated methods

Decorating class methods for inheritance has some notable edge cases, see Inheriting decorated methods for further details.