react-obsidian
Version:
Dependency injection framework for React and React Native applications
37 lines • 1.72 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GRAPH_INSTANCE_NAME_KEY = void 0;
const lodash_1 = require("lodash");
const ObjectGraph_1 = require("../../graph/ObjectGraph");
const GraphRegistry_1 = __importDefault(require("../../graph/registry/GraphRegistry"));
const InjectionMetadata_1 = __importDefault(require("./InjectionMetadata"));
const reflect_1 = require("../../utils/reflect");
exports.GRAPH_INSTANCE_NAME_KEY = 'GRAPH_INSTANCE_NAME';
class LateInjector {
inject(target, keyOrGraph) {
if (keyOrGraph)
GraphRegistry_1.default.ensureRegistered(keyOrGraph);
const injectionMetadata = new InjectionMetadata_1.default();
const graph = this.getGraph(target, keyOrGraph);
injectionMetadata.getLatePropertiesToInject(target.constructor).forEach((key) => {
reflect_1.Reflect.set(target, key, graph.retrieve(key));
});
return target;
}
getGraph(target, keyOrGraph) {
if (keyOrGraph instanceof ObjectGraph_1.ObjectGraph)
return keyOrGraph;
if ((0, lodash_1.isString)(keyOrGraph))
return GraphRegistry_1.default.resolve(keyOrGraph, 'classInjection');
return this.getGraphInstance(target);
}
getGraphInstance(target) {
const graphInstanceName = reflect_1.Reflect.getMetadata(exports.GRAPH_INSTANCE_NAME_KEY, target.constructor);
return GraphRegistry_1.default.getGraphInstance(graphInstanceName);
}
}
exports.default = new LateInjector();
//# sourceMappingURL=LateInjector.js.map