UNPKG

vue-di-container

Version:
39 lines (38 loc) 1.61 kB
const PARAMETER_INJECTIONS = Symbol('parameter injections'); const PROPERTY_INJECTIONS = Symbol('property injections'); export class Metadata { constructor(reflection) { this.reflection = reflection; } getParameterKeys(fun, explicitKeys = []) { const reflectionTypes = this.reflection.getParameterTypes(fun); const parameterInjections = fun[PARAMETER_INJECTIONS] || []; const keys = []; const maxIndex = Math.max(reflectionTypes.length, parameterInjections.length, explicitKeys.length); for (let i = 0; i < maxIndex; i++) { keys[i] = explicitKeys[i] || parameterInjections[i] || reflectionTypes[i]; } return keys; } setParameterKey(fun, index, key) { const parameterInjections = fun[PARAMETER_INJECTIONS] || (fun[PARAMETER_INJECTIONS] = []); parameterInjections[index] = key; } getPropertyKeys(cls, explicitKeys = {}) { const propertyInjections = cls[PROPERTY_INJECTIONS] || {}; const keys = {}; const merged = Object.assign({}, propertyInjections, explicitKeys); for (const name of Object.keys(merged)) { const key = merged[name] || this.reflection.getPropertyType(cls, name) || null; if (key === null) { throw new Error('Property injection key missing'); } keys[name] = key; } return keys; } setPropertyKey(cls, name, key) { const propertyInjections = cls[PROPERTY_INJECTIONS] || (cls[PROPERTY_INJECTIONS] = {}); propertyInjections[name] = key; } }