Last active
June 5, 2017 18:00
-
-
Save starbeast/4ec667ccaae237026763448133477197 to your computer and use it in GitHub Desktop.
MixinInjectable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let resolveParameters = function (target: any, params: any[]) { | |
if(params) { | |
params.forEach((param, index) => { | |
// this code is needed to make @Inject work | |
if (param && param.annotation && param.annotation instanceof Inject) { | |
param(target, void 0, index); | |
} | |
}); | |
} | |
return Reflect.getMetadata('parameters', target); | |
}; | |
let getParamTypesAndParameters = function(target, paramTypes, rawParameters) { | |
var parameters = Array(paramTypes.length).fill(null); | |
if(rawParameters) { | |
rawParameters.slice(0, paramTypes.length).forEach((el, i) => { | |
parameters[i] = el; | |
}); | |
} | |
return [paramTypes, parameters]; | |
}; | |
function MixinInjectable(...paramTypes: any[]) { | |
return function (childTarget: any) { | |
let parentTarget = Object.getPrototypeOf(childTarget.prototype).constructor; | |
let [parentParamTypes, parentParameters] = getParamTypesAndParameters(parentTarget, | |
Reflect.getMetadata('design:paramtypes', parentTarget), | |
Reflect.getMetadata('parameters', parentTarget) | |
); | |
let [childParamTypes, childParameters] = getParamTypesAndParameters(childTarget, | |
paramTypes || [], | |
resolveParameters(childTarget, paramTypes) | |
); | |
Reflect.defineMetadata('design:paramtypes', childParamTypes.concat(parentParamTypes), childTarget); | |
Reflect.defineMetadata('parameters', childParameters.concat(parentParameters), childTarget); | |
Reflect.defineMetadata('parentParameters', parentParameters, childTarget); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment