1 | #!/usr/bin/env node
|
2 |
|
3 | var path = require('path')
|
4 | , advisable = require(path.resolve(__dirname, '..', 'lib', 'advisable'))
|
5 | , target;
|
6 |
|
7 | function Target(val) {
|
8 | this.val = val;
|
9 | }
|
10 |
|
11 | Target.prototype.syncFunc = function (a, b) {
|
12 | console.log(
|
13 | 'Target.prototype.syncFunc, a = %d, b = %d, this.val = %d'
|
14 | , a
|
15 | , b
|
16 | , this.val
|
17 | );
|
18 |
|
19 | return a + b + this.val;
|
20 | };
|
21 |
|
22 | Target.prototype.asyncFunc = function (a, b, callback) {
|
23 | console.log(
|
24 | 'Target.prototype.asyncFunc, a = %d, b = %d, this.val = %d'
|
25 | , a
|
26 | , b
|
27 | , this.val
|
28 | );
|
29 |
|
30 | process.nextTick(function () {
|
31 | callback(null, a + b + this.val);
|
32 | }.bind(this));
|
33 | };
|
34 |
|
35 |
|
36 | advisable.sync.call(Target.prototype);
|
37 |
|
38 |
|
39 | advisable.async.call(Target.prototype);
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | target = new Target(1);
|
55 |
|
56 |
|
57 | console.log('syncFunc, no advice: %d', target.syncFunc(10, 100));
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | target = new Target(1);
|
63 | target.beforeSync('syncFunc', function (a, b) {
|
64 | this.val++;
|
65 | });
|
66 |
|
67 |
|
68 | console.log('syncFunc, increment before: %d', target.syncFunc(10, 100));
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | target = new Target(1);
|
74 | target.beforeSync('syncFunc', function (a, b) {
|
75 | return [ a/10, b/10 ];
|
76 | }, { mutate: true });
|
77 |
|
78 |
|
79 | console.log('syncFunc, divide args before: %d', target.syncFunc(10, 100));
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | target = new Target(1);
|
85 | target.afterSync('syncFunc', function (a, b) {
|
86 | this.val--;
|
87 | });
|
88 |
|
89 |
|
90 | console.log('syncFunc, decrement after: %d', target.syncFunc(10, 100));
|
91 |
|
92 |
|
93 | console.log('syncFunc, decremented: %d', target.val);
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | target = new Target(1);
|
99 | target.afterSync('syncFunc', function (v) {
|
100 | return v * 2;
|
101 | }, { mutate: true });
|
102 |
|
103 |
|
104 | console.log('syncFunc, double after: %d', target.syncFunc(10, 100));
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | target = new Target(1);
|
110 | target.aroundSync(
|
111 | 'syncFunc'
|
112 | , function (a, b) {
|
113 | console.log('around:before called with args: %d, %d', a, b);
|
114 | }
|
115 | , function (a, b) {
|
116 | console.log('around:after called with args: %d, %d', a, b);
|
117 | }
|
118 | );
|
119 |
|
120 |
|
121 | console.log('syncFunc, around advice: %d', target.syncFunc(10, 100));
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | target = new Target(1);
|
127 | target.aroundSync(
|
128 | 'syncFunc'
|
129 | , function (a, b) {
|
130 | console.log('around:before called with args: %d, %d', a, b);
|
131 | return [ a * 3, b * 3 ];
|
132 | }
|
133 | , function (v) {
|
134 | console.log('around:after called with arg: %d', v);
|
135 | return v + 123;
|
136 | }
|
137 | , { mutate: true }
|
138 | );
|
139 |
|
140 |
|
141 | console.log('syncFunc, mutated around advice: %d', target.syncFunc(10, 100));
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | target = new Target(1);
|
151 |
|
152 |
|
153 | target.asyncFunc(10, 100, function (err, result) {
|
154 | console.log('asyncFunc, no advice: %d', result);
|
155 | });
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | target = new Target(1);
|
161 | target.before('asyncFunc', function (a, b, callback) {
|
162 | this.val++;
|
163 |
|
164 |
|
165 | callback();
|
166 | });
|
167 |
|
168 |
|
169 | target.asyncFunc(10, 100, function (err, result) {
|
170 | console.log('asyncFunc, increment before: %d', result);
|
171 | });
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | target = new Target(1);
|
177 | target.before('asyncFunc', function (a, b, callback) {
|
178 | callback(null, a/10, b/10);
|
179 | }, { mutate: true });
|
180 |
|
181 |
|
182 | target.asyncFunc(10, 100, function (err, result) {
|
183 | console.log('asyncFunc, divide args before: %d', result);
|
184 | });
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | target = new Target(1);
|
190 | target.after('asyncFunc', function (a, b, callback) {
|
191 | this.val--;
|
192 | callback();
|
193 | });
|
194 |
|
195 |
|
196 | target.asyncFunc(10, 100, function (err, result) {
|
197 | console.log('asyncFunc, decrement after: %d', result);
|
198 |
|
199 | console.log('asyncFunc, decremented: %d', this.val);
|
200 | }.bind(target));
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | target = new Target(1);
|
206 | target.after('asyncFunc', function (v, callback) {
|
207 | callback(null, v * 2);
|
208 | }, { mutate: true });
|
209 |
|
210 |
|
211 | target.asyncFunc(10, 100, function (err, result) {
|
212 | console.log('asyncFunc, double after: %d', result);
|
213 | });
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | target = new Target(1);
|
219 | target.around(
|
220 | 'asyncFunc'
|
221 | , function (a, b, callback) {
|
222 | console.log('around:before called with args: %d, %d', a, b);
|
223 | callback();
|
224 | }
|
225 | , function (a, b, callback) {
|
226 | console.log('around:after called with args: %d, %d', a, b);
|
227 | callback();
|
228 | }
|
229 | );
|
230 |
|
231 |
|
232 | target.asyncFunc(10, 100, function (err, result) {
|
233 | console.log('asyncFunc, around advice: %d', result);
|
234 | });
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | target = new Target(1);
|
240 | target.around(
|
241 | 'asyncFunc'
|
242 | , function (a, b, callback) {
|
243 | console.log('around:before called with args: %d, %d', a, b);
|
244 | callback(null, a * 3, b * 3);
|
245 | }
|
246 | , function (v, callback) {
|
247 | console.log('around:after called with arg: %d', v);
|
248 | callback(null, v + 123);
|
249 | }
|
250 | , { mutate: true }
|
251 | );
|
252 |
|
253 |
|
254 | target.asyncFunc(10, 100, function (err, result) {
|
255 | console.log('syncFunc, mutated around advice: %d', result);
|
256 | });
|