metaf-react
Version:
Resolvable component and injection root for adding DI/IoC to React App
90 lines • 3.68 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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApplicationRoot = exports.config = void 0;
var react_1 = require("react");
var Resolver_1 = require("./Resolver");
/**
* @internal
*/
exports.config = {
shouldThrowForSeveralRoots: true,
};
var applicationRootWasAdded = false;
/**
* Component that is used for declaring overrides for `dependencies` and `requirements`.
* Also `requirements` HOCs applied to `render` method of this component in order to
* wrap you application tree for providing proper environment for your `ResolvableComponent`s
* @description ApplicationRoot Component
* > **NOTE:** `<ApplicationRoot>` is meant to appear only once in your app
* and in majority of cases it should be the first and only wrapper around
* your `App` component. Adding other `ApplicationRoot`s somewhere in your app
* won't affect dependency resolution mechanism but instead will throw an error
*/
var ApplicationRoot = /** @class */ (function (_super) {
__extends(ApplicationRoot, _super);
/**
* Creates an instance of application root.
* @param props contains overrides for `dependencies` and `requirements`
*/
function ApplicationRoot(props) {
var _this = _super.call(this, props) || this;
/**
* @internal
*/
_this.resolver = Resolver_1.resolver;
/**
* @internal
*/
_this.wrapInHocs = function () {
var children = _this.props.children;
var hocs = _this.state.hocs;
return hocs.reduce(function (result, hoc) { return hoc(result); }, children);
};
/**
* @internal
*/
_this.requirementsUpdate = function (requirements) {
_this.setState({ hocs: requirements });
};
if (exports.config.shouldThrowForSeveralRoots && applicationRootWasAdded) {
throw new Error("It's restricted to add more than one <ApplicationRoot>");
}
else {
applicationRootWasAdded = true;
}
_this.resolver.setOverrides(props.dependencies);
_this.resolver.setRequirementsOverrides(props.requirements);
var hocs = _this.resolver.subscribeForRequirements(_this.requirementsUpdate);
_this.state = { hocs: hocs };
return _this;
}
ApplicationRoot.prototype.render = function () {
return this.wrapInHocs();
};
/**
* by default there are no predefined `dependencies` or `requirements`
* @description default props of `ApplicationRoot` component
*/
ApplicationRoot.defaultProps = {
dependencies: [],
requirements: [],
};
return ApplicationRoot;
}(react_1.Component));
exports.ApplicationRoot = ApplicationRoot;
//# sourceMappingURL=ApplicationRoot.js.map