UNPKG

@web-atoms/core

Version:
62 lines (61 loc) 2.1 kB
System.register(["../core/types", "./TypeKey"], function (_export, _context) { "use strict"; var ArrayHelper, TypeKey, ServiceDescription, ServiceCollection, Scope; _export({ ServiceDescription: void 0, ServiceCollection: void 0, Scope: void 0 }); return { setters: [function (_coreTypes) { ArrayHelper = _coreTypes.ArrayHelper; }, function (_TypeKey) { TypeKey = _TypeKey.TypeKey; }], execute: function () { (function (Scope) { Scope[Scope["Global"] = 1] = "Global"; Scope[Scope["Scoped"] = 2] = "Scoped"; Scope[Scope["Transient"] = 3] = "Transient"; })(Scope || _export("Scope", Scope = {})); _export("ServiceDescription", ServiceDescription = class ServiceDescription { constructor(id, scope, type, factory) { this.id = id; this.scope = scope; this.type = type; this.factory = factory; this.factory = this.factory || (sp => { return sp.create(type); }); } }); _export("ServiceCollection", ServiceCollection = class ServiceCollection { constructor() { this.registrations = []; this.ids = 1; } register(type, factory, scope = Scope.Transient, id) { ArrayHelper.remove(this.registrations, r => id ? r.id === id : r.type === type); if (!id) { id = TypeKey.get(type); this.ids++; } const sd = new ServiceDescription(id, scope, type, factory); this.registrations.push(sd); return sd; } registerScoped(type, factory, id) { return this.register(type, factory, Scope.Scoped, id); } registerSingleton(type, factory, id) { return this.register(type, factory, Scope.Global, id); } get(type) { return this.registrations.find(s => s.id === type || s.type === type); } }); ServiceCollection.instance = new ServiceCollection(); } }; }); //# sourceMappingURL=ServiceCollection.js.map