react-async-call
Version:
Declarative promise handling in React
3 lines (2 loc) • 11.3 kB
JavaScript
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var l=require("react"),d=e(l),t=require("prop-types"),r=e(t);function n(e){return function(){return e}}function o(){}o.thatReturns=n,o.thatReturnsFalse=n(!1),o.thatReturnsTrue=n(!0),o.thatReturnsNull=n(null),o.thatReturnsThis=function(){return this},o.thatReturnsArgument=function(e){return e};var i=o,u=i,h=u,s=Object.prototype.hasOwnProperty;function a(e,t){return e===t?0!==e||1/e==1/t:e!=e&&t!=t}function c(e,t){if(a(e,t))return!0;if("object"!=typeof e||null===e||"object"!=typeof t||null===t)return!1;var n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(var o=0;o<n.length;o++)if(!s.call(t,n[o])||!a(e[n[o]],t[n[o]]))return!1;return!0}var p=c,f=function(e){return!!(e&&e.constructor&&e.call&&e.apply)},v=function(e,t){return(f(e.children)?e.children(t):e.children)||null},y=function(e,t){return f(e.children)&&e.children(t)||null},m=function(e,r,o,t,n){return function t(n){return l.createElement(e,null,function(e){return D(e,R,t.displayName,r),o(n,e)||null})}},_=void 0,g=void 0,b=void 0,R=void 0,w=void 0,S=void 0,j=void 0,P=void 0,x=void 0,U=void 0,E=void 0,O=void 0,F=void 0,W=void 0,C=void 0;function D(e,t,n,r,o,i,u,s){var a;if(!e)throw(a=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.")).framesToPop=1,a}var A="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function M(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function N(e,t){return e(t={exports:{}},t.exports),t.exports}var B="__global_unique_id__",T=function(){return A[B]=(A[B]||0)+1},V=N(function(e,t){t.__esModule=!0;n(d);var s=n(r),a=n(T);n(h);function n(e){return e&&e.__esModule?e:{default:e}}function l(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function p(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var f=1073741823;t.default=function(e,u){var t,n,r="__create-react-context-"+(0,a.default)()+"__",o=function(s){function a(){var e,t,n,r;l(this,a);for(var o=arguments.length,i=Array(o),u=0;u<o;u++)i[u]=arguments[u];return(e=t=c(this,s.call.apply(s,[this].concat(i)))).emitter=(n=t.props.value,r=[],{on:function(e){r.push(e)},off:function(t){r=r.filter(function(e){return e!==t})},get:function(){return n},set:function(e,t){n=e,r.forEach(function(e){return e(n,t)})}}),c(t,e)}return p(a,s),a.prototype.getChildContext=function(){var e;return(e={})[r]=this.emitter,e},a.prototype.componentWillReceiveProps=function(e){if(this.props.value!==e.value){var t=this.props.value,n=e.value,r=void 0;((o=t)===(i=n)?0!==o||1/o==1/i:o!=o&&i!=i)?r=0:(r="function"==typeof u?u(t,n):f,0!=(r|=0)&&this.emitter.set(e.value,r))}var o,i},a.prototype.render=function(){return this.props.children},a}(d.Component);o.childContextTypes=((t={})[r]=s.default.object.isRequired,t);var i=function(i){function u(){var e,n;l(this,u);for(var t=arguments.length,r=Array(t),o=0;o<t;o++)r[o]=arguments[o];return(e=n=c(this,i.call.apply(i,[this].concat(r)))).state={value:n.getValue()},n.onUpdate=function(e,t){0!=((0|n.observedBits)&t)&&n.setState({value:n.getValue()})},c(n,e)}return p(u,i),u.prototype.componentWillReceiveProps=function(e){var t=e.observedBits;this.observedBits=null==t?f:t},u.prototype.componentDidMount=function(){this.context[r]&&this.context[r].on(this.onUpdate);var e=this.props.observedBits;this.observedBits=null==e?f:e},u.prototype.componentWillUnmount=function(){this.context[r]&&this.context[r].off(this.onUpdate)},u.prototype.getValue=function(){return this.context[r]?this.context[r].get():e},u.prototype.render=function(){return e=this.props.children,(Array.isArray(e)?e[0]:e)(this.state.value);var e},u}(d.Component);return i.contextTypes=((n={})[r]=s.default.object,n),{Provider:o,Consumer:i}},e.exports=t.default});M(V);var I=N(function(e,t){t.__esModule=!0;var n=o(d),r=o(V);function o(e){return e&&e.__esModule?e:{default:e}}t.default=n.default.createContext||r.default,e.exports=t.default}),q=M(I);function Q(){var e=this.constructor.getDerivedStateFromProps(this.props,this.state);null!=e&&this.setState(e)}function k(n){this.setState(function(e){var t=this.constructor.getDerivedStateFromProps(n,e);return null!=t?t:null}.bind(this))}function z(e,t){try{var n=this.props,r=this.state;this.props=e,this.state=t,this.__reactInternalSnapshotFlag=!0,this.__reactInternalSnapshot=this.getSnapshotBeforeUpdate(n,r)}finally{this.props=n,this.state=r}}function H(e){var t=e.prototype;if(!t||!t.isReactComponent)throw new Error("Can only polyfill class components");if("function"!=typeof e.getDerivedStateFromProps&&"function"!=typeof t.getSnapshotBeforeUpdate)return e;var n=null,r=null,o=null;if("function"==typeof t.componentWillMount?n="componentWillMount":"function"==typeof t.UNSAFE_componentWillMount&&(n="UNSAFE_componentWillMount"),"function"==typeof t.componentWillReceiveProps?r="componentWillReceiveProps":"function"==typeof t.UNSAFE_componentWillReceiveProps&&(r="UNSAFE_componentWillReceiveProps"),"function"==typeof t.componentWillUpdate?o="componentWillUpdate":"function"==typeof t.UNSAFE_componentWillUpdate&&(o="UNSAFE_componentWillUpdate"),null!==n||null!==r||null!==o){var i=e.displayName||e.name,u="function"==typeof e.getDerivedStateFromProps?"getDerivedStateFromProps()":"getSnapshotBeforeUpdate()";throw Error("Unsafe legacy lifecycles will not be called for components using new component APIs.\n\n"+i+" uses "+u+" but also contains the following legacy lifecycles:"+(null!==n?"\n "+n:"")+(null!==r?"\n "+r:"")+(null!==o?"\n "+o:"")+"\n\nThe above lifecycles should be removed. Learn more about this warning here:\nhttps://fb.me/react-async-component-lifecycle-hooks")}if("function"==typeof e.getDerivedStateFromProps&&(t.componentWillMount=Q,t.componentWillReceiveProps=k),"function"==typeof t.getSnapshotBeforeUpdate){if("function"!=typeof t.componentDidUpdate)throw new Error("Cannot polyfill getSnapshotBeforeUpdate() for components that do not define componentDidUpdate() on the prototype");t.componentWillUpdate=z;var s=t.componentDidUpdate;t.componentDidUpdate=function(e,t,n){var r=this.__reactInternalSnapshotFlag?this.__reactInternalSnapshot:n;s.call(this,e,t,r)}}return e}Q.__suppressDeprecationWarning=!0,k.__suppressDeprecationWarning=!0,z.__suppressDeprecationWarning=!0;var L=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},G=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},J=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},K=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},X="initialValue",Y=function(e,n){var t=q(),r=t.Consumer,o=t.Provider,i=function(i){function u(){var e,t;L(this,u);for(var n=arguments.length,r=Array(n),o=0;o<n;o++)r[o]=arguments[o];return(e=t=K(this,i.call.apply(i,[this].concat(r)))).state={hasResult:!1,wasResolved:t.props.resolved},t.reset=function(e){t.setState(s(t.props)),e&&e()},K(t,e)}return J(u,i),u.prototype.componentDidMount=function(){var e=this.props,t=e.resolved;(t||e.hasOwnProperty(X))&&this.setState({hasResult:!0,result:t?e.result:e.initialValue,wasResolved:t})},u.getDerivedStateFromProps=function(e,t){var n=e.resolved;return e.reset?G({},s(e),{wasResolved:n}):n&&!t.wasResolved?t.hasResult?{result:e.reduce(t.result,e.result),wasResolved:n}:{hasResult:!0,result:e.result,wasResolved:n}:n!==t.wasResolved?{wasResolved:n}:null},u.prototype.render=function(){var e=this.state,t=this.props,n=G({hasResult:e.hasResult,reset:t.resetFn},e.hasResult?{result:e.result}:{});return l.createElement(o,{value:n},v(t,n))},u}(l.Component),s=function(e){return e.hasOwnProperty(X)?{hasResult:!0,result:e.initialValue}:{hasResult:!1}},a=H(i),u=function(i){function u(){var e,t;L(this,u);for(var n=arguments.length,r=Array(n),o=0;o<n;o++)r[o]=arguments[o];return(e=t=K(this,i.call.apply(i,[this].concat(r)))).reset=function(){var e=!(0<arguments.length&&void 0!==arguments[0])||arguments[0];t.ref.reset(e&&t._execute)},K(t,e)}return J(u,i),u.prototype.render=function(){var t=this;return l.createElement(e,null,function(e){return D(e,R,u.displayName,n),h(!t.props.hasOwnProperty("reset"),j),t._execute=e.execute,l.createElement(a,G({ref:function(e){return t.ref=e},resetFn:t.reset},t.props,e))})},u}(l.Component);return u.defaultProps={reduce:function(e,t){return t}},u.HasResult=m(r,u.displayName,function(e,t){return t.hasResult&&y(e,{result:t.result})},C,g),u.Resetter=m(r,u.displayName,function(e,t){return y(e,{reset:t.reset})},W,g),u},Z=function(s,e){var t=q(),n=t.Provider,r=t.Consumer,o=function(o){function i(){var e,u;L(this,i);for(var t=arguments.length,n=Array(t),r=0;r<t;r++)n[r]=arguments[r];return(e=u=K(this,o.call.apply(o,[this].concat(n)))).state={running:!0,rejected:!1,resolved:!1},u._callQueryFunc=function(e){u.setState({running:!0,rejected:!1,resolved:!1});var t=s(e);D(t&&t.then&&f(t.then),S),t.then(function(e){u._mounted&&u.setState({running:!1,rejected:!1,resolved:!0,result:e})},function(e){u._mounted&&u.setState({running:!1,rejected:!0,rejectReason:e})})},u._getState=function(){var e=u.state,t=e.running,n=e.resolved,r=e.rejected,o=e.result,i=e.rejectReason;return o=n?{result:o}:{},i=r?{rejectReason:i}:{},G({running:t,rejected:r,resolved:n,execute:u.execute},o,i)},u.execute=function(){u._callQueryFunc(u.props.params)},K(u,e)}return J(i,o),i.prototype.componentDidMount=function(){D(f(s),w,s),this._mounted=!0;var e=this.props;e.lazy||this._callQueryFunc(e.params)},i.prototype.componentWillUnmount=function(){this._mounted=!1},i.prototype.componentDidUpdate=function(e){var t=this.props,n=t.params;t.lazy||p(n,e.params)||this._callQueryFunc(n)},i.prototype.render=function(){var e=this._getState(),t=this.props;return l.createElement(n,{value:e},void 0!==t.children&&v(t,e))},i}(l.Component);return o.Running=m(r,!1,function(e,t){return t.running&&e.children},O,_),o.Resolved=m(r,!1,function(e,t){return t.resolved&&v(e,{result:t.result})},E,b),o.Rejected=m(r,!1,function(e,t){return t.rejected&&v(e,{rejectReason:t.rejectReason})},U,b),o.Executor=m(r,!1,function(e,t){return y(e,{execute:t.execute})},x,g),o.State=m(r,!1,y,F,g),o.ResultStore=Y(r,!1),o.Completed=m(r,!1,function(e,t){return(t.resolved||t.rejected)&&e.children},P,_),o};exports.default=Z;
//# sourceMappingURL=react-async-call.production.min.js.map