1 | "use strict";
|
2 |
|
3 |
|
4 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
5 | if (k2 === undefined) k2 = k;
|
6 | var desc = Object.getOwnPropertyDescriptor(m, k);
|
7 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
8 | desc = { enumerable: true, get: function() { return m[k]; } };
|
9 | }
|
10 | Object.defineProperty(o, k2, desc);
|
11 | }) : (function(o, m, k, k2) {
|
12 | if (k2 === undefined) k2 = k;
|
13 | o[k2] = m[k];
|
14 | }));
|
15 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
16 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
17 | }) : function(o, v) {
|
18 | o["default"] = v;
|
19 | });
|
20 | var __importStar = (this && this.__importStar) || function (mod) {
|
21 | if (mod && mod.__esModule) return mod;
|
22 | var result = {};
|
23 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
24 | __setModuleDefault(result, mod);
|
25 | return result;
|
26 | };
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | exports.ConfigCache = void 0;
|
29 | const tsdoc_config_1 = require("@microsoft/tsdoc-config");
|
30 | const path = __importStar(require("path"));
|
31 | const Debug_1 = require("./Debug");
|
32 |
|
33 |
|
34 | const CACHE_CHECK_INTERVAL_MS = 3 * 1000;
|
35 |
|
36 |
|
37 | const CACHE_EXPIRE_MS = 20 * 1000;
|
38 |
|
39 | const CACHE_MAX_SIZE = 100;
|
40 | class ConfigCache {
|
41 | |
42 |
|
43 |
|
44 | static _getTimeInMs() {
|
45 | const [seconds, nanoseconds] = process.hrtime();
|
46 | return seconds * 1000 + nanoseconds / 1000000;
|
47 | }
|
48 | static getForSourceFile(sourceFilePath) {
|
49 | const sourceFileFolder = path.dirname(path.resolve(sourceFilePath));
|
50 |
|
51 | const configFilePath = tsdoc_config_1.TSDocConfigFile.findConfigPathForFolder(sourceFileFolder);
|
52 |
|
53 |
|
54 | const cacheKey = configFilePath || sourceFileFolder + '/';
|
55 | Debug_1.Debug.log(`Cache key: "${cacheKey}"`);
|
56 | const nowMs = ConfigCache._getTimeInMs();
|
57 | let cachedConfig = undefined;
|
58 |
|
59 | cachedConfig = ConfigCache._cachedConfigs.get(cacheKey);
|
60 | if (cachedConfig) {
|
61 | Debug_1.Debug.log('Cache hit');
|
62 |
|
63 | const loadAgeMs = nowMs - cachedConfig.loadTimeMs;
|
64 | const lastCheckAgeMs = nowMs - cachedConfig.lastCheckTimeMs;
|
65 | if (loadAgeMs > CACHE_EXPIRE_MS || loadAgeMs < 0) {
|
66 | Debug_1.Debug.log('Evicting because item is expired');
|
67 | cachedConfig = undefined;
|
68 | ConfigCache._cachedConfigs.delete(cacheKey);
|
69 | }
|
70 | else if (lastCheckAgeMs > CACHE_CHECK_INTERVAL_MS || lastCheckAgeMs < 0) {
|
71 | Debug_1.Debug.log('Checking for modifications');
|
72 | cachedConfig.lastCheckTimeMs = nowMs;
|
73 | if (cachedConfig.configFile.checkForModifiedFiles()) {
|
74 |
|
75 | Debug_1.Debug.log('Evicting because item was modified');
|
76 | cachedConfig = undefined;
|
77 | ConfigCache._cachedConfigs.delete(cacheKey);
|
78 | }
|
79 | }
|
80 | }
|
81 |
|
82 | if (!cachedConfig) {
|
83 | if (ConfigCache._cachedConfigs.size > CACHE_MAX_SIZE) {
|
84 | Debug_1.Debug.log('Clearing cache');
|
85 | ConfigCache._cachedConfigs.clear();
|
86 | }
|
87 | const configFile = tsdoc_config_1.TSDocConfigFile.loadFile(configFilePath);
|
88 | if (configFile.fileNotFound) {
|
89 | Debug_1.Debug.log(`File not found: "${configFilePath}"`);
|
90 | }
|
91 | else {
|
92 | Debug_1.Debug.log(`Loaded: "${configFilePath}"`);
|
93 | }
|
94 | cachedConfig = {
|
95 | configFile,
|
96 | lastCheckTimeMs: nowMs,
|
97 | loadTimeMs: nowMs
|
98 | };
|
99 | ConfigCache._cachedConfigs.set(cacheKey, cachedConfig);
|
100 | }
|
101 | return cachedConfig.configFile;
|
102 | }
|
103 | }
|
104 | exports.ConfigCache = ConfigCache;
|
105 |
|
106 | ConfigCache._cachedConfigs = new Map();
|
107 |
|
\ | No newline at end of file |