UNPKG

4.63 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SerializedGraph = void 0;
4const application_config_1 = require("../application-config");
5const external_context_creator_1 = require("../helpers/external-context-creator");
6const http_adapter_host_1 = require("../helpers/http-adapter-host");
7const inquirer_constants_1 = require("../injector/inquirer/inquirer-constants");
8const lazy_module_loader_1 = require("../injector/lazy-module-loader/lazy-module-loader");
9const module_ref_1 = require("../injector/module-ref");
10const modules_container_1 = require("../injector/modules-container");
11const request_constants_1 = require("../router/request/request-constants");
12const reflector_service_1 = require("../services/reflector.service");
13const deterministic_uuid_registry_1 = require("./deterministic-uuid-registry");
14class SerializedGraph {
15 constructor() {
16 this.nodes = new Map();
17 this.edges = new Map();
18 this.entrypoints = new Map();
19 this.extras = {
20 orphanedEnhancers: [],
21 attachedEnhancers: [],
22 };
23 this._status = 'complete';
24 }
25 set status(status) {
26 this._status = status;
27 }
28 set metadata(metadata) {
29 this._metadata = metadata;
30 }
31 insertNode(nodeDefinition) {
32 if (nodeDefinition.metadata.type === 'provider' &&
33 SerializedGraph.INTERNAL_PROVIDERS.includes(nodeDefinition.metadata.token)) {
34 nodeDefinition.metadata = Object.assign(Object.assign({}, nodeDefinition.metadata), { internal: true });
35 }
36 if (this.nodes.has(nodeDefinition.id)) {
37 return this.nodes.get(nodeDefinition.id);
38 }
39 this.nodes.set(nodeDefinition.id, nodeDefinition);
40 return nodeDefinition;
41 }
42 insertEdge(edgeDefinition) {
43 var _a;
44 if (edgeDefinition.metadata.type === 'class-to-class' &&
45 (SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.sourceClassToken) ||
46 SerializedGraph.INTERNAL_PROVIDERS.includes(edgeDefinition.metadata.targetClassToken))) {
47 edgeDefinition.metadata = Object.assign(Object.assign({}, edgeDefinition.metadata), { internal: true });
48 }
49 const id = (_a = edgeDefinition.id) !== null && _a !== void 0 ? _a : this.generateUuidByEdgeDefinition(edgeDefinition);
50 const edge = Object.assign(Object.assign({}, edgeDefinition), { id });
51 this.edges.set(id, edge);
52 return edge;
53 }
54 insertEntrypoint(definition, parentId) {
55 if (this.entrypoints.has(parentId)) {
56 const existingCollection = this.entrypoints.get(parentId);
57 existingCollection.push(definition);
58 }
59 else {
60 this.entrypoints.set(parentId, [definition]);
61 }
62 }
63 insertOrphanedEnhancer(entry) {
64 this.extras.orphanedEnhancers.push(entry);
65 }
66 insertAttachedEnhancer(nodeId) {
67 this.extras.attachedEnhancers.push({
68 nodeId,
69 });
70 }
71 getNodeById(id) {
72 return this.nodes.get(id);
73 }
74 toJSON() {
75 const json = {
76 nodes: Object.fromEntries(this.nodes),
77 edges: Object.fromEntries(this.edges),
78 entrypoints: Object.fromEntries(this.entrypoints),
79 extras: this.extras,
80 };
81 if (this._status) {
82 json['status'] = this._status;
83 }
84 if (this._metadata) {
85 json['metadata'] = this._metadata;
86 }
87 return json;
88 }
89 toString() {
90 const replacer = (key, value) => {
91 var _a;
92 if (typeof value === 'symbol') {
93 return value.toString();
94 }
95 return typeof value === 'function' ? (_a = value.name) !== null && _a !== void 0 ? _a : 'Function' : value;
96 };
97 return JSON.stringify(this.toJSON(), replacer, 2);
98 }
99 generateUuidByEdgeDefinition(edgeDefinition) {
100 return deterministic_uuid_registry_1.DeterministicUuidRegistry.get(JSON.stringify(edgeDefinition));
101 }
102}
103SerializedGraph.INTERNAL_PROVIDERS = [
104 application_config_1.ApplicationConfig,
105 module_ref_1.ModuleRef,
106 http_adapter_host_1.HttpAdapterHost,
107 lazy_module_loader_1.LazyModuleLoader,
108 external_context_creator_1.ExternalContextCreator,
109 modules_container_1.ModulesContainer,
110 reflector_service_1.Reflector,
111 SerializedGraph,
112 http_adapter_host_1.HttpAdapterHost.name,
113 reflector_service_1.Reflector.name,
114 request_constants_1.REQUEST,
115 inquirer_constants_1.INQUIRER,
116];
117exports.SerializedGraph = SerializedGraph;