Introduction
Library for aspect-oriented programming with JavaScript, which takes advantage of ECMAScript 2016 decorators syntax.
NOTE: if you are using
aspect.js
in a plain JavaScript project that uses@babel/plugin-proposal-decorators
, you must set itslegacy
property totrue
until #72 is fixed. See this migration note for more information. A sample project using JavaScript with Node >=10.12.0 (or >=8.12.0) & Babel 7.x can be found at https://github.com/matthewadams/aspect.js-babel7-poc. It's a good way to get going quickly withaspect.js
in that environment.
For further reading on decorators, take a look at the spec.
Blog post, introduction to the AOP and the library could be found here.
Talk from AngularConnect.
Sample usage
; new ArticleCollection.getArticle1; // Result:// Inside of the logger. Called ArticleCollection.getArticle with args: 1.// Getting article with id: 1.
In case you're using aspect.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:
In this case you can omit the @Advised
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/mgechev/aop.js --depth 1
npm install -g ts-node
ts-node demo/index.ts
API
The library offers the following combinations of advices and join points:
Method calls
beforeMethod(MethodSelector)
- invoked before method callafterMethod(MethodSelector)
- invoked after method callaroundMethod(MethodSelector)
- invoked around method callonThrowOfMethod(MethodSelector)
- invoked on throw of method callasyncOnThrowOfMethod(MethodSelector)
- invoked on throw of async method call
Static method calls
beforeStaticMethod(MethodSelector)
- invoked before static method callafterStaticMethod(MethodSelector)
- invoked after static method callaroundStaticMethod(MethodSelector)
- invoked around static method callonThrowOfStaticMethod(MethodSelector)
- invoked on throw of static method callasyncOnThrowOfStaticMethod(MethodSelector)
- invoked on throw of async static method call
Accessors
beforeSetter(PropertySelector)
- invoked before setter callafterSetter(PropertySelector)
- invoked after setter callaroundSetter(PropertySelector)
- invoked around setter callonThrowOfSetter(PropertySelector)
- invoked on throw of setter callasyncOnThrowOfSetter(PropertySelector)
- invoked on throw of async setter callbeforeGetter(PropertySelector)
- invoked before getter callafterGetter(PropertySelector)
- invoked after getter callaroundGetter(PropertySelector)
- invoked around getter callonThrowOfGetter(PropertySelector)
- invoked on throw of getter callasyncOnThrowOfGetter(PropertySelector)
- invoked on throw of async getter call
MethodSelector
PropertySelector
Metadata
MethodMetadata
Diagram
Here's a UML class diagram which shows the relations between the individual abstractions:
Roadmap
- Tests
- Type annotations and DTS generation
- Aspect factories
- Generic aspects
- Implement the following advices:
- Before
- After
- Throwing
- Returning
- Around
- Implement the following join points:
- Method execution
- Static method execution
- Filed get
- Field set
License
MIT