Giter Club home page Giter Club logo

aspectjs-next's Introduction

AspectJS

AspectJS logo

An AOP framework for Typescript, compatible with Browsers & Node.

ci-status coverage report npm version license NPM Downloads bundlejs Latest Release



๐Ÿ“œ Abstract

Inspired by the AspectJ java framework, AspectJS leverages ES Decorators to bring Aspect Oriented Programming to Javascript and Typescript.

๐Ÿš€ Why?

ECMAScript Decorators are fantastic: they allow developers to hide boilerplate code behind a simple @ sign, keeping the code clean, easy to read, and easy to write. Widely used in popular projects such as Angular, Nest.js or TypeORM, decorators have one major drawback: they come with their own built-in behavior, making interoperability between tools difficult, and preventing them from being repurposed for other uses.

AspectJS takes a different approach, by introducing two important concepts: Annotations and Aspects.

  • An Annotation is essentially an empty decorator that marks a target (class, property, method or parameter) as a candidate for further enhancements.
  • Aspects can be selectively enabled to introduce new behaviors into the annotated elements.
  • without aspects
    const A = function( target: Function) {
       // behaviorA
    }
    const B = function( target: Function) {
       // behaviorB
    }
     
    @A() 
    @B()
    class MyClass {}
  • with aspects
    const af = new AnnotationFactory('my.org')
    const A = af.create('A');
    const B = af.create('B');
    
    @A()
    @B()
    class MyClass {}
    
    @Aspect()
    class AB_Aspect {
      // behavior A
      // behavior B
    }
    
    getWeaver().enable(new AB_Aspect())

โš™๏ธ Usage:

  • Install the package

    npm i @aspectjs/core @aspectjs/common
  • Create an annotation:

    // toasted.annotation.ts
    import { AnnotationFactory, AnnotationKind } from '@aspectjs/common';
    
    const ANNOTATION_FACTORY = new AnnotationFactory('demo');
    const Toasted = ANNOTATION_FACTORY.create(
      AnnotationKind.METHOD,
      function Toasted() {},
    );
  • Use that annotation on a class, a property, a method or a parameter:

    // main.ts
    class Main {
      @Toasted()
      run() {
        console.log('run');
      }
    }
  • Declare an aspect triggered by the annotation:

    // toasted.aspect.ts
    import { Around, AroundContext, Aspect, JoinPoint, on } from '@aspectjs/core';
    @Aspect()
    class ToastedAspect {
      @Around(on.methods.withAnnotations(Toasted))
      toast(ctxt: AroundContext, jp: JoinPoint, jpArgs: unknown[]) {
        const result = jp(...jpArgs);
        const text = `${ctxt.target.label} completed successfully`;
        showToast(text);
        return result;
      }
    }
  • Enable the aspect

    // aop.ts
    import { getWeaver } from '@aspectjs/core';
    
    getWeaver().enable(new ToastedAspect());

๐Ÿ”— Documentation

For more advanced usage, please read the documentation: https://aspectjs.gitlab.io/.

MIT Licensed

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.