react-promise-tracker
Version:
Simple React Promise tracker Hook/HOC helper to add loading spinner indicators
1 lines • 6.87 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.reactPromiseTracker=t(require("react")):e.reactPromiseTracker=t(e.react)}(window,function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(t,r){t.exports=e},function(e,t,r){e.exports=r(2)},function(e,t,r){"use strict";function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}r.r(t);var o,i,u,a=new(function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}var t,r,o;return t=e,(r=[{key:"emit",value:function(e){if(!e)return this;for(var t=arguments.length,r=new Array(t>1?t-1:0),n=1;n<t;n++)r[n-1]=arguments[n];var o=!0,i=!1,u=void 0;try{for(var a,f=this._e(e)[Symbol.iterator]();!(o=(a=f.next()).done);o=!0){var s=a.value;s.apply(s.ctx,[].concat(r)),1==s.off_event&&this.off(e,s)}}catch(e){i=!0,u=e}finally{try{o||null==f.return||f.return()}finally{if(i)throw u}}return this}},{key:"on",value:function(e,t,r){return e?(t.ctx=r,this._e(e).push(t),this):this}},{key:"once",value:function(e,t,r){return e?(t.ctx=r,t.off_event=!0,this.on(e,t)):this}},{key:"off",value:function(e,t){if(!e)return this;if(!this[e])return this;var r=this._e(e);return t?(this[e]=r.filter(function(e){return e!=t}),this):(delete this[e],this)}},{key:"_e",value:function(e){return this[e]||(this[e]=[])}}])&&n(t.prototype,r),o&&n(t,o),e}()),f=(u=0,(i="default-area")in(o={})?Object.defineProperty(o,i,{value:u,enumerable:!0,configurable:!0,writable:!0}):o[i]=u,o),s=function(e){return f[e]},c=function(e,t){l(t=t||"default-area");var r=p(t);a.emit("promise-counter-update",r,t);var n=function(){return y(t)};return e.then(n,n),e},l=function(e){Boolean(f[e])?f[e]++:f[e]=1},p=function(e){return f[e]>0},y=function(e){d(e);var t=p(e);a.emit("promise-counter-update",t,e)},d=function(e){f[e]--},b=r(0),h=r.n(b),v={area:"default-area",delay:0},m=function(e){return{area:e&&e.area?e.area:"default-area",delay:e&&e.delay?e.delay:0}};function g(e){return(g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function P(){return(P=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function w(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function O(e){return(O=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function j(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function S(e,t){return(S=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var _=function(e){return function(t){function r(e){var t,n,o;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),n=this,(t=!(o=O(r).call(this,e))||"object"!==g(o)&&"function"!=typeof o?j(n):o).state={promiseInProgress:!1,internalPromiseInProgress:!1,config:m(e.config)},t.notifyPromiseInProgress=t.notifyPromiseInProgress.bind(j(t)),t.updateProgress=t.updateProgress.bind(j(t)),t.subscribeToCounterUpdate=t.subscribeToCounterUpdate.bind(j(t)),t}var n,o,i;return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&S(e,t)}(r,b["Component"]),n=r,(o=[{key:"notifyPromiseInProgress",value:function(){var e=this;0===this.state.config.delay?this.setState({promiseInProgress:!0}):setTimeout(function(){var t=Boolean(s(e.state.config.area)>0);e.setState({promiseInProgress:t})},this.state.config.delay)}},{key:"updateProgress",value:function(e,t){this.setState({internalPromiseInProgress:e},t),e?this.notifyPromiseInProgress():this.setState({promiseInProgress:!1})}},{key:"subscribeToCounterUpdate",value:function(){var e=this;a.on("promise-counter-update",function(t,r){e.state.config.area===r&&e.updateProgress(t)})}},{key:"componentDidMount",value:function(){this.updateProgress(Boolean(s(this.state.config.area)>0),this.subscribeToCounterUpdate)}},{key:"componentWillUnmount",value:function(){a.off("promise-counter-update")}},{key:"render",value:function(){return h.a.createElement(e,P({},this.props,{config:this.state.config,promiseInProgress:this.state.promiseInProgress}))}}])&&w(n.prototype,o),i&&w(n,i),r}()};function k(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var u,a=e[Symbol.iterator]();!(n=(u=a.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){o=!0,i=e}finally{try{n||null==a.return||a.return()}finally{if(o)throw i}}return r}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var x=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:v,t=k(h.a.useState(m(e)),1)[0];h.a.useEffect(function(){t&&t.area&&s(t.area)>0&&(o(!0),f(!0))},[t]);var r=k(h.a.useState(!1),2),n=r[0],o=r[1],i=k(h.a.useState(!1),2),u=i[0],f=i[1],c=h.a.useRef(n),l=function(e,r){t.area===r&&(o(e),c.current=e,e?t&&t.delay&&0!==t.delay?setTimeout(function(){c.current&&f(!0)},t.delay):f(!0):f(!1))};return h.a.useEffect(function(){return c.current=n,a.on("promise-counter-update",function(e,t){l(e,t)}),function(){a.off("promise-counter-update")}},[]),{promiseInProgress:u}};r.d(t,"trackPromise",function(){return c}),r.d(t,"promiseTrackerHoc",function(){return _}),r.d(t,"usePromiseTracker",function(){return x})}])});