// 构造次数 _constructionCounter: number = 0; // ResolvedReflectiveProvider列表 public _providers: ResolvedReflectiveProvider[]; // 父级注入器 public _parent: Injector|null; // ReflectiveKey id列表 keyIds: number[]; // 依赖对象列表 objs: any[];
ReflectiveInjector_ 构造函数
export class ReflectiveInjector_ implements ReflectiveInjector { constructor(_providers: ResolvedReflectiveProvider[], _parent?: Injector) { this._providers = _providers; // 设置父级注入器 this._parent = _parent || null; const len = _providers.length; this.keyIds = new Array(len); this.objs = new Array(len); // 初始化keyIds列表和objs对象列表 for (let i = 0; i < len; i++) { this.keyIds[i] = _providers[i].key.id; this.objs[i] = UNDEFINED; } } } const UNDEFINED = new Object();
ReflectiveInjector_ 类的方法
ReflectiveInjector_ 类中的方法较多,我们只分析其中比较重要的方法,首先先根据方法的实现的功能进行分类:
用于创建ReflectiveInjector注入器
用于获取对象
用于创建对象
用于获取工厂函数依赖对象
用于创建ReflectiveInjector注入器
// 基于Provider列表并创建子注入器 resolveAndCreateChild(providers: Provider[]): ReflectiveInjector { const ResolvedReflectiveProviders = ReflectiveInjector.resolve(providers); return this.createChildFromResolved(ResolvedReflectiveProviders); } // 基于已解析的ResolvedReflectiveProvider列表,创建子注入器 createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector { const inj = new ReflectiveInjector_(providers); inj._parent = this; return inj; }
用于获取对象
// 获取当前注入器的父级注入器 get parent(): Injector|null { return this._parent; } // 获取token对应的依赖对象 get(token: any, notFoundValue: any = THROW_IF_NOT_FOUND): any { return this._getByKey(ReflectiveKey.get(token), null, notFoundValue); } // 根据ReflectiveKey及visibility可见性,获取对应的依赖对象 private _getByKey(key: ReflectiveKey, visibility: Self|SkipSelf|null, notFoundValue: any): any { // const INJECTOR_KEY = ReflectiveKey.get(Injector); if (key === INJECTOR_KEY) { return this; } // 判断该依赖对象是否使用@Self装饰器定义,表示从本级注入器获取依赖对象 if (visibility instanceof Self) { return this._getByKeySelf(key, notFoundValue); } else { // 使用默认的方式获取依赖对象 return this._getByKeyDefault(key, notFoundValue, visibility); } } // 从本级注入器获取依赖对象 _getByKeySelf(key: ReflectiveKey, notFoundValue: any): any { const obj = this._getObjByKeyId(key.id); return (obj !== UNDEFINED) ? obj : this._throwOrNull(key, notFoundValue); } // 使用默认的方式获取依赖对象 _getByKeyDefault(key: ReflectiveKey, notFoundValue: any, visibility: Self|SkipSelf|null): any { let inj: Injector|null; // 判断该依赖对象是否使用@SkipSelf装饰器定义,表示不从本级注入器获取依赖对象 if (visibility instanceof SkipSelf) { inj = this._parent; } else { inj = this; } // 从本级注入器获取依赖对象,若本级获取不到,则从父级注入器中查找 while (inj instanceof ReflectiveInjector_) { const inj_ = <ReflectiveInjector_>inj; const obj = inj_._getObjByKeyId(key.id); if (obj !== UNDEFINED) return obj; inj = inj_._parent; } if (inj !== null) { return inj.get(key.token, notFoundValue); } else { return this._throwOrNull(key, notFoundValue); } } // 获取keyId对应的对象,如依赖对象未创建,则调用_new()方法创建一个,然后保存到 // this.objs对象列表中 private _getObjByKeyId(keyId: number): any { for (let i = 0; i < this.keyIds.length; i++) { if (this.keyIds[i] === keyId) { // const UNDEFINED = new Object(); if (this.objs[i] === UNDEFINED) { this.objs[i] = this._new(this._providers[i]); } return this.objs[i]; } } return UNDEFINED; }
用于创建对象
// 创建依赖对象 _new(provider: ResolvedReflectiveProvider): any { // 判断是否存在循环依赖 if (this._constructionCounter++ > this._getMaxNumberOfObjects()) { throw cyclicDependencyError(this, provider.key); } return this._instantiateProvider(provider); } // 获取最大的对象个数 private _getMaxNumberOfObjects(): number { return this.objs.length; } // 根据已解析的provider创建依赖对象。若是multi provider则,循环创建multi provider对象。 private _instantiateProvider(provider: ResolvedReflectiveProvider): any { if (provider.multiProvider) { const res = new Array(provider.resolvedFactories.length); for (let i = 0; i < provider.resolvedFactories.length; ++i) { res[i] = this._instantiate(provider, provider.resolvedFactories[i]); } return res; } else { return this._instantiate(provider, provider.resolvedFactories[0]); } } // 根据已解析的provider和已解析的工厂创建依赖对象 private _instantiate( provider: ResolvedReflectiveProvider, ResolvedReflectiveFactory: ResolvedReflectiveFactory): any { // 获取对象工厂函数 const factory = ResolvedReflectiveFactory.factory; // 获取工厂函数所依赖的对象列表 let deps: any[]; try { deps = ResolvedReflectiveFactory.dependencies .map(dep => this._getByReflectiveDependency(dep)); } catch (e) { if (e.addKey) { e.addKey(this, provider.key); } throw e; } // 调用对象工厂函数创建依赖对象 let obj: any; try { obj = factory(...deps); } catch (e) { throw instantiationError(this, e, e.stack, provider.key); } return obj; }
用于获取工厂函数依赖对象