UNPKG

@decaf-ts/db-decorators

Version:

Agnostic database decorators and repository

68 lines (67 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OperationsRegistry = void 0; const Operations_1 = require("./Operations.cjs"); /** * @summary Holds the registered operation handlers * * @class OperationsRegistry * @implements IRegistry<OperationHandler<any>> * * @see OperationHandler * * @category Operations */ class OperationsRegistry { constructor() { this.cache = {}; } /** * @summary retrieves an {@link OperationHandler} if it exists * @param {string} target * @param {string} propKey * @param {string} operation * @param accum * @return {OperationHandler | undefined} */ get(target, propKey, operation, accum) { accum = accum || []; let name; try { name = typeof target === "string" ? target : target.constructor.name; accum.unshift(...Object.values(this.cache[name][propKey][operation] || [])); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (e) { if (typeof target === "string" || target === Object.prototype || Object.getPrototypeOf(target) === Object.prototype) return accum; } let proto = Object.getPrototypeOf(target); if (proto.constructor.name === name) proto = Object.getPrototypeOf(proto); return this.get(proto, propKey, operation, accum); } /** * @summary Registers an {@link OperationHandler} * @param {OperationHandler} handler * @param {string} operation * @param {{}} target * @param {string | symbol} propKey */ register(handler, operation, target, propKey) { const name = target.constructor.name; const handlerName = Operations_1.Operations.getHandlerName(handler); if (!this.cache[name]) this.cache[name] = {}; if (!this.cache[name][propKey]) this.cache[name][propKey] = {}; if (!this.cache[name][propKey][operation]) this.cache[name][propKey][operation] = {}; if (this.cache[name][propKey][operation][handlerName]) return; this.cache[name][propKey][operation][handlerName] = handler; } } exports.OperationsRegistry = OperationsRegistry;