UNPKG

@shopgate/pwa-common

Version:

Common library for the Shopgate Connect PWA.

69 lines (64 loc) 1.67 kB
import logGroup from '@shopgate/pwa-core/helpers/logGroup'; /** * Class to maintain different configurations */ let Configuration = /*#__PURE__*/function () { /** * Constructor */ function Configuration() { this.store = new Map(); } /** * Get a config value * @param {*} key config key * @param {*} defaultValue defaultValue * @returns {*|undefined} */ var _proto = Configuration.prototype; _proto.get = function get(key, defaultValue) { return this.store.get(key) || defaultValue; } /** * Sets a configuration. * @param {*} key key * @param {*} value value * @returns {Configuration} */; _proto.set = function set(key, value) { this.store.set(key, value); return this; } /** * Update config with given callback function * @param {*} key key * @param {Function} updater function to update value * @returns {Configuration} */; _proto.update = function update(key, updater) { if (!this.store.has(key)) { logGroup('CONFIGURATION%c not found', { key }, '#e0061e'); return this; } if (typeof updater !== 'function') { logGroup('CONFIGURATION%c updater is not function', { key }, '#e0061e'); return this; } const prevValue = this.store.get(key); const newValue = updater(this.store.get(key)); // eslint-disable-next-line extra-rules/no-single-line-objects logGroup('CONFIGURATION%c changed', { key, prevValue, newValue }, '#069215'); return this.set(key, newValue); }; return Configuration; }(); /** @type {Configuration} */ export default new Configuration();