UNPKG

infinity-forge

Version:
112 lines 4.74 kB
"use strict"; 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 __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorBoundary = void 0; var react_1 = require("react"); var ErrorBoundaryContext_1 = require("./ErrorBoundaryContext.js"); var initialState = { didCatch: false, error: null, }; var ErrorBoundary = /** @class */ (function (_super) { __extends(ErrorBoundary, _super); function ErrorBoundary(props) { var _this = _super.call(this, props) || this; _this.resetErrorBoundary = _this.resetErrorBoundary.bind(_this); _this.state = initialState; return _this; } ErrorBoundary.getDerivedStateFromError = function (error) { return { didCatch: true, error: error }; }; ErrorBoundary.prototype.resetErrorBoundary = function () { var _a, _b; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var error = this.state.error; if (error !== null) { (_b = (_a = this.props).onReset) === null || _b === void 0 ? void 0 : _b.call(_a, { args: args, reason: "imperative-api", }); this.setState(initialState); } }; ErrorBoundary.prototype.componentDidCatch = function (error, info) { var _a, _b; (_b = (_a = this.props).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, info); }; ErrorBoundary.prototype.componentDidUpdate = function (prevProps, prevState) { var _a, _b; var didCatch = this.state.didCatch; var resetKeys = this.props.resetKeys; // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array, // we'd end up resetting the error boundary immediately. // This would likely trigger a second error to be thrown. // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set. if (didCatch && prevState.error !== null && hasArrayChanged(prevProps.resetKeys, resetKeys)) { (_b = (_a = this.props).onReset) === null || _b === void 0 ? void 0 : _b.call(_a, { next: resetKeys, prev: prevProps.resetKeys, reason: "keys", }); this.setState(initialState); } }; ErrorBoundary.prototype.render = function () { var _a = this.props, children = _a.children, fallbackRender = _a.fallbackRender, FallbackComponent = _a.FallbackComponent, fallback = _a.fallback; var _b = this.state, didCatch = _b.didCatch, error = _b.error; var childToRender = children; if (didCatch) { var props = { error: error, resetErrorBoundary: this.resetErrorBoundary, }; if (typeof fallbackRender === "function") { childToRender = fallbackRender(props); } else if (FallbackComponent) { childToRender = (0, react_1.createElement)(FallbackComponent, props); } else if (fallback === null || (0, react_1.isValidElement)(fallback)) { childToRender = fallback; } else { throw error; } } return (0, react_1.createElement)(ErrorBoundaryContext_1.ErrorBoundaryContext.Provider, { value: { didCatch: didCatch, error: error, resetErrorBoundary: this.resetErrorBoundary, }, }, childToRender); }; return ErrorBoundary; }(react_1.Component)); exports.ErrorBoundary = ErrorBoundary; function hasArrayChanged(a, b) { if (a === void 0) { a = []; } if (b === void 0) { b = []; } return (a.length !== b.length || a.some(function (item, index) { return !Object.is(item, b[index]); })); } //# sourceMappingURL=ErrorBoundary.js.map