|
| 1 | +import { difference, differenceWith, intersection, isEqual } from 'lodash'; |
1 | 2 | import { join, normalize, relative, resolve } from 'path'; |
2 | 3 |
|
3 | 4 | import { DeclarationInfo } from './declarations/DeclarationInfo'; |
@@ -34,10 +35,16 @@ function getNodeLibraryName(path: string): string { |
34 | 35 | */ |
35 | 36 | type Resources = { [name: string]: Resource }; |
36 | 37 |
|
37 | | -// export type DeltaIndex = { |
38 | | -// deleted: string[]; |
39 | | -// updated: { [declaration: string]: DeclarationInfo[] }; |
40 | | -// }; |
| 38 | +/** |
| 39 | + * IndexDelta type, is calculated by the declaration index to give an overview, what has changed in the index. |
| 40 | + * Returns a list of deleted declarations, newly added declarations (with the corresponding infos) and |
| 41 | + * which declarations have been updated (with all declarations under that name). |
| 42 | + */ |
| 43 | +export type IndexDelta = { |
| 44 | + added: { [declaration: string]: DeclarationInfo[] }; |
| 45 | + updated: { [declaration: string]: DeclarationInfo[] }; |
| 46 | + deleted: string[]; |
| 47 | +}; |
41 | 48 |
|
42 | 49 | /** |
43 | 50 | * Interface for file changes. Contains lists of file uri's to the specific action. |
@@ -119,6 +126,48 @@ export class DeclarationIndex { |
119 | 126 |
|
120 | 127 | constructor(private parser: TypescriptParser, private rootPath: string) { } |
121 | 128 |
|
| 129 | + /** |
| 130 | + * Calculates the differences between two indices. Calculates removed, added and updated declarations. |
| 131 | + * The updated declarations are calculated and all declarations that the new index contains are inserted in the list. |
| 132 | + * |
| 133 | + * @static |
| 134 | + * @param {{ [declaration: string]: DeclarationInfo[] }} oldIndex |
| 135 | + * @param {{ [declaration: string]: DeclarationInfo[] }} newIndex |
| 136 | + * @returns {IndexDelta} |
| 137 | + * @memberof DeclarationIndex |
| 138 | + */ |
| 139 | + public static calculateIndexDelta( |
| 140 | + oldIndex: { [declaration: string]: DeclarationInfo[] }, |
| 141 | + newIndex: { [declaration: string]: DeclarationInfo[] }, |
| 142 | + ): IndexDelta { |
| 143 | + const oldKeys = Object.keys(oldIndex); |
| 144 | + const newKeys = Object.keys(newIndex); |
| 145 | + |
| 146 | + return { |
| 147 | + added: difference(newKeys, oldKeys).reduce( |
| 148 | + (obj, currentKey) => { |
| 149 | + obj[currentKey] = newIndex[currentKey]; |
| 150 | + return obj; |
| 151 | + }, |
| 152 | + {} as { [declaration: string]: DeclarationInfo[] }, |
| 153 | + ), |
| 154 | + updated: intersection(oldKeys, newKeys).reduce( |
| 155 | + (obj, currentKey) => { |
| 156 | + const old = oldIndex[currentKey]; |
| 157 | + const neu = newIndex[currentKey]; |
| 158 | + |
| 159 | + if (differenceWith(neu, old, isEqual).length > 0 || differenceWith(old, neu, isEqual).length > 0) { |
| 160 | + obj[currentKey] = neu; |
| 161 | + } |
| 162 | + |
| 163 | + return obj; |
| 164 | + }, |
| 165 | + {} as { [declaration: string]: DeclarationInfo[] }, |
| 166 | + ), |
| 167 | + deleted: difference(oldKeys, newKeys), |
| 168 | + }; |
| 169 | + } |
| 170 | + |
122 | 171 | /** |
123 | 172 | * Resets the whole index. Does delete everything. Period. |
124 | 173 | * Is useful for unit testing or similar things. |
@@ -159,13 +208,14 @@ export class DeclarationIndex { |
159 | 208 |
|
160 | 209 | /** |
161 | 210 | * Is called when file events happen. Does reindex for the changed files and creates a new index. |
| 211 | + * Returns the differences for the new index. |
162 | 212 | * |
163 | 213 | * @param {FileEvent[]} changes |
164 | | - * @returns {Promise<void>} |
| 214 | + * @returns {Promise<IndexDelta>} |
165 | 215 | * |
166 | 216 | * @memberof DeclarationIndex |
167 | 217 | */ |
168 | | - public async reindexForChanges(changes: FileChanges): Promise<void> { |
| 218 | + public async reindexForChanges(changes: FileChanges): Promise<IndexDelta> { |
169 | 219 | const rebuildResources: string[] = []; |
170 | 220 | const removeResources: string[] = []; |
171 | 221 | const rebuildFiles: string[] = []; |
@@ -212,11 +262,10 @@ export class DeclarationIndex { |
212 | 262 | for (const key of Object.keys(resources)) { |
213 | 263 | this.parsedResources[key] = resources[key]; |
214 | 264 | } |
| 265 | + const old = this._index || {}; |
215 | 266 | this._index = await this.createIndex(this.parsedResources); |
216 | | - // return { |
217 | | - // deleted: removeResources, |
218 | | - // updated: await this.createIndex(resources), |
219 | | - // }; |
| 267 | + |
| 268 | + return DeclarationIndex.calculateIndexDelta(old, this._index); |
220 | 269 | } |
221 | 270 |
|
222 | 271 | /** |
|
0 commit comments