triviality-react
Version:
Exposes triviality service container to react components
68 lines • 2.92 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 (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
exports.ServiceContainerContext = React.createContext(null);
/**
* This ServiceContainerProvider can be nested with each other, passes to level container to all children.
*/
var ServiceContainerProvider = /** @class */ (function (_super) {
__extends(ServiceContainerProvider, _super);
function ServiceContainerProvider() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.state = {
error: null,
};
_this.handleConsumeCallback = function (value) {
var _a = _this.props, children = _a.children, factory = _a.factory;
if (value && value.serviceContainer) {
// We are a nested provider, so we can just return directly and let the parent provider handle the rest.
return children;
}
factory
.then(function (serviceContainer) {
_this.setState({ serviceContainer: serviceContainer });
})
.catch(function (error) {
_this.setState({
error: error,
});
});
return null;
};
return _this;
}
ServiceContainerProvider.prototype.render = function () {
var children = this.props.children;
var _a = this.state, serviceContainer = _a.serviceContainer, error = _a.error;
if (error) {
throw error;
}
if (serviceContainer) {
return (React.createElement(exports.ServiceContainerContext.Provider, { value: { serviceContainer: serviceContainer } }, children));
}
return (React.createElement(exports.ServiceContainerContext.Consumer, null, this.handleConsumeCallback));
};
return ServiceContainerProvider;
}(React.PureComponent));
exports.ServiceContainerProvider = ServiceContainerProvider;
//# sourceMappingURL=ServiceContainerProvider.js.map