@shopgate/engage
Version:
Shopgate's ENGAGE library.
12 lines • 3.13 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 _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArrayLimit(arr,i){var _arr=[];var _n=true;var _d=false;var _e=undefined;try{for(var _i=arr[Symbol.iterator](),_s;!(_n=(_s=_i.next()).done);_n=true){_arr.push(_s.value);if(i&&_arr.length===i)break;}}catch(err){_d=true;_e=err;}finally{try{if(!_n&&_i["return"]!=null)_i["return"]();}finally{if(_d)throw _e;}}return _arr;}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}import React from'react';/**
* General purpose hook that allow a `React.useMemo` hook with
* extended async support. The output contains the output value
* from promise and a potential error object.
* @param {Function} asyncFunction The function that will be executed to generate memoized output.
* @param {Array} asyncFunctionDependencies
* A list of dependencies (same behavior as with `React.useMemo`)
* @param {Any} initialValue The initial value that is set before promise is fulfilled.
* @returns {Array}
*/export function useAsyncMemo(asyncFunction){var asyncFunctionDependencies=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];var initialValue=arguments.length>2&&arguments[2]!==undefined?arguments[2]:null;var _React$useState=React.useState(initialValue),_React$useState2=_slicedToArray(_React$useState,2),value=_React$useState2[0],setValue=_React$useState2[1];var _React$useState3=React.useState(false),_React$useState4=_slicedToArray(_React$useState3,2),error=_React$useState4[0],setError=_React$useState4[1];/* eslint-disable react-hooks/exhaustive-deps */React.useEffect(function(){/**
* Wraps the async function as React doesn't allow direct async usage.
*/var fn=/*#__PURE__*/function(){var _ref=_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(){var output;return _regeneratorRuntime.wrap(function _callee$(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;_context.next=3;return asyncFunction();case 3:output=_context.sent;setValue(output);_context.next=10;break;case 7:_context.prev=7;_context.t0=_context["catch"](0);setError(_context.t0);case 10:case"end":return _context.stop();}},_callee,null,[[0,7]]);}));return function fn(){return _ref.apply(this,arguments);};}();fn();},asyncFunctionDependencies);/* eslint-enable react-hooks/exhaustive-deps */return[value,error];}