Skip to content

Instantly share code, notes, and snippets.

@starbeast
Last active June 5, 2017 18:00
Show Gist options
  • Save starbeast/4ec667ccaae237026763448133477197 to your computer and use it in GitHub Desktop.
Save starbeast/4ec667ccaae237026763448133477197 to your computer and use it in GitHub Desktop.
MixinInjectable
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