power-di
Version:
A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.
79 lines • 3.46 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.createConsumerComponent = exports.BaseConsumerComponent = exports.PureComponent = exports.Component = void 0;
var React = require("react");
var context_1 = require("./context");
function injectInstance(instance, context) {
context.inject(instance, { autoRunPostConstruct: false });
// process postConstruct
var oriWillMount = instance.componentWillMount || instance.UNSAFE_componentWillMount;
instance.componentWillMount = function () {
context.runPostConstruct(instance);
oriWillMount && oriWillMount.bind(instance)();
};
delete instance.UNSAFE_componentWillMount;
// process preDe
var oriWillUnmount = instance.componentWillUnmount;
instance.componentWillUnmount = function () {
context.runPreDestroy(instance);
oriWillUnmount && oriWillUnmount.bind(instance)();
};
if (context.config.createInstanceHook) {
instance = context.config.createInstanceHook(instance, context);
}
return instance;
}
var Component = exports.Component = /** @class */ (function (_super) {
__extends(Component, _super);
function Component(props, context) {
var _this = _super.call(this, props, context) || this;
return injectInstance(_this, context);
}
Component.contextType = context_1.Context;
return Component;
}(React.Component));
var PureComponent = exports.PureComponent = /** @class */ (function (_super) {
__extends(PureComponent, _super);
function PureComponent(props, context) {
var _this = _super.call(this, props, context) || this;
return injectInstance(_this, context);
}
PureComponent.contextType = context_1.Context;
return PureComponent;
}(React.PureComponent));
var BaseConsumerComponent = /** @class */ (function (_super) {
__extends(BaseConsumerComponent, _super);
function BaseConsumerComponent(props, context) {
var _this = _super.call(this, props, context) || this;
return injectInstance(_this, props[context_1.ContextSymbol]);
}
return BaseConsumerComponent;
}(React.Component));
exports.BaseConsumerComponent = BaseConsumerComponent;
function createConsumerComponent(Comp) {
return /** @class */ (function (_super) {
__extends(ConsumerComponent, _super);
function ConsumerComponent(props, context) {
var _this = _super.call(this, props, context) || this;
return injectInstance(_this, props[context_1.ContextSymbol]);
}
return ConsumerComponent;
}(Comp));
}
exports.createConsumerComponent = createConsumerComponent;
//# sourceMappingURL=BaseComponent.js.map