diff --git a/commonjs/withJob.js b/commonjs/withJob.js index 990bc5f..f699448 100644 --- a/commonjs/withJob.js +++ b/commonjs/withJob.js @@ -106,7 +106,8 @@ function withJob(config) { this.setState({ data: result ? result.data : null, error: null, - completed: result != null + completed: result != null, + workingProps: null }); } }, { @@ -171,12 +172,12 @@ function withJob(config) { this.resolveWork = function (props) { var workDefinition = void 0; - _this2.setState({ completed: false, data: null, error: null }); + _this2.setState({ completed: false, data: null, error: null, workingProps: props }); try { workDefinition = work(props); } catch (error) { - _this2.setState({ completed: true, error: error }); + _this2.setState({ completed: true, error: error, workingProps: null }); // Ensures asyncBootstrap stops return false; } @@ -184,23 +185,23 @@ function withJob(config) { if ((0, _utils.isPromise)(workDefinition)) { // Asynchronous result. return workDefinition.then(function (data) { - if (_this2.unmounted) { + if (_this2.unmounted || _this2.state.workingProps !== props) { return undefined; } - _this2.setState({ completed: true, data: data }); + _this2.setState({ completed: true, data: data, workingProps: null }); if (_this2.context.jobs) { _this2.context.jobs.register(id, { data: data }); } // Ensures asyncBootstrap continues return true; }).catch(function (error) { - if (_this2.unmounted) { + if (_this2.unmounted || _this2.state.workingProps !== props) { return undefined; } if (env === 'browser') { setTimeout(function () { if (!_this2.unmounted) { - _this2.setState({ completed: true, error: error }); + _this2.setState({ completed: true, error: error, workingProps: null }); } }, 16); } else { @@ -218,7 +219,7 @@ function withJob(config) { } // Synchronous result. - _this2.setState({ completed: true, data: workDefinition, error: null }); + _this2.setState({ completed: true, data: workDefinition, error: null, workingProps: null }); // Ensures asyncBootstrap continues return true; diff --git a/src/withJob.js b/src/withJob.js index 5a12cae..58acfd0 100644 --- a/src/withJob.js +++ b/src/withJob.js @@ -78,6 +78,7 @@ export default function withJob(config) { data: result ? result.data : null, error: null, completed: result != null, + workingProps: null, }) } @@ -110,12 +111,12 @@ export default function withJob(config) { resolveWork = (props) => { let workDefinition - this.setState({ completed: false, data: null, error: null }) + this.setState({ completed: false, data: null, error: null, workingProps: props }) try { workDefinition = work(props) } catch (error) { - this.setState({ completed: true, error }) + this.setState({ completed: true, error, workingProps: null }) // Ensures asyncBootstrap stops return false } @@ -124,10 +125,10 @@ export default function withJob(config) { // Asynchronous result. return workDefinition .then((data) => { - if (this.unmounted) { + if (this.unmounted || this.state.workingProps !== props) { return undefined } - this.setState({ completed: true, data }) + this.setState({ completed: true, data, workingProps: null }) if (this.context.jobs) { this.context.jobs.register(id, { data }) } @@ -135,14 +136,14 @@ export default function withJob(config) { return true }) .catch((error) => { - if (this.unmounted) { + if (this.unmounted || this.state.workingProps !== props) { return undefined } if (env === 'browser') { setTimeout( () => { if (!this.unmounted) { - this.setState({ completed: true, error }) + this.setState({ completed: true, error, workingProps: null }) } }, 16, @@ -162,7 +163,7 @@ export default function withJob(config) { } // Synchronous result. - this.setState({ completed: true, data: workDefinition, error: null }) + this.setState({ completed: true, data: workDefinition, error: null, workingProps: null }) // Ensures asyncBootstrap continues return true diff --git a/umd/react-jobs.js b/umd/react-jobs.js index 5469272..73217de 100644 --- a/umd/react-jobs.js +++ b/umd/react-jobs.js @@ -262,7 +262,8 @@ function withJob(config) { this.setState({ data: result ? result.data : null, error: null, - completed: result != null + completed: result != null, + workingProps: null }); } }, { @@ -327,12 +328,12 @@ function withJob(config) { this.resolveWork = function (props) { var workDefinition = void 0; - _this2.setState({ completed: false, data: null, error: null }); + _this2.setState({ completed: false, data: null, error: null, workingProps: props }); try { workDefinition = work(props); } catch (error) { - _this2.setState({ completed: true, error: error }); + _this2.setState({ completed: true, error: error, workingProps: null }); // Ensures asyncBootstrap stops return false; } @@ -340,23 +341,23 @@ function withJob(config) { if ((0, _utils.isPromise)(workDefinition)) { // Asynchronous result. return workDefinition.then(function (data) { - if (_this2.unmounted) { + if (_this2.unmounted || _this2.state.workingProps !== props) { return undefined; } - _this2.setState({ completed: true, data: data }); + _this2.setState({ completed: true, data: data, workingProps: null }); if (_this2.context.jobs) { _this2.context.jobs.register(id, { data: data }); } // Ensures asyncBootstrap continues return true; }).catch(function (error) { - if (_this2.unmounted) { + if (_this2.unmounted || _this2.state.workingProps !== props) { return undefined; } if (env === 'browser') { setTimeout(function () { if (!_this2.unmounted) { - _this2.setState({ completed: true, error: error }); + _this2.setState({ completed: true, error: error, workingProps: null }); } }, 16); } else { @@ -374,7 +375,7 @@ function withJob(config) { } // Synchronous result. - _this2.setState({ completed: true, data: workDefinition, error: null }); + _this2.setState({ completed: true, data: workDefinition, error: null, workingProps: null }); // Ensures asyncBootstrap continues return true; diff --git a/umd/react-jobs.min.js b/umd/react-jobs.min.js index b13c81d..28066a4 100644 --- a/umd/react-jobs.min.js +++ b/umd/react-jobs.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react"),require("prop-types")):"function"==typeof define&&define.amd?define(["react","prop-types"],t):"object"==typeof exports?exports.ReactJobs=t(require("react"),require("prop-types")):e.ReactJobs=t(e.React,e.PropTypes)}(this,function(e,t){return function(e){function t(r){if(o[r])return o[r].exports;var n=o[r]={i:r,l:!1,exports:{}};return e[r].call(n.exports,n,n.exports,t),n.l=!0,n.exports}var o={};return t.m=e,t.c=o,t.d=function(e,o,r){t.o(e,o)||Object.defineProperty(e,o,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var o=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(o,"a",o),o},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=3)}([function(t,o){t.exports=e},function(e,o){e.exports=t},function(e,t,o){"use strict";function r(){var e=0,t={};return{getNextId:function(){return e+=1},resetIds:function(){e=0},register:function(e,o){t[e]=o},get:function(e){return t[e]},getState:function(){return{jobs:t}}}}Object.defineProperty(t,"__esModule",{value:!0}),t.default=r},function(e,t,o){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.createJobContext=t.JobProvider=t.withJob=void 0;var n=o(4),u=r(n),i=o(6),a=r(i),s=o(2),c=r(s);t.withJob=u.default,t.JobProvider=a.default,t.createJobContext=c.default},function(e,t,o){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(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 i(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)}function a(e){if("object"!==(void 0===e?"undefined":f(e)))throw new Error("You must provide a config object to withJob");var t=e.work,o=e.LoadingComponent,r=e.ErrorComponent,a=e.serverMode,p=void 0===a?"resolve":a,m=e.shouldWorkAgain,j=void 0===m?v:m;if("function"!=typeof t)throw new Error("You must provide a work function to withJob");if(-1===h.indexOf(p))throw new Error("Invalid serverMode provided to asyncComponent");var g="undefined"==typeof window?"node":"browser";return function(e){var a=void 0,f=function(t){function f(e,t){n(this,f);var o=u(this,(f.__proto__||Object.getPrototypeOf(f)).call(this,e,t));return h.call(o),t.jobs&&(a=t.jobs.getNextId()),o}return i(f,t),c(f,[{key:"asyncBootstrap",value:function(){return"browser"===g||"defer"!==p&&this.resolveWork(this.props)}},{key:"componentWillMount",value:function(){var e=void 0;this.context.jobs&&(e="browser"===g?this.context.jobs.getRehydrate(a):this.context.jobs.get(a)),this.setState({data:e?e.data:null,error:null,completed:null!=e})}},{key:"componentDidMount",value:function(){this.state.completed||this.resolveWork(this.props),this.context.jobs&&"browser"===g&&this.context.jobs.removeRehydrate(a)}},{key:"componentWillUnmount",value:function(){this.unmounted=!0}},{key:"componentWillReceiveProps",value:function(e){j((0,y.propsWithoutInternal)(this.props),(0,y.propsWithoutInternal)(e),this.getJobState())&&this.resolveWork(e)}},{key:"render",value:function(){var t=this.state,n=t.data,u=t.error,i=t.completed;return u?r?d.default.createElement(r,s({},this.props,{error:u})):null:i?d.default.createElement(e,s({},this.props,{jobResult:n})):o?d.default.createElement(o,this.props):null}}]),f}(l.Component);f.displayName="WithJob("+(0,y.getDisplayName)(e)+")",f.contextTypes={jobs:b.default.shape({getNextId:b.default.func.isRequired,register:b.default.func.isRequired,get:b.default.func.isRequired,getRehydrate:b.default.func.isRequired,removeRehydrate:b.default.func.isRequired})};var h=function(){var e=this;this.resolveWork=function(o){var r=void 0;e.setState({completed:!1,data:null,error:null});try{r=t(o)}catch(t){return e.setState({completed:!0,error:t}),!1}return(0,y.isPromise)(r)?r.then(function(t){if(!e.unmounted)return e.setState({completed:!0,data:t}),e.context.jobs&&e.context.jobs.register(a,{data:t}),!0}).catch(function(t){if(!e.unmounted)return"browser"===g?setTimeout(function(){e.unmounted||e.setState({completed:!0,error:t})},16):(console.warn("Failed to resolve job"),console.warn(t)),!1}):(e.setState({completed:!0,data:r,error:null}),!0)},this.getJobState=function(){return{completed:e.state.completed,error:e.state.error,data:e.state.data}}};return f}}Object.defineProperty(t,"__esModule",{value:!0});var s=Object.assign||function(e){for(var t=1;t=0||Object.prototype.hasOwnProperty.call(e,r)&&(o[r]=e[r]);return o}function n(e){return e.displayName||e.name||"Component"}Object.defineProperty(t,"__esModule",{value:!0});var u="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};t.getDisplayName=n;var i=(t.isPromise=function(e){return"object"===(void 0===e?"undefined":u(e))&&"function"==typeof e.then},function(e){e.jobInitState,e.onJobProcessed;return r(e,["jobInitState","onJobProcessed"])});t.propsWithoutInternal=i},function(e,t,o){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(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 i(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)}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var o=0;o=0||Object.prototype.hasOwnProperty.call(e,r)&&(o[r]=e[r]);return o}function n(e){return e.displayName||e.name||"Component"}Object.defineProperty(t,"__esModule",{value:!0});var u="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};t.getDisplayName=n;var i=(t.isPromise=function(e){return"object"===(void 0===e?"undefined":u(e))&&"function"==typeof e.then},function(e){e.jobInitState,e.onJobProcessed;return r(e,["jobInitState","onJobProcessed"])});t.propsWithoutInternal=i},function(e,t,o){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function u(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 i(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)}Object.defineProperty(t,"__esModule",{value:!0});var s=function(){function e(e,t){for(var o=0;o