Skip to content

Commit 3f18fbc

Browse files
committed
introduce branded type for TypingsArray
1 parent dbd5249 commit 3f18fbc

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace ts {
8787
return node.end - node.pos;
8888
}
8989

90-
export function arrayIsEqualTo<T>(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean): boolean {
90+
export function arrayIsEqualTo<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, equaler?: (a: T, b: T) => boolean): boolean {
9191
if (!array1 || !array2) {
9292
return array1 === array2;
9393
}

src/server/editorServices.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ namespace ts.server {
214214
switch (response.kind) {
215215
case "set":
216216
this.typingsCache.updateTypingsForProject(response.projectName, response.compilerOptions, response.typingOptions, response.typings);
217-
project.setTypings(response.typings);
218217
project.updateGraph();
219218
break;
220219
case "invalidate":

src/server/project.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace ts.server {
4747
*/
4848
private projectStateVersion = 0;
4949

50-
private typingFiles: string[];
50+
private typingFiles: TypingsArray;
5151

5252
constructor(
5353
readonly projectKind: ProjectKind,
@@ -226,18 +226,19 @@ namespace ts.server {
226226
if (!this.languageServiceEnabled) {
227227
return true;
228228
}
229-
const hasChanges = this.updateGraphWorker();
229+
let hasChanges = this.updateGraphWorker();
230+
const cachedTypings = this.projectService.typingsCache.getTypingsForProject(this);
231+
if (this.setTypings(cachedTypings)) {
232+
hasChanges = this.updateGraphWorker() || hasChanges;
233+
}
230234
if (hasChanges) {
231-
if (this.setTypings(this.projectService.typingsCache.getTypingsForProject(this))) {
232-
this.updateGraphWorker();
233-
}
234235
this.projectStructureVersion++;
235236
}
236237
return !hasChanges;
237238
}
238239

239-
setTypings(typings: string[]): boolean {
240-
if (typings === this.typingFiles) {
240+
private setTypings(typings: TypingsArray): boolean {
241+
if (arrayIsEqualTo(this.typingFiles, typings)) {
241242
return false;
242243
}
243244
this.typingFiles = typings;

src/server/typingsCache.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts.server {
1616
class TypingsCacheEntry {
1717
readonly typingOptions: TypingOptions;
1818
readonly compilerOptions: CompilerOptions;
19-
readonly typings: string[];
19+
readonly typings: TypingsArray;
2020
}
2121

2222
const emptyArray: any[] = [];
@@ -43,9 +43,7 @@ namespace ts.server {
4343
if ((arr1 || emptyArray).length === 0 && (arr2 || emptyArray).length === 0) {
4444
return true;
4545
}
46-
/* tslint:disable:no-null-keyword */
47-
const set: Map<boolean> = Object.create(null);
48-
/* tslint:enable:no-null-keyword */
46+
const set: Map<boolean> = createMap<boolean>();
4947
let unique = 0;
5048

5149
for (const v of arr1) {
@@ -77,24 +75,33 @@ namespace ts.server {
7775
return opt1.allowJs != opt2.allowJs;
7876
}
7977

78+
export interface TypingsArray extends ReadonlyArray<string> {
79+
" __typingsArrayBrand": any;
80+
}
81+
82+
function toTypingsArray(arr: string[]): TypingsArray {
83+
arr.sort();
84+
return <any>arr;
85+
}
86+
8087
export class TypingsCache {
8188
private readonly perProjectCache: Map<TypingsCacheEntry> = createMap<TypingsCacheEntry>();
8289

8390
constructor(private readonly installer: ITypingsInstaller) {
8491
}
8592

86-
getTypingsForProject(project: Project): Path[] {
93+
getTypingsForProject(project: Project): TypingsArray {
8794
const typingOptions = getTypingOptionsForProjects(project);
8895

8996
if (!typingOptions.enableAutoDiscovery) {
90-
return emptyArray;
97+
return <any>emptyArray;
9198
}
9299

93100
const entry = this.perProjectCache[project.getProjectName()];
94101
if (!entry || typingOptionsChanged(typingOptions, entry.typingOptions) || compilerOptionsChanged(project.getCompilerOptions(), entry.compilerOptions)) {
95102
this.installer.enqueueInstallTypingsRequest(project, typingOptions);
96103
}
97-
return entry ? entry.typings : emptyArray;
104+
return entry ? entry.typings : <any>emptyArray;
98105
}
99106

100107
invalidateCachedTypingsForProject(project: Project) {
@@ -109,7 +116,7 @@ namespace ts.server {
109116
this.perProjectCache[projectName] = {
110117
compilerOptions,
111118
typingOptions,
112-
typings: newTypings
119+
typings: toTypingsArray(newTypings)
113120
};
114121
}
115122

0 commit comments

Comments
 (0)