|
1 | | -const {generateKeywordKey} = require('./../utility/db_required/keyGenerator'); |
| 1 | +const { generateKeywordKey } = require('./../utility/db_required/keyGenerator'); |
2 | 2 |
|
| 3 | +// In-memory store of keyword timestamps, keyed by a canonical compound key |
| 4 | +// (<KEYWORD>|<fileName>|<line>). Loaded from VS Code globalState on startup. |
3 | 5 | let highlightTimeStamps = new Map(); |
4 | 6 |
|
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// Lifecycle |
| 9 | +// --------------------------------------------------------------------------- |
5 | 10 |
|
| 11 | +/** |
| 12 | + * Initialises the in-memory timestamp store from VS Code's persisted |
| 13 | + * globalState. Must be called once during extension activation before any |
| 14 | + * other DB functions are used. |
| 15 | + * @param {import('vscode').ExtensionContext} context |
| 16 | + */ |
6 | 17 | async function initDB(context) { |
7 | 18 | try { |
8 | 19 | const data = await context.globalState.get("highlightTimeStamps", {}); |
9 | 20 | highlightTimeStamps.clear(); |
10 | | - |
11 | 21 | for (const [key, value] of Object.entries(data)) { |
12 | | - highlightTimeStamps.set(key, value); // ✅ Already a fully qualified key |
| 22 | + highlightTimeStamps.set(key, value); |
13 | 23 | } |
14 | | - |
15 | | - console.log("Timestamps successfully loaded from global-state."); |
16 | 24 | } catch (err) { |
17 | | - console.error({ |
18 | | - errorMessage: "Failed to initialize the DB.", |
19 | | - err, |
20 | | - }); |
| 25 | + console.error({ errorMessage: "Failed to initialize the DB.", err }); |
21 | 26 | } |
22 | 27 | } |
23 | 28 |
|
| 29 | +/** |
| 30 | + * Reloads all timestamps from globalState into the in-memory store. |
| 31 | + * Useful when the store may be out of sync with the persisted data. |
| 32 | + * @param {import('vscode').ExtensionContext} context |
| 33 | + */ |
24 | 34 | async function loadAllTimestampsToMemory(context) { |
25 | 35 | const stored = (await context.globalState.get("highlightTimeStamps")) || {}; |
26 | | - highlightTimeStamps.clear(); // Ensure clean slate |
| 36 | + highlightTimeStamps.clear(); |
27 | 37 | for (const [key, value] of Object.entries(stored)) { |
28 | 38 | highlightTimeStamps.set(key, value); |
29 | 39 | } |
30 | 40 | } |
31 | 41 |
|
| 42 | +// --------------------------------------------------------------------------- |
| 43 | +// Persistence helper |
| 44 | +// --------------------------------------------------------------------------- |
| 45 | + |
| 46 | +/** |
| 47 | + * Writes the current in-memory timestamp map to VS Code's globalState. |
| 48 | + * @param {import('vscode').ExtensionContext} context |
| 49 | + */ |
32 | 50 | async function persist(context) { |
33 | 51 | try { |
34 | 52 | const data = Object.fromEntries(highlightTimeStamps); |
35 | | - console.log("Persisting Timestamps: ", data); |
36 | 53 | await context.globalState.update("highlightTimeStamps", data); |
37 | 54 | } catch (err) { |
38 | | - console.error({ |
39 | | - errorMessage: "Failed to persist() timeStamp.", |
40 | | - err, |
41 | | - }); |
| 55 | + console.error({ errorMessage: "Failed to persist() timeStamp.", err }); |
42 | 56 | } |
43 | 57 | } |
44 | 58 |
|
45 | | - |
46 | | -async function saveTimestamp(keyword, fileName=null, line=null, context) { |
| 59 | +// --------------------------------------------------------------------------- |
| 60 | +// CRUD operations |
| 61 | +// --------------------------------------------------------------------------- |
| 62 | + |
| 63 | +/** |
| 64 | + * Saves a timestamp for the given keyword location if one does not already |
| 65 | + * exist. Existing timestamps are intentionally preserved so that the original |
| 66 | + * discovery time is never overwritten. |
| 67 | + * @param {string} keyword Uppercase keyword with trailing colon (e.g. "TODO:"). |
| 68 | + * @param {string|null} fileName Source file name or path. |
| 69 | + * @param {number|null} line 1-based line number. |
| 70 | + * @param {import('vscode').ExtensionContext} context |
| 71 | + */ |
| 72 | +async function saveTimestamp(keyword, fileName = null, line = null, context) { |
47 | 73 | try { |
48 | 74 | const key = generateKeywordKey(keyword, fileName, line); |
49 | 75 | if (!highlightTimeStamps.has(key)) { |
50 | | - const currentTime = Date.now(); |
51 | | - highlightTimeStamps.set(key, currentTime); |
52 | | - console.log("Saved timestamp for:", key); |
| 76 | + highlightTimeStamps.set(key, Date.now()); |
53 | 77 | await persist(context); |
54 | | - } else { |
55 | | - console.log("⏱️ Existing timestamp preserved for:", key); |
56 | 78 | } |
57 | 79 | } catch (err) { |
58 | | - console.error({ |
59 | | - errorMessage: "Failed to save() timestamp.", |
60 | | - err, |
61 | | - }); |
| 80 | + console.error({ errorMessage: "Failed to save() timestamp.", err }); |
62 | 81 | } |
63 | 82 | } |
64 | 83 |
|
| 84 | +/** |
| 85 | + * Deletes the timestamp for the given keyword location. |
| 86 | + * @param {string} keyword |
| 87 | + * @param {string} filePath |
| 88 | + * @param {number} line |
| 89 | + * @param {import('vscode').ExtensionContext} context |
| 90 | + */ |
65 | 91 | async function deleteTimestamp(keyword, filePath, line, context) { |
66 | 92 | try { |
67 | | - const key = key(keyword, filePath, line); |
| 93 | + const key = generateKeywordKey(keyword, filePath, line); |
68 | 94 | if (highlightTimeStamps.has(key)) { |
69 | 95 | highlightTimeStamps.delete(key); |
70 | | - console.log("Deleted timestamp for:", key); |
71 | 96 | await persist(context); |
72 | 97 | } |
73 | 98 | } catch (err) { |
74 | | - console.error({ |
75 | | - errorMessage: "Failed to Delete() timestamp.", |
76 | | - err, |
77 | | - }); |
| 99 | + console.error({ errorMessage: "Failed to Delete() timestamp.", err }); |
78 | 100 | } |
79 | 101 | } |
80 | 102 |
|
| 103 | +/** |
| 104 | + * Returns the stored timestamp (ms since epoch) for the given keyword location, |
| 105 | + * or `undefined` if not found. |
| 106 | + * @param {string} keyword |
| 107 | + * @param {string} fileName |
| 108 | + * @param {number} line |
| 109 | + * @returns {number|undefined} |
| 110 | + */ |
81 | 111 | function getTimestamp(keyword, fileName, line) { |
82 | 112 | return highlightTimeStamps.get(generateKeywordKey(keyword, fileName, line)); |
83 | 113 | } |
84 | 114 |
|
| 115 | +/** |
| 116 | + * Returns the full in-memory timestamp map (read-only by convention). |
| 117 | + * @returns {Map<string, number>} |
| 118 | + */ |
85 | 119 | function getAllTimestamps() { |
86 | 120 | return highlightTimeStamps; |
87 | 121 | } |
88 | 122 |
|
89 | | - |
90 | | - |
91 | 123 | module.exports = { |
92 | 124 | initDB, |
93 | 125 | saveTimestamp, |
94 | 126 | deleteTimestamp, |
95 | 127 | getTimestamp, |
96 | 128 | getAllTimestamps, |
97 | 129 | highlightTimeStamps, |
98 | | - loadAllTimestampsToMemory |
| 130 | + loadAllTimestampsToMemory, |
99 | 131 | }; |
100 | | - |
101 | | -// async function initDB(context, highlightTimeStamps) { |
102 | | -// try { |
103 | | -// const data = context.globalState.get("highlightTimeStamps", {}); |
104 | | -// highlightTimeStamps.clear(); |
105 | | -// Object.entries(data).forEach(([key, value]) => |
106 | | -// highlightTimeStamps.set(key, value) |
107 | | -// ); |
108 | | -// console.log({ |
109 | | -// context, |
110 | | -// highlightTimeStamps |
111 | | -// }) |
112 | | -// console.log("Timestamps successfully loaded from global-state."); |
113 | | -// } catch (err) { |
114 | | -// console.error({ |
115 | | -// ERRORMESSAGE: "Failed to load timestamps from global-state!", |
116 | | -// err, |
117 | | -// }); |
118 | | -// } |
119 | | -// } |
120 | | - |
121 | | -// async function saveTimestamp(keyword, highlightTimeStamps, context) { |
122 | | -// const currentTime = Date.now(); |
123 | | -// try { |
124 | | -// const existingTime = highlightTimeStamps.get(keyword); |
125 | | -// if (existingTime && existingTime === currentTime) return; |
126 | | - |
127 | | -// console.log("Saved timestamp for:", keyword); |
128 | | -// highlightTimeStamps.set(keyword, currentTime); |
129 | | -// await persist(context, highlightTimeStamps); |
130 | | -// } catch (err) { |
131 | | -// console.error({ |
132 | | -// ERRORMESSAGE: "Failed to save timestamp to global-state!", |
133 | | -// err, |
134 | | -// }); |
135 | | -// } |
136 | | -// } |
137 | | - |
138 | | -// async function deleteTimestamp(keyword, highlightTimeStamps, context) { |
139 | | -// try { |
140 | | -// console.log(`Successfully deleted timestamp for ${keyword}.`); |
141 | | -// highlightTimeStamps.delete(keyword); |
142 | | -// await persist(context, highlightTimeStamps); |
143 | | -// } catch (err) { |
144 | | -// console.error({ |
145 | | -// ERRORMESSAGE: "Failed to delete timestamp from global-state!", |
146 | | -// err, |
147 | | -// }); |
148 | | -// } |
149 | | -// } |
150 | | - |
151 | | -// async function loadTimestampsFromDB(context) { |
152 | | -// const globalState = context.globalState; |
153 | | -// const saved = globalState.get("highlightTimeStamps"); |
154 | | - |
155 | | -// if (saved) { |
156 | | -// for (const [key, value] of Object.entries(saved)) { |
157 | | -// highlightTimeStamps.set(key, value); |
158 | | -// } |
159 | | -// } else { |
160 | | -// console.log("No saved timestamps found in globalState."); |
161 | | -// } |
162 | | -// } |
163 | | - |
164 | | -// async function persist(context, highlighttimestamps) { |
165 | | -// try { |
166 | | -// // if (!context || !context.globalState || !context.globalState.update) { |
167 | | -// // throw new Error("❌ Missing or invalid extension context or globalState!"); |
168 | | -// // } |
169 | | -// console.log("🧪 Context during persist():", { |
170 | | -// CONTEXTEXISTS: !!context, |
171 | | -// GLOBALSTATEEXISTS: !!context?.globalState, |
172 | | -// UPDATEEXISTS: typeof context?.globalState?.update === "function", |
173 | | -// }); |
174 | | -// const data = Object.fromEntries(highlighttimestamps); |
175 | | -// await context.globalState.update("highlightTimeStamps", data); |
176 | | -// } catch (err) { |
177 | | -// console.error({ |
178 | | -// ERRORMESSAGE: "Failed to save to global-state!", |
179 | | -// err, |
180 | | -// }); |
181 | | -// } |
182 | | -// } |
183 | | - |
184 | | -// module.exports = { |
185 | | -// initDB, |
186 | | -// saveTimestamp, |
187 | | -// deleteTimestamp, |
188 | | -// loadTimestampsFromDB, |
189 | | -// }; |
0 commit comments