@shopgate/pwa-common
Version:
Common library for the Shopgate Connect PWA.
21 lines • 2.12 kB
JavaScript
function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}import logGroup from'@shopgate/pwa-core/helpers/logGroup';/**
* Class to maintain different configurations
*/var Configuration=/*#__PURE__*/function(){/**
* Constructor
*/function Configuration(){_classCallCheck(this,Configuration);this.store=new Map();}/**
* Get a config value
* @param {*} key config key
* @param {*} defaultValue defaultValue
* @returns {*|undefined}
*/return _createClass(Configuration,[{key:"get",value:function get(key,defaultValue){return this.store.get(key)||defaultValue;}/**
* Sets a configuration.
* @param {*} key key
* @param {*} value value
* @returns {Configuration}
*/},{key:"set",value: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}
*/},{key:"update",value:function update(key,updater){if(!this.store.has(key)){logGroup('CONFIGURATION%c not found',{key:key},'#e0061e');return this;}if(typeof updater!=='function'){logGroup('CONFIGURATION%c updater is not function',{key:key},'#e0061e');return this;}var prevValue=this.store.get(key);var newValue=updater(this.store.get(key));// eslint-disable-next-line extra-rules/no-single-line-objects
logGroup('CONFIGURATION%c changed',{key:key,prevValue:prevValue,newValue:newValue},'#069215');return this.set(key,newValue);}}]);}();/** @type {Configuration} */export default new Configuration();