forked from cujojs/wire
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaop.js
260 lines (207 loc) · 7.61 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/** @license MIT License (c) copyright B Cavalier & J Hann */
/**
* wire/aop plugin
* Provides AOP for components created via wire, including Decorators,
* Introductions (mixins), and Pointcut-based Aspect Weaving.
*
* wire is part of the cujo.js family of libraries (http://cujojs.com/)
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*/
(function(define) {
define(['meld', 'when', './lib/connection'], function(meld, when, connection) {
var adviceTypes, adviceStep, undef;
// "after" is not included in these standard advice types because
// it is created as promise-aware advice.
adviceTypes = ['before', 'around', 'afterReturning', 'afterThrowing'];
adviceStep = 'connect:before';
//
// Decoration
//
function applyDecorator(target, Decorator, args) {
args = args ? [target].concat(args) : [target];
Decorator.apply(null, args);
}
function doDecorate(target, decorator, args, wire) {
function apply(Decorator) {
return args
? when(wire(args), function (resolvedArgs) {
applyDecorator(target, Decorator, resolvedArgs);
})
: applyDecorator(target, Decorator);
}
return when(wire.resolveRef(decorator), apply);
}
function decorateFacet(resolver, facet, wire) {
var target, options, promises;
target = facet.target;
options = facet.options;
promises = [];
for(var d in options) {
promises.push(doDecorate(target, d, options[d], wire));
}
when.chain(when.all(promises), resolver);
}
//
// Simple advice
//
function addSingleAdvice(addAdviceFunc, advices, proxy, advice, options, wire) {
function handleAopConnection(srcObject, srcMethod, adviceHandler) {
advices.push(addAdviceFunc(srcObject, srcMethod, adviceHandler));
}
return connection.parse(proxy, advice, options, wire, handleAopConnection);
}
function makeSingleAdviceAdd(adviceType) {
return function (source, sourceMethod, advice) {
return meld[adviceType](source, sourceMethod, advice);
};
}
function addAfterFulfillingAdvice(source, sourceMethod, advice) {
return meld.afterReturning(source, sourceMethod, function(promise) {
return when(promise, advice);
});
}
function addAfterRejectingAdvice(source, sourceMethod, advice) {
return meld.afterReturning(source, sourceMethod, function(promise) {
return when(promise, null, advice);
});
}
function addAfterPromiseAdvice(source, sourceMethod, advice) {
return meld.after(source, sourceMethod, function(promise) {
return when(promise, advice, advice);
});
}
function makeAdviceFacet(addAdviceFunc, advices) {
return function(resolver, facet, wire) {
var advice, target, advicesToAdd, promises;
target = facet;
advicesToAdd = facet.options;
promises = [];
for(advice in advicesToAdd) {
promises.push(addSingleAdvice(addAdviceFunc, advices,
target, advice, advicesToAdd[advice], wire));
}
when.chain(when.all(promises), resolver);
};
}
//
// Aspect Weaving
//
function applyAspectCombined(target, aspect, wire, add) {
return when(wire.resolveRef(aspect), function (aspect) {
var pointcut = aspect.pointcut;
if (pointcut) {
add(target, pointcut, aspect);
}
return target;
});
}
function applyAspectSeparate(target, aspect, wire, add) {
var pointcut, advice;
pointcut = aspect.pointcut;
advice = aspect.advice;
function applyAdvice(pointcut) {
return when(wire.resolveRef(advice), function (aspect) {
add(target, pointcut, aspect);
return target;
});
}
return typeof pointcut === 'string'
? when(wire.resolveRef(pointcut, applyAdvice))
: applyAdvice(pointcut);
}
function weave(resolver, proxy, wire, options, add) {
// TODO: Refactor weaving to use proxy.invoke
var target, path, aspects, applyAdvice;
aspects = options.aspects;
path = proxy.path;
if (!aspects || path === undef) {
resolver.resolve();
return;
}
target = proxy.target;
applyAdvice = applyAspectCombined;
// Reduce will preserve order of aspects being applied
when.chain(when.reduce(aspects, function(target, aspect) {
var aspectPath;
if (aspect.advice) {
aspectPath = aspect.advice;
applyAdvice = applyAspectSeparate;
} else {
aspectPath = aspect;
}
return typeof aspectPath === 'string' && aspectPath !== path
? applyAdvice(target, aspect, wire, add)
: target;
}, target), resolver);
}
return {
/**
* Creates wire/aop plugin instances.
*
* @param ready {Promise} promise that will be resolved when the context has been wired,
* rejected if there is an error during the wiring process, and will receive progress
* events for object creation, property setting, and initialization.
* @param destroyed {Promise} promise that will be resolved when the context has been destroyed,
* rejected if there is an error while destroying the context, and will receive progress
* events for objects being destroyed.
* @param options {Object}
*/
wire$plugin: function(ready, destroyed, options) {
// Track aspects so they can be removed when the context is destroyed
var woven, plugin, i, len, adviceType;
woven = [];
// Remove all aspects that we added in this context
when(destroyed, function() {
for(var i = woven.length - 1; i >= 0; --i) {
woven[i].remove();
}
});
/**
* Function to add an aspect and remember it in the current context
* so that it can be removed when the context is destroyed.
* @param target
* @param pointcut
* @param aspect
*/
function add(target, pointcut, aspect) {
woven.push(meld.add(target, pointcut, aspect));
}
function makeFacet(step, callback) {
var facet = {};
facet[step] = function(resolver, proxy, wire) {
callback(resolver, proxy, wire);
};
return facet;
}
// Plugin
plugin = {
facets: {
decorate: makeFacet('configure:after', decorateFacet),
afterFulfilling: makeFacet(adviceStep, makeAdviceFacet(addAfterFulfillingAdvice, woven)),
afterRejecting: makeFacet(adviceStep, makeAdviceFacet(addAfterRejectingAdvice, woven)),
after: makeFacet(adviceStep, makeAdviceFacet(addAfterPromiseAdvice, woven))
}
};
if(options.aspects) {
plugin.create = function(resolver, proxy, wire) {
weave(resolver, proxy, wire, options, add);
};
}
// Add all regular single advice facets
for(i = 0, len = adviceTypes.length; i<len; i++) {
adviceType = adviceTypes[i];
plugin.facets[adviceType] = makeFacet(adviceStep, makeAdviceFacet(makeSingleAdviceAdd(adviceType), woven));
}
return plugin;
}
};
});
})(typeof define == 'function'
// use define for AMD if available
? define
: function(deps, factory) {
module.exports = factory.apply(this, deps.map(require));
}
);