1 Star 0 Fork 5

前端 / aspects-js

forked from 散漫的水元素 / aspects-js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

aspects-js

aspects-js

Use aspect in node js

1.Install

Just install by npm

$ npm install --save aspects-js

2.Usage

You need require aspects-js at first of entry js file

require('aspects-js');

3.Add aspect

Add a js file to write a aspect. First, you should require class Aspect from aspects-js.

//file: testAspect.js
const { Aspect } = require('aspects-js');

Secondly, you should declare a class extends Aspect and implements property pointcut and functions for join point.

//file: testAspect.js
class TestAspect extends Aspect {
    get pointcut() { return '*.do*()' },
    before() { console.log('this is for before join point') },
    after() { console.log('this is for after join point') }
}

Then, you should exports a instance of your class which is extends Aspect

//file: testAspect.js
module.exports = new TestAspect();

At last, require your aspects at entry.js file

//file: entry.js
require('./testAspect.js');

Now, all classes when you required will be cut by all your aspects.

4.Classes and Interfaces

Interface Aspect

interface Aspect {
    readonly pointcut: Pointcut | string;

    after(joinPoint: JoinPoint, result: any, error: Error);
    afterReturn(joinPoint: JoinPoint, result: any): any;
    afterThrow(joinPoint: JoinPoint, error: Error): void;
    before(joinPoint: JoinPoint):void;
    around(joinPoint: JoinPoint): any;
}

Class JoinPoint

class JoinPoint {
    readonly type: Class;
    readonly fun: Function;
    readonly thisArg: any;
    readonly target: any;
    readonly args: any[];

    proceed(...args: any[]): any;
}

Class Pointcut

class Pointcut {
    constructor(pointcut: string);

    matches(type: Class | Function): boolean;
}

5.Pointcut expression

1.Normal

"ClassName.FunctionName()"

2.Execution

"execution(ClassName.FunctionName())"

3.Within

"within(ClassName)"

4.Wildcards

> * Match all word

"*Service.do*()"

Match all methods which's name is start with "do" and in classes which's name is end with "Service"

> ? Match one word

"you?.do?()"

> + Or operate

"within(Test1+Test2)"

Just match all methods in classes which's name is "Test1" or "Test2"

空文件

简介

javascript面向切面编程,可用于日志输出,事务处理等无关业务的统一代码处理 展开 收起
JavaScript
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/liqingu/aspects-js.git
git@gitee.com:liqingu/aspects-js.git
liqingu
aspects-js
aspects-js
master

搜索帮助