react-promise
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -1,141 +0,2 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var statusTypes = { | ||
none: 'none', | ||
pending: 'pending', | ||
rejected: 'rejected', | ||
resolved: 'resolved' | ||
}; | ||
var Async = function (_React$Component) { | ||
_inherits(Async, _React$Component); | ||
function Async(props) { | ||
_classCallCheck(this, Async); | ||
var _this = _possibleConstructorReturn(this, (Async.__proto__ || Object.getPrototypeOf(Async)).call(this, props)); | ||
_this.state = { | ||
status: statusTypes.none | ||
}; | ||
return _this; | ||
} | ||
_createClass(Async, [{ | ||
key: 'componentWillReceiveProps', | ||
value: function componentWillReceiveProps(nP) { | ||
if (nP.promise !== this.props.promise) { | ||
this.setState({ | ||
status: statusTypes.none | ||
}); | ||
this.handlePromise(nP.promise); | ||
} | ||
} | ||
}, { | ||
key: 'handlePromise', | ||
value: function handlePromise(prom) { | ||
var _this2 = this; | ||
this.promise = prom; | ||
this.setState({ | ||
status: statusTypes.pending | ||
}); | ||
prom.then(function (res) { | ||
if (_this2.promise !== prom) { | ||
return; // this promise has been switched for some other before it resolved, so we can early return | ||
} | ||
if (!_this2.unmounted) { | ||
_this2.setState({ | ||
status: statusTypes.resolved, | ||
value: res | ||
}); | ||
} | ||
}, function (err) { | ||
if (_this2.promise !== prom) { | ||
return; // this promise has been switched for some other before it rejected, so we can early return | ||
} | ||
if (!_this2.unmounted) { | ||
_this2.setState({ | ||
status: statusTypes.rejected, | ||
value: err | ||
}); | ||
} | ||
}); | ||
} | ||
}, { | ||
key: 'componentWillMount', | ||
value: function componentWillMount() { | ||
if (this.props.promise) { | ||
this.handlePromise(this.props.promise); | ||
} | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.unmounted = true; | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var props = this.props, | ||
state = this.state; | ||
switch (state.status) { | ||
case statusTypes.none: | ||
if (props.before) { | ||
return props.before(this.handlePromise.bind(this)); | ||
} | ||
break; | ||
case statusTypes.pending: | ||
if (props.pending) { | ||
if (typeof props.pending === 'function') { | ||
return props.pending(); | ||
} else { | ||
return props.pending; | ||
} | ||
} else if (Async.defaultPending) { | ||
if (typeof Async.defaultPending === 'function') { | ||
return Async.defaultPending(); | ||
} else { | ||
return Async.defaultPending; | ||
} | ||
} | ||
break; | ||
case statusTypes.resolved: | ||
if (props.then) { | ||
return props.then(state.value); | ||
} | ||
break; | ||
case statusTypes.rejected: | ||
if (props.catch) { | ||
return props.catch(state.value); | ||
} | ||
break; | ||
} | ||
return null; | ||
} | ||
}]); | ||
return Async; | ||
}(React.Component); | ||
Async.propTypes = { | ||
before: PropTypes.func, // renders it's return value before promise is handled | ||
then: PropTypes.func, // renders it's return value when promise is resolved | ||
catch: PropTypes.func, // renders it's return value when promise is rejected | ||
pending: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), // renders it's value when promise is pending | ||
promise: PropTypes.object // promise itself | ||
}; | ||
export default Async; | ||
import e from"react";import t from"prop-types";var n={none:"none",pending:"pending",rejected:"rejected",resolved:"resolved"},o=function(e){function t(t){e.call(this,t),this.state={status:n.none}}return e&&(t.__proto__=e),(t.prototype=Object.create(e&&e.prototype)).constructor=t,t.prototype.componentWillReceiveProps=function(e){e.promise!==this.props.promise&&(this.setState({status:n.none}),this.handlePromise(e.promise))},t.prototype.handlePromise=function(e){var t=this;this.promise=e,this.setState({status:n.pending}),e.then(function(o){t.promise===e&&(t.unmounted||t.setState({status:n.resolved,value:o}))},function(o){t.promise===e&&(t.unmounted||t.setState({status:n.rejected,value:o}))})},t.prototype.componentWillMount=function(){this.props.promise&&this.handlePromise(this.props.promise)},t.prototype.componentWillUnmount=function(){this.unmounted=!0},t.prototype.render=function(){var e=this.props,o=this.state;switch(o.status){case n.none:if(e.before)return e.before(this.handlePromise.bind(this));break;case n.pending:if(e.pending)return"function"==typeof e.pending?e.pending():e.pending;if(t.defaultPending)return"function"==typeof t.defaultPending?t.defaultPending():t.defaultPending;break;case n.resolved:if(e.then)return e.then(o.value);break;case n.rejected:if(e.catch)return e.catch(o.value)}return null},t}(e.Component);o.propTypes={before:t.func,then:t.func,catch:t.func,pending:t.oneOfType([t.node,t.func]),promise:t.object},o.Async=o;export default o;export{o as Async}; | ||
//# sourceMappingURL=react-promise.es.js.map |
{ | ||
"name": "react-promise", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "a react.js component for declarative way of handling promises", | ||
"main": "./dist/react-promise.cjs.js", | ||
"umd:main": "dist/react-promise.umd.umd.js", | ||
"module": "./dist/react-promise.es.js", | ||
"types": "./src/async.d.ts", | ||
"module": "./dist/react-promise.es.js", | ||
"source": "src/react-promise.js", | ||
"jspm": { | ||
@@ -15,3 +17,3 @@ "format": "esm" | ||
"precommit": "npm test", | ||
"build": "BABEL_ENV=rollup rollup -c", | ||
"build": "microbundle", | ||
"clean": "rimraf dist", | ||
@@ -44,33 +46,28 @@ "prepublishOnly": "npm run clean && npm run build" | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^22.4.4", | ||
"babel-loader": "^7.1.4", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-react": "^6.24.1", | ||
"babel-register": "^6.26.0", | ||
"chai": "^4.1.2", | ||
"enzyme": "^3.3.0", | ||
"enzyme-adapter-react-16": "^1.1.1", | ||
"husky": "^0.14.3", | ||
"jest": "^22.4.4", | ||
"raf": "^3.4.0", | ||
"react": "^16.3.2", | ||
"@babel/core": "^7.2.2", | ||
"@babel/preset-env": "^7.2.3", | ||
"@babel/preset-react": "^7.0.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^23.6.0", | ||
"babel-loader": "^8.0.4", | ||
"chai": "^4.2.0", | ||
"enzyme": "^3.8.0", | ||
"enzyme-adapter-react-16": "^1.7.1", | ||
"husky": "^1.3.1", | ||
"jest": "^23.6.0", | ||
"microbundle": "^0.9.0", | ||
"raf": "^3.4.1", | ||
"react": "^16.7.0", | ||
"react-addons-test-utils": "^15.6.2", | ||
"react-dom": "^16.3.2", | ||
"react-test-renderer": "^16.3.2", | ||
"rimraf": "^2.6.2", | ||
"rollup": "^0.59.2", | ||
"rollup-plugin-babel": "^3.0.4", | ||
"webpack": "^4.8.3", | ||
"webpack-cli": "^2.1.3", | ||
"webpack-dev-server": "^3.1.4" | ||
"react-dom": "^16.7.0", | ||
"react-test-renderer": "^16.7.0", | ||
"rimraf": "^2.6.3", | ||
"webpack": "^4.28.3", | ||
"webpack-cli": "^3.2.0", | ||
"webpack-dev-server": "^3.1.14" | ||
}, | ||
"dependencies": { | ||
"prop-types": "^15.6.1" | ||
"prop-types": "^15.6.2" | ||
}, | ||
"jest": { | ||
"transform": { | ||
"^.+\\.js$": "babel-jest" | ||
}, | ||
"setupFiles": [ | ||
@@ -77,0 +74,0 @@ "raf/polyfill" |
@@ -16,3 +16,3 @@ # react-promise | ||
and you want to make a component, which renders out in it's body 'a value'. Without react-async, such component looks like this: | ||
and you want to make a component, which renders out in it's body 'a value'. Without react-promise, such component looks like this: | ||
@@ -24,2 +24,4 @@ ```javascript | ||
this.state = {} | ||
} | ||
componentDidMount() { | ||
prom.then((value) => { | ||
@@ -35,3 +37,3 @@ this.setState({val: value}) | ||
and with react-async: | ||
and with react-promise: | ||
@@ -47,3 +49,3 @@ ```javascript | ||
Much simpler, right? | ||
Because the latter code handles that promise declaratively instead of imperatively. | ||
Because the latter code handles that promise declaratively instead of relying on react lifecycle hooks. | ||
In case you need user input before you can make the async call, there is a `before` property. Assign a function into it if you need to render a form for example. | ||
@@ -50,0 +52,0 @@ |
@@ -8,3 +8,3 @@ import * as React from 'react' | ||
catch?: (err: any) => React.ReactNode | ||
pending?: () => React.ReactNode | React.ReactNode | ||
pending?: (() => React.ReactNode) | React.ReactNode | ||
} | ||
@@ -16,6 +16,7 @@ | ||
declare class Async<T> extends React.Component<Props<T>, State> { | ||
defaultPending: () => React.ReactNode | React.ReactNode | ||
export declare class Async<T> extends React.Component<Props<T>, State> { | ||
static defaultPending: (() => React.ReactNode) | React.ReactNode | ||
} | ||
export default Async |
@@ -11,3 +11,3 @@ import React from 'react' | ||
class Async extends React.Component { | ||
export class Async extends React.Component { | ||
constructor(props) { | ||
@@ -114,2 +114,5 @@ super(props) | ||
// Ensures named exports compatibility with the CommonJS-style `module.exports = Async;` | ||
Async.Async = Async | ||
export default Async |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32293
21
12
113
248
2
Updatedprop-types@^15.6.2