-
Notifications
You must be signed in to change notification settings - Fork 0
/
aop.js
193 lines (162 loc) · 3.8 KB
/
aop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* aop.js
*
* JS AOP Library
*
* @author Jacky
*/
(function(window){
// 是否禁用AOP,默认为false
if (typeof disable_aop == 'undefined') {
disable_aop = false;
}
var beforeAdviceKey = 'before';
var aroundAdviceKey = 'around';
var afterAdviceKey = 'after';
/**
* 切点对象类
*/
var AopJoinPoint = function() {
var _args;
var _returnedValue;
var _joinPoint;
var _scope;
var _kind;
this.getArguments = function() {
return _args;
}
this.setArguments = function(args) {
_args = args;
}
this.getReturnedValue = function() {
return _returnedValue;
}
this.setReturnedValue = function(value) {
_returnedValue = value;
}
this.setKindOfAdvice = function(kind) {
_kind = kind;
}
this.getKindOfAdvice = function() {
return _kind;
}
this.setJoinPoint = function(joinPoint,scope) {
_joinPoint = joinPoint;
_scope = scope;
}
this.getJoinPoint = function() {
return _joinPoint;
}
this.process = function() {
return _joinPoint.apply(_scope, this.getArguments());
}
}
/**
* aop 处理过程
*
* @param Function func
* @return Function
*/
var __aop_process = function(func) {
var aop_process = function() {
if (disable_aop === true) {
return func.apply(this, arguments);
}
var args = Array.prototype.slice.apply(arguments);
var aopJoinPoint = new AopJoinPoint();
aopJoinPoint.setArguments(args);
aopJoinPoint.setJoinPoint(func, this);
var beforeAdvices = aop_process[beforeAdviceKey];
var aroundAdvices = aop_process[aroundAdviceKey];
var afterAdvices = aop_process[afterAdviceKey];
var len = 0;
if (beforeAdvices && beforeAdvices.length) {
len = beforeAdvices.length;
for (var i = 0; i < len; i++) {
aopJoinPoint.setKindOfAdvice(beforeAdviceKey);
beforeAdvices[i].apply(this,[aopJoinPoint]);
}
}
if (aroundAdvices && aroundAdvices.length) {
len = aroundAdvices.length;
for (var i = 0; i < len; i++) {
aopJoinPoint.setKindOfAdvice(aroundAdviceKey);
aopJoinPoint.setReturnedValue(aroundAdvices[i].apply(this,[aopJoinPoint]));
}
} else {
aopJoinPoint.setReturnedValue(func.apply(this, aopJoinPoint.getArguments()));
}
if (afterAdvices && afterAdvices.length) {
len = afterAdvices.length;
for (var i = 0; i < len; i++) {
aopJoinPoint.setKindOfAdvice(afterAdviceKey);
afterAdvices[i].apply(this,[aopJoinPoint]);
}
}
return aopJoinPoint.getReturnedValue();
};
aop_process.aop_process = true;
return aop_process;
};
/**
* 添加前切点
*
* @param Function func
* @param Function advice
* @return Function
*/
var aop_add_before = function(func, advice) {
if (!func.aop_process) {
func = __aop_process(func);
}
var advices = func[beforeAdviceKey];
if (null == advices) {
advices = [];
func[beforeAdviceKey] = advices;
}
advices.push(advice);
return func;
}
/**
* 添加后切点
*
* @param Function func
* @param Function advice
* @return Function
*/
var aop_add_after = function(func, advice) {
if (!func.aop_process) {
func = __aop_process(func);
}
var advices = func[afterAdviceKey];
if (null == advices) {
advices = [];
func[afterAdviceKey] = advices;
}
advices.push(advice);
return func;
}
/**
* 添加环绕切点
*
* @param Function func
* @param Function advice
* @return Function
*/
var aop_add_around = function(func, advice) {
if (!func.aop_process) {
func = __aop_process(func);
}
var advices = func[aroundAdviceKey];
if (null == advices) {
advices = [];
func[aroundAdviceKey] = advices;
}
advices.push(advice);
return func;
}
window.AopJoinPoint = AopJoinPoint;
window.aop_add_before = aop_add_before;
window.aop_add_after = aop_add_after;
window.aop_add_around = aop_add_around;
})(window);