@shopgate/engage
Version:
Shopgate's ENGAGE library.
47 lines • 4.45 kB
JavaScript
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;}/**
* @callback AppInitializationHandler
* @param {Object} params
* @param {Function} params.dispatch Redux dispatch function
* @param {Function} params.getState Redux dispatch function
* @returns {Promise}
*/ /**
* Class to maintain handler functions that are executed before the React app initializes.
*
* Handlers are invoked before the React app initializes. They can
* interact with Redux via "dispatch" and "getState" and e.g. initialize states based on
* information which is retrieved via app commands.
*
* Since the handler execution is blocking and postpones the React app initialization, this
* system should be handled with care.
*/var AppInitialization=/*#__PURE__*/function(){/**
* Constructor
*/function AppInitialization(){_classCallCheck(this,AppInitialization);this.store=new Map();}/**
* Get an initialization handler
* @param {*} key Identifier for the handler
* @returns {Promise}
*/return _createClass(AppInitialization,[{key:"get",value:function get(key){return this.store.get(key);}/**
* Sets an initialization handler.
*
* @param {*} key Identifier for the handler
* @param {AppInitializationHandler} handler The actual handler
* @returns {AppInitialization}
*
* @example
* const handler = ({ dispatch, getState }) => {
* return new Promise(async (resolve) => {
* // Retrieve some data from the app via app command
* const appCommandResponse = await getAppCommandResponse();
* // Retrieve some data from Redux (store will be nearly empty at this point)
* const reduxValue = getValueFromRedux(getState());
* // Dispatch Redux actions to initialize a Redux state
* dispatch(reduxAction(reduxValue, appCommandResponse));
* resolve();
* });
*
* appInitialization.set('handler-key', handler);
* }
*/},{key:"set",value:function set(key,handler){this.store.set(key,handler);return this;}/**
* Invokes all registered handlers
* @returns {Promise<{results: [], errors: []}>}
*/},{key:"initialize",value:function(){var _initialize=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref){var dispatch,getState,results,errors,_i,_Array$from,handler;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:dispatch=_ref.dispatch,getState=_ref.getState;results=[];errors=[];// eslint-disable-next-line no-restricted-syntax
_i=0,_Array$from=Array.from(this.store.values());case 4:if(!(_i<_Array$from.length)){_context.next=20;break;}handler=_Array$from[_i];_context.prev=6;_context.t0=results;_context.next=10;return handler({dispatch:dispatch,getState:getState});case 10:_context.t1=_context.sent;_context.t0.push.call(_context.t0,_context.t1);_context.next=17;break;case 14:_context.prev=14;_context.t2=_context["catch"](6);errors.push(_context.t2);case 17:_i++;_context.next=4;break;case 20:return _context.abrupt("return",{results:results,errors:errors});case 21:case"end":return _context.stop();}},_callee,this,[[6,14]]);}));function initialize(_x){return _initialize.apply(this,arguments);}return initialize;}()}]);}();export default new AppInitialization();