@@ -67,11 +67,11 @@ define(function(require) {
67
67
// Simple advice
68
68
//
69
69
70
- function addSingleAdvice ( addAdviceFunc , advices , proxy , advice , options , wire ) {
70
+ function addSingleAdvice ( addAdviceFunc , proxy , advice , options , wire , advices ) {
71
71
72
- function handleAopConnection ( srcObject , srcMethod , adviceHandler ) {
73
- checkAdvisable ( srcObject , srcMethod ) ;
74
- advices . push ( addAdviceFunc ( srcObject , srcMethod , adviceHandler ) ) ;
72
+ function handleAopConnection ( srcProxy , srcMethod , adviceHandler ) {
73
+ checkAdvisable ( srcProxy . target , srcMethod ) ;
74
+ advices . push ( addAdviceFunc ( srcProxy , srcMethod , adviceHandler ) ) ;
75
75
}
76
76
77
77
return connection . parse ( proxy , advice , options , wire , handleAopConnection ) ;
@@ -84,30 +84,38 @@ define(function(require) {
84
84
}
85
85
86
86
function makeSingleAdviceAdd ( adviceType ) {
87
- return function ( source , sourceMethod , advice ) {
88
- return meld [ adviceType ] ( source , sourceMethod , advice ) ;
87
+ return function ( srcProxy , sourceMethod , advice ) {
88
+ var aspect = { } ;
89
+ aspect [ adviceType ] = advice ;
90
+ return srcProxy . advise ( sourceMethod , aspect ) ;
89
91
} ;
90
92
}
91
93
92
- function addAfterFulfillingAdvice ( source , sourceMethod , advice ) {
93
- return meld . afterReturning ( source , sourceMethod , function ( promise ) {
94
- return when ( promise , advice ) ;
94
+ function addAfterFulfillingAdvice ( srcProxy , sourceMethod , advice ) {
95
+ return srcProxy . advise ( sourceMethod , {
96
+ afterReturning : function ( promise ) {
97
+ return when ( promise , advice ) ;
98
+ }
95
99
} ) ;
96
100
}
97
101
98
- function addAfterRejectingAdvice ( source , sourceMethod , advice ) {
99
- return meld . afterReturning ( source , sourceMethod , function ( promise ) {
100
- return when ( promise , null , advice ) ;
102
+ function addAfterRejectingAdvice ( srcProxy , sourceMethod , advice ) {
103
+ return srcProxy . advise ( sourceMethod , {
104
+ afterReturning : function ( promise ) {
105
+ return when ( promise , null , advice ) ;
106
+ }
101
107
} ) ;
102
108
}
103
109
104
- function addAfterPromiseAdvice ( source , sourceMethod , advice ) {
105
- return meld . after ( source , sourceMethod , function ( promise ) {
106
- return when ( promise , advice , advice ) ;
110
+ function addAfterPromiseAdvice ( srcProxy , sourceMethod , advice ) {
111
+ return srcProxy . advise ( sourceMethod , {
112
+ after : function ( promise ) {
113
+ return when ( promise , advice , advice ) ;
114
+ }
107
115
} ) ;
108
116
}
109
117
110
- function makeAdviceFacet ( addAdviceFunc , advices ) {
118
+ function makeAdviceFacet ( advices , addAdviceFunc ) {
111
119
return function ( resolver , facet , wire ) {
112
120
var advice , target , advicesToAdd , promises ;
113
121
@@ -116,8 +124,8 @@ define(function(require) {
116
124
promises = [ ] ;
117
125
118
126
for ( advice in advicesToAdd ) {
119
- promises . push ( addSingleAdvice ( addAdviceFunc , advices ,
120
- target , advice , advicesToAdd [ advice ] , wire ) ) ;
127
+ promises . push ( addSingleAdvice ( addAdviceFunc ,
128
+ target , advice , advicesToAdd [ advice ] , wire , advices ) ) ;
121
129
}
122
130
123
131
resolver . resolve ( when . all ( promises ) ) ;
@@ -128,28 +136,28 @@ define(function(require) {
128
136
// Aspect Weaving
129
137
//
130
138
131
- function applyAspectCombined ( target , aspect , wire , add ) {
139
+ function applyAspectCombined ( targetProxy , aspect , wire , aspects ) {
132
140
return when ( wire . resolveRef ( aspect ) , function ( aspect ) {
133
141
var pointcut = aspect . pointcut ;
134
142
135
143
if ( pointcut ) {
136
- add ( target , pointcut , aspect ) ;
144
+ aspects . push ( targetProxy . advise ( pointcut , aspect ) ) ;
137
145
}
138
146
139
- return target ;
147
+ return targetProxy ;
140
148
} ) ;
141
149
}
142
150
143
- function applyAspectSeparate ( target , aspect , wire , add ) {
151
+ function applyAspectSeparate ( targetProxy , aspect , wire , aspects ) {
144
152
var pointcut , advice ;
145
153
146
154
pointcut = aspect . pointcut ;
147
155
advice = aspect . advice ;
148
156
149
157
function applyAdvice ( pointcut ) {
150
158
return when ( wire . resolveRef ( advice ) , function ( aspect ) {
151
- add ( target , pointcut , aspect ) ;
152
- return target ;
159
+ aspects . push ( targetProxy . advise ( pointcut , aspect ) ) ;
160
+ return targetProxy ;
153
161
} ) ;
154
162
}
155
163
@@ -158,24 +166,23 @@ define(function(require) {
158
166
: applyAdvice ( pointcut ) ;
159
167
}
160
168
161
- function weave ( resolver , proxy , wire , options , add ) {
169
+ function weave ( proxy , wire , options , wovenAspects ) {
162
170
// TODO: Refactor weaving to use proxy.invoke
163
171
164
172
var target , path , aspects , applyAdvice ;
165
173
166
174
aspects = options . aspects ;
167
175
path = proxy . path ;
168
176
169
- if ( ! aspects || path === undef ) {
170
- resolver . resolve ( ) ;
177
+ if ( path === undef ) {
171
178
return ;
172
179
}
173
180
174
181
target = proxy . target ;
175
182
applyAdvice = applyAspectCombined ;
176
183
177
184
// Reduce will preserve order of aspects being applied
178
- resolver . resolve ( when . reduce ( aspects , function ( target , aspect ) {
185
+ return when . reduce ( aspects , function ( proxy , aspect ) {
179
186
var aspectPath ;
180
187
181
188
if ( aspect . advice ) {
@@ -186,10 +193,10 @@ define(function(require) {
186
193
}
187
194
188
195
return typeof aspectPath === 'string' && aspectPath !== path
189
- ? applyAdvice ( target , aspect , wire , add )
190
- : target ;
196
+ ? applyAdvice ( proxy , aspect , wire , wovenAspects )
197
+ : proxy ;
191
198
192
- } , target ) ) ;
199
+ } , proxy ) ;
193
200
}
194
201
195
202
/**
@@ -199,67 +206,51 @@ define(function(require) {
199
206
*/
200
207
return function ( options ) {
201
208
202
- // Track aspects so they can be removed when the context is destroyed
203
- var woven , plugin , i , len , adviceType ;
204
-
205
- woven = [ ] ;
206
-
207
- /**
208
- * Function to add an aspect and remember it in the current context
209
- * so that it can be removed when the context is destroyed.
210
- * @param target
211
- * @param pointcut
212
- * @param aspect
213
- */
214
- function add ( target , pointcut , aspect ) {
215
- woven . push ( meld . add ( target , pointcut , aspect ) ) ;
216
- }
217
-
218
- function makeFacet ( step , callback ) {
219
- var facet = { } ;
209
+ var plugin , aspects , makeAdvice ;
220
210
221
- facet [ step ] = function ( resolver , proxy , wire ) {
222
- callback ( resolver , proxy , wire ) ;
223
- } ;
224
-
225
- return facet ;
226
- }
211
+ aspects = [ ] ;
212
+ makeAdvice = makeAdviceFacet . bind ( null , aspects ) ;
227
213
228
- // Plugin
229
214
plugin = {
230
215
context : {
231
216
destroy : function ( resolver ) {
232
- woven . forEach ( function ( aspect ) {
233
- aspect . remove ( ) ;
234
- } ) ;
217
+ connection . removeAll ( aspects ) ;
235
218
resolver . resolve ( ) ;
236
219
}
237
220
} ,
238
221
facets : {
239
222
decorate : makeFacet ( 'configure:after' , decorateFacet ) ,
240
- afterFulfilling : makeFacet ( adviceStep , makeAdviceFacet ( addAfterFulfillingAdvice , woven ) ) ,
241
- afterRejecting : makeFacet ( adviceStep , makeAdviceFacet ( addAfterRejectingAdvice , woven ) ) ,
242
- after : makeFacet ( adviceStep , makeAdviceFacet ( addAfterPromiseAdvice , woven ) )
223
+ afterFulfilling : makeFacet ( adviceStep , makeAdvice ( addAfterFulfillingAdvice ) ) ,
224
+ afterRejecting : makeFacet ( adviceStep , makeAdvice ( addAfterRejectingAdvice ) ) ,
225
+ after : makeFacet ( adviceStep , makeAdvice ( addAfterPromiseAdvice ) )
243
226
}
244
227
} ;
245
228
246
229
if ( options . aspects ) {
247
230
plugin . create = function ( resolver , proxy , wire ) {
248
- weave ( resolver , proxy , wire , options , add ) ;
231
+ var woven = weave ( proxy , wire , options , aspects ) ;
232
+ resolver . resolve ( woven ) ;
249
233
} ;
250
234
}
251
235
252
236
// Add all regular single advice facets
253
- for ( i = 0 , len = adviceTypes . length ; i < len ; i ++ ) {
254
- adviceType = adviceTypes [ i ] ;
255
- plugin . facets [ adviceType ] = makeFacet ( adviceStep , makeAdviceFacet ( makeSingleAdviceAdd ( adviceType ) , woven ) ) ;
256
- }
237
+ adviceTypes . forEach ( function ( adviceType ) {
238
+ plugin . facets [ adviceType ] = makeFacet ( adviceStep ,
239
+ makeAdvice ( makeSingleAdviceAdd ( adviceType ) ) ) ;
240
+ } ) ;
257
241
258
242
return plugin ;
259
- } ;
243
+
244
+ function makeFacet ( step , callback ) {
245
+ var facet = { } ;
246
+
247
+ facet [ step ] = function ( resolver , proxy , wire ) {
248
+ callback ( resolver , proxy , wire ) ;
249
+ } ;
250
+
251
+ return facet ;
252
+ }
253
+
254
+ } ;
260
255
} ) ;
261
- } ) ( typeof define == 'function'
262
- // use define for AMD if available
263
- ? define
264
- : function ( factory ) { module . exports = factory ( require ) ; }
265
- ) ;
256
+ } ( typeof define == 'function' && define . amd ? define : function ( factory ) { module . exports = factory ( require ) ; } ) ) ;
0 commit comments