UNPKG

react-obsidian

Version:

Dependency injection framework for React and React Native applications

147 lines 6.25 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphRegistry = void 0; const GraphMiddlewareChain_1 = __importDefault(require("./GraphMiddlewareChain")); const ObtainLifecycleBoundGraphException_1 = require("./ObtainLifecycleBoundGraphException"); const getGlobal_1 = require("../../utils/getGlobal"); class GraphRegistry { constructor() { this.constructorToInstance = new Map(); this.instanceToConstructor = new Map(); this.injectionTokenToInstance = new Map(); this.instanceToInjectionToken = new Map(); this.nameToInstance = new Map(); this.graphToSubgraphs = new Map(); this.graphMiddlewares = new GraphMiddlewareChain_1.default(); } register(constructor, subgraphs = []) { this.graphToSubgraphs.set(constructor, new Set(subgraphs)); } ensureRegistered(graph) { if (this.instanceToConstructor.get(graph)) return; this.set(graph.constructor, graph); } getSubgraphs(graph) { var _a; const Graph = this.instanceToConstructor.get(graph); const subgraphs = (_a = this.graphToSubgraphs.get(Graph)) !== null && _a !== void 0 ? _a : new Set(); return Array.from(subgraphs).map((G) => this.resolve(G)); } getGraphInstance(name) { return this.nameToInstance.get(name); } resolve(Graph, source = 'lifecycleOwner', props = undefined, injectionToken) { if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph, injectionToken)) { return this.isComponentScopedLifecycleBound(Graph) ? this.getByInjectionToken(Graph, injectionToken) : this.getFirst(Graph); } if (this.isBoundToReactLifecycle(Graph) && source !== 'lifecycleOwner') { throw new ObtainLifecycleBoundGraphException_1.ObtainLifecycleBoundGraphException(Graph); } const graph = this.graphMiddlewares.resolve(Graph, props); this.set(Graph, graph, injectionToken); return graph; } has(Graph, injectionToken) { var _a, _b; const instances = this.constructorToInstance.get(Graph); if (!instances) return false; if (this.isComponentScopedLifecycleBound(Graph)) { return Array .from(instances) .some((graph) => this.instanceToInjectionToken.get(graph) === injectionToken); } return ((_b = (_a = this.constructorToInstance.get(Graph)) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : 0) > 0; } getFirst(Graph) { return this.constructorToInstance.get(Graph).values().next().value; } getByInjectionToken(Graph, injectionToken) { return Array .from(this.constructorToInstance.get(Graph)) .find((graph) => { return this.instanceToInjectionToken.get(graph) === injectionToken; }); } set(Graph, graph, injectionToken) { var _a; const graphs = (_a = this.constructorToInstance.get(Graph)) !== null && _a !== void 0 ? _a : new Set(); if (injectionToken && this.isComponentScopedLifecycleBound(Graph)) { this.injectionTokenToInstance.set(injectionToken, graph); this.instanceToInjectionToken.set(graph, injectionToken); } graphs.add(graph); this.constructorToInstance.set(Graph, graphs); this.instanceToConstructor.set(graph, Graph); this.nameToInstance.set(graph.name, graph); } isSingleton(Graph) { var _a; return (_a = Reflect.getMetadata('isSingleton', Graph)) !== null && _a !== void 0 ? _a : false; } isBoundToReactLifecycle(Graph) { var _a; return (_a = Reflect.getMetadata('isLifecycleBound', Graph)) !== null && _a !== void 0 ? _a : false; } isComponentScopedLifecycleBound(Graph) { return Reflect.getMetadata('lifecycleScope', Graph) === 'component'; } clearGraphAfterItWasMockedInTests(graphName) { const graphNames = this.nameToInstance.keys(); for (const name of graphNames) { if (name.match(graphName)) { const graph = this.nameToInstance.get(name); if (!graph) return; const Graph = this.instanceToConstructor.get(graph); if (!Graph) return; this.instanceToConstructor.delete(graph); this.constructorToInstance.get(Graph).delete(graph); this.nameToInstance.delete(graph.name); const token = this.instanceToInjectionToken.get(graph); if (token) { this.injectionTokenToInstance.delete(token); this.instanceToInjectionToken.delete(graph); } } } } clear(graph) { const Graph = this.instanceToConstructor.get(graph); if (!Graph || this.isSingleton(Graph)) return; this.instanceToConstructor.delete(graph); this.constructorToInstance.get(Graph).delete(graph); this.nameToInstance.delete(graph.name); const token = this.instanceToInjectionToken.get(graph); if (token) { this.injectionTokenToInstance.delete(token); this.instanceToInjectionToken.delete(graph); } } addGraphMiddleware(middleware) { this.graphMiddlewares.add(middleware); } clearGraphMiddlewares() { this.graphMiddlewares.clear(); } clearAll() { this.instanceToConstructor.clear(); this.constructorToInstance.clear(); this.nameToInstance.clear(); this.injectionTokenToInstance.clear(); this.instanceToInjectionToken.clear(); } } exports.GraphRegistry = GraphRegistry; const globalObject = (0, getGlobal_1.getGlobal)(); globalObject.graphRegistry = globalObject.graphRegistry || new GraphRegistry(); exports.default = globalObject.graphRegistry; //# sourceMappingURL=GraphRegistry.js.map