@vlsergey/react-promise
Version:
React wrappers for Promises
191 lines (190 loc) • 7.53 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("react");
var memoize = __importStar(require("./memoize"));
var shallowCompare_1 = __importDefault(require("./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 ((0, shallowCompare_1.default)(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;
}(react_1.PureComponent));
exports.default = PromisesComponent;