UNPKG

@allgemein/schema-api

Version:
108 lines 4.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegistryFactory = void 0; /** * Schema definitions can be different depending by handled structure. * We want use one api for use and accessing objects. The registry handle for a namespace can be * registered here. */ const lodash_1 = require("lodash"); const Constants_1 = require("../Constants"); const DefaultNamespacedRegistry_1 = require("./DefaultNamespacedRegistry"); const MetadataRegistry_1 = require("./MetadataRegistry"); const base_1 = require("@allgemein/base"); class RegistryFactory { /** * Return the registry handling schema/objects of given context or if not exists the generic default namescaped registry * * @param namespace */ static get(namespace = Constants_1.DEFAULT_NAMESPACE, options) { if (!this.$handles[namespace]) { for (const type of this.$types) { if ((0, lodash_1.isString)(type.pattern)) { if (namespace === type.pattern) { this.$handles[namespace] = Reflect.construct(type.registryClass, [namespace, options]); break; } } else { if ((0, lodash_1.isRegExp)(type.pattern) && type.pattern.test(namespace)) { this.$handles[namespace] = Reflect.construct(type.registryClass, [namespace, options]); break; } } } if (!this.$handles[namespace]) { // create default as fallback if nothing passes this.$handles[namespace] = new DefaultNamespacedRegistry_1.DefaultNamespacedRegistry(namespace, options); } if (this.$handles[namespace].prepare) { this.$handles[namespace].prepare(); } } return this.$handles[namespace]; } /** * Remove registry from handles list * * @param namespace */ static remove(namespace) { if ((0, lodash_1.has)(this.$handles, namespace)) { this.get(namespace).reset(); } delete this.$handles[namespace]; } /** * Register a special registry for a given namespace or pattern, remove previous if existed * * @param namespace * @param registry */ static register(namespace, registryClass) { const existsAlready = this.$types.find(x => (0, lodash_1.isString)(x.pattern) && (0, lodash_1.isString)(namespace) ? x.pattern === namespace : (0, lodash_1.isRegExp)(x.pattern) && (0, lodash_1.isRegExp)(namespace) ? x.pattern.source === namespace.source : x.pattern === namespace); if (!existsAlready) { this.$types.unshift({ pattern: namespace, registryClass: registryClass }); } } /** * Return active and registered namespaces */ static getNamespaces() { return (0, lodash_1.keys)(this.$handles); } static getTargets() { return MetadataRegistry_1.MetadataRegistry.$().getTargets(); } /** * Return all declared namespaces */ static getDeclaredNamespaces() { return [].concat(...MetadataRegistry_1.MetadataRegistry.$().getTargets().map(x => { const entries = MetadataRegistry_1.MetadataRegistry.$().getByContextAndTarget(Constants_1.METATYPE_ENTITY, x, 'merge'); return entries.map(x => { return { target: x.target, namespace: x.namespace ? x.namespace : base_1.C_DEFAULT }; }); })); } static reset() { for (const ns of this.getNamespaces()) { this.remove(ns); } } } exports.RegistryFactory = RegistryFactory; RegistryFactory.$types = [ { pattern: /.*/, registryClass: DefaultNamespacedRegistry_1.DefaultNamespacedRegistry } ]; RegistryFactory.$handles = {}; //# sourceMappingURL=RegistryFactory.js.map