Git Product home page Git Product logo

crosscut.js's Introduction

Introduction

Build Status

Library for aspect-oriented programming with JavaScript, which takes advantage of ECMAScript 2016 decorators syntax.

For further reading on decorators, take a look at the spec.

Blog post, introduction to the AOP and the library could be found here.

Sample usage (Custom Decorators)

To use advice using custom decorators, you can use the makeMethodDecorator() and makeMemberDecorator() functions. Here's an example:

import { makeMethodDecorator } from 'crosscut.js';

export const Log = (message?: string) => {
  return makeMethodDecorator(((target: object, propertyKey: string | symbol) => {
    Reflect.defineMetadata(Log, { message }, target, propertyKey);
  });
};

class LoggerAspect {
  @beforeMethod({
    decorators: [ Log ],
  })
  invokeBeforeMethod(meta: Metadata) {
    let { message } = Reflect.getMetadata(Log, meta.method.context, meta.method.name);
    if (message != null) {
      console.log(message);
    } else {
      console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
    }
  }
}

class Article {
  id: number;
  title: string;
  content: string;
}

class ArticleCollection {
  articles: Article[] = [];

  @Log()
  getArticle(id: number) {
    console.log(`Getting article with id: ${id}.`);
    return this.articles.filter(a => {
      return a.id === id;
    }).pop();
  }

  @Log('Log a custom message')
  setArticle(article: Article) {
    console.log(`Setting article with id: ${article.id}.`);
    this.articles.push(article);
  }
}

new ArticleCollection().getArticle(1);
new ArticleCollection().setArticle({
  id: 2,
  title: 'Current event',
  content: '...',
});

// Result:
// Inside of the logger. Called ArticleCollection.getArticle with args: 1.
// Getting article with id: 1.
// Log a custom message
// Setting article with id: 2.

Sample usage (@Wove)

To use the aspect.js style of weaving, use the @Wove class decorator.

import {beforeMethod, Wove, Metadata} from 'crosscut.js';

class LoggerAspect {
  @beforeMethod({
    classNamePattern: /^Article/,
    methodNamePattern: /^(get|set)/
  })
  invokeBeforeMethod(meta: Metadata) {
    // meta.woveMetadata == { bar: 42 }
    console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
  }
}

class Article {
  id: number;
  title: string;
  content: string;
}

@Wove({ bar: 42 })
class ArticleCollection {
  articles: Article[] = [];
  getArticle(id: number) {
    console.log(`Getting article with id: ${id}.`);
    return this.articles.filter(a => {
      return a.id === id;
    }).pop();
  }
  setArticle(article: Article) {
    console.log(`Setting article with id: ${article.id}.`);
    this.articles.push(article);
  }
}

new ArticleCollection().getArticle(1);

// Result:
// Inside of the logger. Called ArticleCollection.getArticle with args: 1.
// Getting article with id: 1.

In case you're using crosscut.js in a browser environment the minifier may break the annotated code because of the performed mangling. In order to handle this problem you can use:

class LoggerAspect {
  @beforeMethod({
    classes: [ArticleCollection],
    methods: [ArticleCollection.prototype.getArticle, ArticleCollection.prototype.setArticle]
  })
  invokeBeforeMethod(meta: Metadata) {
    // meta.woveMetadata == { bar: 42 }
    console.log(`Inside of the logger. Called ${meta.className}.${meta.method.name} with args: ${meta.method.args.join(', ')}.`);
  }
}

class ArticleCollection {
  getArticle(id: number) {...}
  setArticle(article: Article) {...}
}

In this case you can omit the @Wove decorator.

This way, by explicitly listing the classes and methods which should be woven, you can prevent the unwanted effect of mangling.

Demo

git clone https://github.com/lizardruss/crosscut.js --depth 1
npm install -g ts-node
ts-node demo/index.ts

API

The library offers the following combinations of advices and joint points:

Method calls

  • beforeMethod(MethodSelector) - invoked before method call
  • afterMethod(MethodSelector) - invoked after method call
  • aroundMethod(MethodSelector) - invoked around method call
  • onThrowOfMethod(MethodSelector) - invoked on throw of method call
  • asyncOnThrowOfMethod(MethodSelector) - invoked on throw of async method call

Static method calls

  • beforeStaticMethod(MethodSelector) - invoked before static method call
  • afterStaticMethod(MethodSelector) - invoked after static method call
  • aroundStaticMethod(MethodSelector) - invoked around static method call
  • onThrowOfStaticMethod(MethodSelector) - invoked on throw of static method call
  • asyncOnThrowOfStaticMethod(MethodSelector) - invoked on throw of async static method call

Accessors

  • beforeSetter(MemberSelector) - invoked before setter call
  • afterSetter(MemberSelector) - invoked after setter call
  • aroundSetter(MemberSelector) - invoked around setter call
  • onThrowOfSetter(MemberSelector) - invoked on throw of setter call
  • asyncOnThrowOfSetter(MemberSelector) - invoked on throw of async setter call
  • beforeGetter(MemberSelector) - invoked before getter call
  • afterGetter(MemberSelector) - invoked after getter call
  • aroundGetter(MemberSelector) - invoked around getter call
  • onThrowOfGetter(MemberSelector) - invoked on throw of getter call
  • asyncOnThrowOfGetter(MemberSelector) - invoked on throw of async getter call

MethodSelector

export type DecoratorKey = string | symbol | MethodDecorator | MethodDecoratorFactory;

export interface MethodSelector {
  classNamePattern?: RegExp;
  methodNamePattern?: RegExp;
  classes?: Function[];
  methods?: Function[];
  decorators?: DecoratorKey[];
}

MemberSelector

export type DecoratorKey = string | symbol | MethodDecorator | MethodDecoratorFactory;

export interface MemberSelector {
  classNamePattern?: RegExp;
  fieldNamePattern?: RegExp;
  classes?: Function[];
  methods?: PropertyDescriptor[];
  decorators?: DecoratorKey[];
}

Metadata

export class Metadata {
  public method: MethodMetadata;
  public className: string;
  public woveMetadata: any;
}

MethodMetadata

export class MethodMetadata {
  public proceed: boolean;
  public name: string;
  public args: any[];
  public context: any;
  public result: any;
  public exception: any;
  public invoke: (...args: any[]) => any;
}

Roadmap

  • Tests
  • Type annotations and DTS generation
  • Aspect factories
    • Generic aspects
  • Implement the following advices:
    • Before
    • After
      • Throwing
      • Returning
    • Around
  • Implement the following joint points:
    • Method execution
    • Static method execution
    • Field get
    • Field set
  • Implement selectos
    • Class & method selector
    • Class & field selector
    • Method & field decorator selectors

License

MIT

crosscut.js's People

Contributors

mgechev avatar lizardruss avatar samverschueren avatar qwertyzw avatar islandman93 avatar jtheoof avatar mrajcok avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.