UNPKG

@shopgate/engage

Version:
16 lines 4.49 kB
import _regeneratorRuntime from"@babel/runtime/regenerator";function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg);var value=info.value;}catch(error){reject(error);return;}if(info.done){resolve(value);}else{Promise.resolve(value).then(_next,_throw);}}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise(function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value);}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err);}_next(undefined);});};}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;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import AppCommand from'@shopgate/pwa-core/classes/AppCommand';import event from'@shopgate/pwa-core/classes/Event';import logGroup from'@shopgate/pwa-core/helpers/logGroup';import{logger}from'@shopgate/pwa-core/helpers';import{GEOLOCATION_ERROR_DENIED,GEOLOCATION_ERROR_TIMEOUT,GEOLOCATION_DEFAULT_TIMEOUT}from"../constants/geolocationRequest";var LIB_VERSION='1.0';var COMMAND_NAME='getLocation';var EVENT_NAME='setLocation';/** * The GeolocationRequestApp class enables to retrieve the current geolocation of the device. * Therefore it utilizes app commands and events. */var GeolocationRequestApp=/*#__PURE__*/function(){/** * Constructor. */function GeolocationRequestApp(){var _this=this;_classCallCheck(this,GeolocationRequestApp);/** * Handles responses * @param {Object} response Response. */_defineProperty(this,"handleResponse",function(response){var queueEntry=_this.responseQueue.shift();if(typeof queueEntry==='undefined'){logger.error("".concat(EVENT_NAME," received but the response handler queue is empty."));return;}logGroup('GetLocationResponse ',response,'#9a9800');var resolve=queueEntry.resolve,reject=queueEntry.reject,timestamp=queueEntry.timestamp,timeout=queueEntry.timeout;if(typeof response!=='undefined'){resolve({accuracy:response.accuracy,latitude:response.latitude,longitude:response.longitude});return;}var error;var isTimeout=new Date().getTime()-timestamp>timeout;if(isTimeout){error=new Error('Timeout expired');error.code=GEOLOCATION_ERROR_TIMEOUT;}else{error=new Error('User denied Geolocation');error.code=GEOLOCATION_ERROR_DENIED;}reject(error);});/** * The getLocation command provide a serial parameter to identify related events. So this class * maintains a request queue that guarantees one event handler per dispatched command. */this.responseQueue=[];event.addCallback(EVENT_NAME,this.handleResponse);}return _createClass(GeolocationRequestApp,[{key:"dispatch",value:/** * Dispatches the request. * @param {number} [timeout=GEOLOCATION_DEFAULT_TIMEOUT] Timeout in ms for the request. * @returns {Promise} */function dispatch(){var _this2=this;var timeout=arguments.length>0&&arguments[0]!==undefined?arguments[0]:GEOLOCATION_DEFAULT_TIMEOUT;return new Promise(/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(resolve,reject){var commandParams;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_this2.responseQueue.push({resolve:resolve,reject:reject,timeout:timeout,timestamp:new Date().getTime()});commandParams={timeout:timeout/1000};logGroup('GetLocationRequest',commandParams,'#9a9800');_context.next=5;return new AppCommand().setCommandName(COMMAND_NAME).setLibVersion(LIB_VERSION).setCommandParams(commandParams).dispatch();case 5:case"end":return _context.stop();}},_callee);}));return function(_x,_x2){return _ref.apply(this,arguments);};}());}}]);}();export{GeolocationRequestApp};export default new GeolocationRequestApp();