aspect.js
4.59 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
define(["dojo/aspect"], function(aspect){
doh.register("tests.aspect",
[
function before(t){
var order = [];
var obj = {
method: function(a){
order.push(a);
}
};
var signal = aspect.before(obj, "method", function(a){
order.push(a);
return [a+1];
});
obj.method(0);
obj.method(2);
var signal2 = aspect.before(obj, "method", function(a){
order.push(a);
return [a+1];
});
obj.method(4);
signal.remove();
obj.method(7);
signal2.remove();
obj.method(9);
t.is(order, [0,1,2,3,4,5,6,7,8,9]);
},
function after(t){
var order = [];
var obj = {
method: function(a){
order.push(a);
return a+1;
}
};
var signal = aspect.after(obj, "method", function(a){
order.push(0);
return a+1;
});
obj.method(0); // 0, 0
var signal2 = aspect.after(obj, "method", function(a){
order.push(a);
});
obj.method(3); // 3, 0, 5
var signal3 = aspect.after(obj, "method", function(a){
order.push(3);
}, true);
obj.method(3); // 3, 0, 5, 3
signal2.remove();
obj.method(6); // 6, 0, 3
signal3.remove();
var signal4 = aspect.after(obj, "method", function(a){
order.push(4);
}, true);
signal.remove();
obj.method(7); // 7, 4
signal4.remove();
var signal5 = aspect.after(obj, "method", function(a){
order.push(a);
aspect.after(obj, "method", function(a){
order.push(a);
});
aspect.after(obj, "method", function(a){
order.push(a);
}).remove();
return a+1;
});
var signal6 = aspect.after(obj, "method", function(a){
order.push(a);
return a+2;
});
obj.method(8); // 8, 9, 10
obj.method(8); // 8, 9, 10, 12
t.is([0, 0, 3, 0, 5, 3, 0, 5, 3, 6, 0, 3, 7, 4, 8, 9, 10, 8, 9, 10, 12], order);
obj = {method: function(){}};
aspect.after(obj, "method", function(){
return false;
}, true);
t.t(obj.method() === false);
},
function afterMultiple(t){
var order = [];
obj = {
foo: function(){}
};
aspect.after(obj, "foo", function(){order.push(1)});
aspect.after(obj, "foo", function(){order.push(2)});
aspect.after(obj, "foo", function(){order.push(3)});
obj.foo();
t.is([1,2,3], order);
},
function around(t){
var order = [];
var obj = {
method: function(a){
order.push(a);
return a+1;
}
};
var beforeSignal = aspect.before(obj, "method", function(a){
order.push(a);
});
var signal = aspect.around(obj, "method", function(original){
return function(a){
a= a + 1;
a = original(a);
order.push(a);
return a+1;
};
});
order.push(obj.method(0));
obj.method(4);
t.is(order, [0,1,2,3,4,5,6]);
},
function around2(t){
var order = [];
var obj = {
method: function(a){
order.push(1);
}
};
var signal1 = aspect.around(obj, "method", function(original){
return function(a){
original();
order.push(2);
};
});
var signal2 = aspect.around(obj, "method", function(original){
return function(a){
original();
order.push(3);
};
});
var signal3 = aspect.around(obj, "method", function(original){
return function(a){
original();
order.push(4);
};
});
signal2.remove();
obj.method();
t.is(order, [1,2,4]);
},
function multipleRemove(t){
var foo = {bar: function(){}};
var order = [];
var signal1 = aspect.after(foo, "bar", function() {
order.push(1);
});
var signal2 = aspect.after(foo, "bar", function() {
order.push(2);
});
var signal3 = aspect.after(foo, "bar", function() {
order.push(3);
});
// This should execute all 3 callbacks
foo.bar();
signal2.remove();
signal3.remove();
// Ideally signal2 should not be removed again, but can happen if the app
// fails to clear its state.
signal2.remove();
// This should execute only the first callback, but notice that the third callback
// is executed as well
foo.bar();
t.is(order, [1,2,3,1]);
},
function delegation(t){
var order = [];
var proto = {
foo: function(x){
order.push(x);
return x;
},
bar: function(){
}
};
aspect.after(proto, "foo", function(x){
order.push(x + 1);
return x;
});
aspect.after(proto, "bar", function(x){
t.t(this.isInstance);
});
proto.foo(0);
function Class(){
}
Class.prototype = proto;
var instance = new Class();
instance.isInstance = true;
aspect.after(instance, "foo", function(x){
order.push(x + 2);
return x;
});
instance.bar();
instance.foo(2);
proto.foo(5);
t.is(order, [0,1,2,3,4,5,6]);
}
]
);
});