@vlsergey/react-promise
Version:
React wrappers for Promises
115 lines (114 loc) • 4.58 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);
};
import { PureComponent } from 'react';
import * as memoize from './memoize';
var PromiseComponent = /** @class */ (function (_super) {
__extends(PromiseComponent, _super);
function PromiseComponent(props) {
var _this = _super.call(this, props) || this;
_this.mounted = false;
_this.state = {
error: undefined,
completed: false,
value: undefined,
};
_this.resetValue = function () {
/* eslint react/no-direct-mutation-state: 0 */
if (_this.mounted) {
_this.setState({ error: null, completed: false, value: undefined });
}
else {
_this.state = __assign(__assign({}, _this.state), { error: null, completed: false, value: undefined });
}
};
_this.setValue = function (value) {
if (_this.mounted) {
_this.setState({ error: null, completed: true, value: value });
}
else {
/* eslint-disable-next-line react/no-direct-mutation-state */
_this.state = __assign(__assign({}, _this.state), { error: null, completed: true, value: value });
}
};
_this.subscribe();
return _this;
}
PromiseComponent.prototype.componentDidMount = function () {
this.mounted = true;
this.subscribe();
};
PromiseComponent.prototype.componentDidUpdate = function () {
this.subscribe();
};
PromiseComponent.prototype.componentWillUnmount = function () {
this.unsubscribe();
this.mounted = false;
};
PromiseComponent.prototype.subscribe = function () {
var _this = this;
var _a = this.props, cleanOnChange = _a.cleanOnChange, promise = _a.promise;
if (this.prevPromise !== promise) {
if (cleanOnChange) {
this.resetValue();
}
this.prevPromise = promise;
if (promise !== undefined && promise !== null) {
var cachedResult = memoize.find(promise);
if (cachedResult)
this.setValue(cachedResult);
promise.then(function (value) {
// cache promise result
memoize.set(promise, value);
if (_this.prevPromise === promise) {
_this.setValue(value);
}
})
.catch(function (error) {
if (_this.prevPromise === promise) {
_this.setState({ error: error, completed: true, value: undefined });
}
});
}
}
};
PromiseComponent.prototype.unsubscribe = function () {
this.prevPromise = undefined;
};
PromiseComponent.prototype.render = function () {
var _a = this.props, children = _a.children, fallback = _a.fallback;
var _b = this.state, error = _b.error, completed = _b.completed, value = _b.value;
if (!completed) {
return fallback || null;
}
if (error !== null) {
throw error;
}
return children(value);
};
return PromiseComponent;
}(PureComponent));
export default PromiseComponent;