/
AutowireProcessor.js
104 lines (81 loc) · 3.69 KB
/
AutowireProcessor.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
/** @license MIT License (c) copyright 2013 original author or authors */
/**
* @Autowired annotation processor.
*
* Autowires parameters of function calls based on a given or the parameter name.
*
* needs wire to be in the configuration object.
*
*
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
* @author Peter Mucha
*
* @version 0.0.3
*/
"use strict";
(function(define) {
define(["underscore", "when"],
function(_, when) {
return {
annotation: "@Autowired",
processParameter: function(obj, fnDescription, annotatedParameters, context) {
//console.log("@Autowired("+ annotationParams +") attached at parameter: " + param.name);
var refs = {};
for(var i = 0; i < annotatedParameters.length; ++i){
var param = annotatedParameters[i];
var refName = param.annotation.parameters.length == 1
? param.annotation.parameters[0]
: param.name;
refs[param.name] = {$ref: refName};
}
context.wire(refs).then(function (resolvedRefs) {
var origFn = obj[fnDescription.name];
obj[fnDescription.name] = function(){
for(var i = 0; i < annotatedParameters.length; ++i){
var param = annotatedParameters[i];
while (arguments.length -1 < param.index)
[].push.call(arguments, undefined);
if (arguments[param.index] == null) //tests for null or undefined because 'null==undefined'
arguments[param.index] = resolvedRefs[param.name];
}
switch(arguments.length){
case 0: return origFn.call(obj);
case 1: return origFn.call(obj, arguments[0]);
case 2: return origFn.call(obj, arguments[0], arguments[1]);
case 3: return origFn.call(obj, arguments[0], arguments[1], arguments[2]);
default: return origFn.apply(obj, arguments);
}
};
},
function(err){
console.error("@Autowired failed: " + err);
});
},
processFunction: function(obj, fnDescription, annotationParams, context){
//console.log("@Autowired("+ annotationParams +") attached at function: " + fnDescription.name);
var refs = when.map(fnDescription.parameters,
function(param){
return {$ref: param.name};
});
when.map(refs, context.wire).then(function (resolvedRefs){
var origFn = obj[fnDescription.name];
obj[fnDescription.name] = function(){
while (arguments.length < resolvedRefs.length)
[].push.call(arguments, undefined);
for(var i = 0; i < resolvedRefs.length; ++i)
if (arguments[i] == null) //tests for null or undefined because 'null==undefined'
arguments[i] = resolvedRefs[i];
switch(arguments.length){
case 0: return origFn.call(obj);
case 1: return origFn.call(obj, arguments[0]);
case 2: return origFn.call(obj, arguments[0], arguments[1]);
case 3: return origFn.call(obj, arguments[0], arguments[1], arguments[2]);
default: return origFn.apply(obj, arguments);
}
};
}, function(err){console.error(err);});
}
};
});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});