@vlsergey/react-promise
Version:
React wrappers for Promises
163 lines (162 loc) • 6.21 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { PureComponent } from 'react';
import * as memoize from './memoize';
import shallowCompare from './shallowCompare';
var PromisesComponent = /** @class */ (function (_super) {
__extends(PromisesComponent, _super);
function PromisesComponent(props) {
var _this = _super.call(this, props) || this;
_this.mounted = false;
_this.prevPromises = undefined;
_this.state = {
error: null,
errors: {},
values: {},
};
_this.cleanValues = function () {
_this.resetValues();
};
_this.setValue = function (key, value) {
var _a;
if (_this.mounted) {
_this.setState(function (state) {
var _a;
return ({
values: __assign(__assign({}, state.values), (_a = {}, _a[key] = value, _a)),
});
});
}
else {
/* eslint-disable-next-line react/no-direct-mutation-state */
_this.state = __assign(__assign({}, _this.state), { values: __assign(__assign({}, _this.state.values), (_a = {}, _a[key] = value, _a)) });
}
};
_this.resetValues = function () {
if (_this.mounted) {
_this.setState({
error: null,
errors: {},
values: {},
});
}
else {
/* eslint-disable-next-line react/no-direct-mutation-state */
_this.state = __assign(__assign({}, _this.state), { error: null, errors: {}, values: {} });
}
};
_this.subscribe();
return _this;
}
PromisesComponent.prototype.componentDidMount = function () {
this.mounted = true;
this.subscribe();
};
PromisesComponent.prototype.componentDidUpdate = function () {
this.subscribe();
};
PromisesComponent.prototype.componentWillUnmount = function () {
this.unsubscribe();
this.mounted = false;
};
PromisesComponent.prototype.subscribe = function () {
var e_1, _a;
var _this = this;
var _b = this.props, cleanOnChange = _b.cleanOnChange, promises = _b.promises;
if (shallowCompare(this.prevPromises, promises)) {
return;
}
if (cleanOnChange)
this.resetValues();
this.prevPromises = promises;
if (promises === null || promises === undefined)
return;
var _loop_1 = function (k) {
var key = k;
var promise = promises[key];
var cachedResult = memoize.find(promise);
if (cachedResult)
this_1.setValue(key, cachedResult);
promise
.then(function (value) {
memoize.set(promise, value);
if (_this.prevPromises === promises) {
_this.setValue(key, value);
}
})
.catch(function (error) {
if (_this.prevPromises === promises) {
_this.setState(function (state) {
var _a;
return ({
error: error,
errors: __assign(__assign({}, state.errors), (_a = {}, _a[key] = error, _a)),
});
});
}
});
};
var this_1 = this;
try {
for (var _c = __values(Object.keys(promises)), _d = _c.next(); !_d.done; _d = _c.next()) {
var k = _d.value;
_loop_1(k);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_1) throw e_1.error; }
}
};
PromisesComponent.prototype.unsubscribe = function () {
this.prevPromises = undefined;
};
PromisesComponent.prototype.render = function () {
var children = this.props.children;
var _a = this.state, error = _a.error, values = _a.values;
if (error !== null) {
throw error;
}
return children(values);
};
return PromisesComponent;
}(PureComponent));
export default PromisesComponent;