UNPKG

@imqueue/rpc

Version:

RPC-like client-service implementation over messaging queue

90 lines 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IMQCache = void 0; /** * Generic cache registry */ class IMQCache { /** * Registers given cache adapter * * @param { ICache | string} adapter - adapter name or class or instance * @param {any} options - adapter specific options * @returns {IMQCache} */ static register(adapter, options) { const self = IMQCache; if (typeof adapter === 'string') { // istanbul ignore else if (!self.adapters[adapter]) { self.adapters[adapter] = new (require(`${__dirname}/cache/${adapter}.js`)[adapter])(); } } else { // istanbul ignore else if (!self.adapters[adapter.name]) { if (typeof adapter === 'function') { self.adapters[adapter.name] = new adapter(); } else { self.adapters[adapter.name] = adapter; } } } self.apply(adapter, options); return self; } /** * Overrides existing adapter options with the given * * @param {ICache | string} adapter - adapter to apply options to * @param {any} options - adapter specific options * @returns {IMQCache} */ static apply(adapter, options) { const self = IMQCache; if (!options) { return self; } const name = typeof adapter === 'string' ? adapter : adapter.name; let opts = self.options[name] || {}; self.options[name] = Object.assign(Object.assign({}, opts), options); return self; } /** * Initializes all registered cache adapters * * @returns {Promise<any>} */ static async init() { const self = IMQCache; const promises = []; for (let adapter of Object.keys(self.adapters)) { // istanbul ignore else if (!self.adapters[adapter].ready) { promises.push(self.adapters[adapter].init(self.options[adapter])); } } await Promise.all(promises); return self; } /** * Returns registered cache adapter by its given name or class * * @param { ICache | string} adapter - adapter name or class * @returns {ICache} - adapter instance */ static get(adapter) { return IMQCache.adapters[typeof adapter === 'string' ? adapter : adapter.name]; } } exports.IMQCache = IMQCache; IMQCache.options = {}; IMQCache.adapters = {}; //# sourceMappingURL=IMQCache.js.map