UNPKG

zhtsdi

Version:

Typescript写的依赖注入库。An dependency-injection library for TypeScript.

89 lines 3.81 kB
/** * Created by Weizehua on 2017/1/13. */ "use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; require("reflect-metadata"); let Injector = Injector_1 = class Injector { constructor() { } static get(ctor) { // A class without @Injectable() decorated can be instantiate too // **But that is not recommended if (Injector_1.strictDecorationCheck && !ctor[Injector_1.isInjectable]) { throw new Error("To get a instance of " + ctor.name + ", you should decorate it with @Injectable()."); } // There is a different instantiation for Singleton class. if (ctor[Injector_1.isSingleton]) return ctor[Injector_1.singletonInstance] = ctor[Injector_1.singletonInstance] || Injector_1.create(ctor); return Injector_1.create(ctor); } // static release<T>(ctor: Newable<T>): void{ // if (!ctor[Injector.isSingleton]) // throw new Error("only class decorated with @Singleton can be released."); // delete ctor[Injector.singletonInstance]; // } static create(ctor, follwer) { let argtypes = Reflect.getMetadata("design:paramtypes", ctor) || []; // If it depends on nothing, just new it. if (argtypes.length === 0) { return new ctor(); } // If it depends on something else, check if there is a dependency loop if (Injector_1.checkDependencyLoop && follwer && follwer.find(val => { return val === ctor; })) { throw new Error("Found dependency loop while trying to instantiate " + ctor.name + ". Please check your code."); } // Prepare for next dependency loop check let newFollower = [ctor].concat(follwer); // Instantiate the arguments it needs let args = []; for (let i = 0; i < argtypes.length; i++) { args.push(Injector_1.get(argtypes[i]), newFollower); } return new ctor(...args); } static instance() { return Injector_1._instance; } static injectableDecorator(ctor) { ctor[Injector_1.isInjectable] = true; return ctor; } static singletonDecorator(ctor) { ctor[Injector_1.isInjectable] = true; ctor[Injector_1.isSingleton] = true; return ctor; } }; Injector._instance = new Injector_1(); Injector.isInjectable = Symbol('Injector:is-injectable'); Injector.isSingleton = Symbol('Injector:is-singleton'); Injector.singletonInstance = Symbol('Injector:singleton-instance'); Injector.checkDependencyLoop = true; Injector.strictDecorationCheck = true; Injector = Injector_1 = __decorate([ Injectable(), __metadata("design:paramtypes", []) ], Injector); exports.Injector = Injector; function Injectable() { return Injector.injectableDecorator; } exports.Injectable = Injectable; function Singleton() { return Injector.singletonDecorator; } exports.Singleton = Singleton; var Injector_1; //# sourceMappingURL=index.js.map