@web-atoms/core-docs
Version:
61 lines • 2.35 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "../core/types", "./TypeKey"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceCollection = exports.ServiceDescription = exports.Scope = void 0;
const types_1 = require("../core/types");
const TypeKey_1 = require("./TypeKey");
var Scope;
(function (Scope) {
Scope[Scope["Global"] = 1] = "Global";
Scope[Scope["Scoped"] = 2] = "Scoped";
Scope[Scope["Transient"] = 3] = "Transient";
})(Scope = exports.Scope || (exports.Scope = {}));
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);
});
}
}
exports.ServiceDescription = ServiceDescription;
class ServiceCollection {
constructor() {
this.registrations = [];
this.ids = 1;
}
register(type, factory, scope = Scope.Transient, id) {
types_1.ArrayHelper.remove(this.registrations, (r) => id ? r.id === id : r.type === type);
if (!id) {
id = TypeKey_1.TypeKey.get(type) + this.ids;
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);
}
}
exports.ServiceCollection = ServiceCollection;
ServiceCollection.instance = new ServiceCollection();
});
//# sourceMappingURL=ServiceCollection.js.map