1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.LazyMetadataStorage = exports.LazyMetadataStorageHost = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
6 | const NO_TARGET_METADATA = Symbol('NO_TARGET_METADATA');
|
7 | const FIELD_LAZY_METADATA = Symbol('FIELD_LAZY_METADATA');
|
8 | class LazyMetadataStorageHost {
|
9 | constructor() {
|
10 | this.lazyMetadataByTarget = new Map();
|
11 | }
|
12 | store(targetOrFn, func, options) {
|
13 | if (func && options?.isField) {
|
14 | this.updateStorage(FIELD_LAZY_METADATA, func);
|
15 | this.updateStorage(targetOrFn, func);
|
16 | }
|
17 | else if (func) {
|
18 | this.updateStorage(targetOrFn, func);
|
19 | }
|
20 | else {
|
21 | this.updateStorage(NO_TARGET_METADATA, targetOrFn);
|
22 | }
|
23 | }
|
24 | load(types = [], options = {
|
25 | skipFieldLazyMetadata: false,
|
26 | }) {
|
27 | types = this.concatPrototypes(types);
|
28 | let loadersToExecute = (0, common_1.flatten)(types
|
29 | .map((target) => this.lazyMetadataByTarget.get(target))
|
30 | .filter((metadata) => metadata));
|
31 | loadersToExecute = [
|
32 | loadersToExecute,
|
33 | this.lazyMetadataByTarget.get(NO_TARGET_METADATA) || [],
|
34 | ].flat();
|
35 | if (!options.skipFieldLazyMetadata) {
|
36 | loadersToExecute = [
|
37 | loadersToExecute,
|
38 | this.lazyMetadataByTarget.get(FIELD_LAZY_METADATA) || [],
|
39 | ].flat();
|
40 | }
|
41 | loadersToExecute.forEach((func) => func());
|
42 | }
|
43 | concatPrototypes(types) {
|
44 | const typesWithPrototypes = types
|
45 | .filter((type) => type && type.prototype)
|
46 | .map((type) => {
|
47 | const parentTypes = [];
|
48 | let parent = type;
|
49 | while (!(0, shared_utils_1.isUndefined)(parent.prototype)) {
|
50 | parent = Object.getPrototypeOf(parent);
|
51 | if (parent === Function.prototype) {
|
52 | break;
|
53 | }
|
54 | parentTypes.push(parent);
|
55 | }
|
56 | parentTypes.push(type);
|
57 | return parentTypes;
|
58 | });
|
59 | return (0, common_1.flatten)(typesWithPrototypes);
|
60 | }
|
61 | updateStorage(key, func) {
|
62 | const existingArray = this.lazyMetadataByTarget.get(key);
|
63 | let called = false;
|
64 | const singleCallFunctionWrapper = () => {
|
65 | if (called)
|
66 | return;
|
67 | func();
|
68 | called = true;
|
69 | };
|
70 | if (existingArray) {
|
71 | existingArray.push(singleCallFunctionWrapper);
|
72 | }
|
73 | else {
|
74 | this.lazyMetadataByTarget.set(key, [singleCallFunctionWrapper]);
|
75 | }
|
76 | }
|
77 | }
|
78 | exports.LazyMetadataStorageHost = LazyMetadataStorageHost;
|
79 | const globalRef = global;
|
80 | exports.LazyMetadataStorage = globalRef.GqlLazyMetadataStorageHost ||
|
81 | (globalRef.GqlLazyMetadataStorageHost = new LazyMetadataStorageHost());
|