1
+ import { ICache } from "./interface/ICache" ;
2
+ import { ICachedContent , IFunctionDeclaration , IVariableAssignment , IEnumDeclaration , IPropDeclaration , IClassDeclaration , FunctionIndexer , IImportDeclaration , ClassIndexer , EnumIndexer , VariableIndexer , ISimpleLanguageService } from "../interface/ISimpleLanguageService" ;
3
+
4
+ export class Cache implements ICache {
5
+ private cache : Map < string , ICachedContent < { } > > = new Map ( ) ;
6
+
7
+ constructor ( private languageService : ISimpleLanguageService ) { }
8
+
9
+ public getCachedPropName ( fileName : string , className : string , propName : string ) : string {
10
+ return `prop.${ fileName } .${ className } .${ propName } ` ;
11
+ }
12
+
13
+ public getCachedVariableName ( fileName : string , variableName : string ) : string {
14
+ return `variable.${ fileName } .${ variableName } ` ;
15
+ }
16
+
17
+ public getCachedEnumName ( fileName : string , enumName : string ) : string {
18
+ return `enum.${ fileName } .${ enumName } ` ;
19
+ }
20
+
21
+ public getCachedClassName ( fileName : string , className : string ) : string {
22
+ return `class.${ fileName } .${ className } ` ;
23
+ }
24
+
25
+ public getCachedFunctionName ( fileName : string , className : string ) : string {
26
+ return `function.${ fileName } .${ className } ` ;
27
+ }
28
+
29
+ public getCachedClassIndexerName ( fileName : string ) : string {
30
+ return `classIndexer.${ fileName } ` ;
31
+ }
32
+
33
+ public getCachedModuleDependenciesName ( fileName : string ) : string {
34
+ return `moduleDependencies.${ fileName } ` ;
35
+ }
36
+
37
+ public getCachedFunctionIndexerName ( fileName : string ) : string {
38
+ return `functionIndexer.${ fileName } ` ;
39
+ }
40
+
41
+ public getCachedVariableIndexerName ( fileName : string ) : string {
42
+ return `variableIndexer.${ fileName } ` ;
43
+ }
44
+
45
+ public getCachedEnumIndexerName ( fileName : string ) : string {
46
+ return `enumIndexer.${ fileName } ` ;
47
+ }
48
+
49
+ public getFromCache < T > ( key : string ) : ICachedContent < T > | null {
50
+ const record = this . cache . get ( key ) ;
51
+ return record == null ? null : < ICachedContent < T > > record ;
52
+ }
53
+
54
+ public getCachedVariable ( fileName : string , variableName : string ) : ICachedContent < IVariableAssignment > | null {
55
+ return this . getFromCache < IVariableAssignment > ( this . getCachedVariableName ( fileName , variableName ) ) ;
56
+ }
57
+
58
+ public getCachedFunction ( fileName : string , functionName : string ) : ICachedContent < IFunctionDeclaration > | null {
59
+ return this . getFromCache < IFunctionDeclaration > ( this . getCachedFunctionName ( fileName , functionName ) ) ;
60
+ }
61
+
62
+ public getCachedEnum ( fileName : string , enumName : string ) : ICachedContent < IEnumDeclaration > | null {
63
+ return this . getFromCache < IEnumDeclaration > ( this . getCachedEnumName ( fileName , enumName ) ) ;
64
+ }
65
+
66
+ public getCachedProp ( fileName : string , className : string , propName : string ) : ICachedContent < IPropDeclaration > | null {
67
+ return this . getFromCache < IPropDeclaration > ( this . getCachedPropName ( fileName , className , propName ) ) ;
68
+ }
69
+
70
+ public getCachedClass ( fileName : string , className : string ) : ICachedContent < IClassDeclaration > | null {
71
+ return this . getFromCache < IClassDeclaration > ( this . getCachedClassName ( fileName , className ) ) ;
72
+ }
73
+
74
+ public getCachedFunctionIndexer ( fileName : string ) : ICachedContent < FunctionIndexer > | null {
75
+ return this . getFromCache < FunctionIndexer > ( this . getCachedFunctionIndexerName ( fileName ) ) ;
76
+ }
77
+
78
+ public getCachedModuleDependencies ( fileName : string ) : ICachedContent < IImportDeclaration [ ] > | null {
79
+ return this . getFromCache < IImportDeclaration [ ] > ( this . getCachedModuleDependenciesName ( fileName ) ) ;
80
+ }
81
+
82
+ public getCachedClassIndexer ( fileName : string ) : ICachedContent < ClassIndexer > | null {
83
+ return this . getFromCache < ClassIndexer > ( this . getCachedClassIndexerName ( fileName ) ) ;
84
+ }
85
+
86
+ public getCachedEnumIndexer ( fileName : string ) : ICachedContent < EnumIndexer > | null {
87
+ return this . getFromCache < EnumIndexer > ( this . getCachedEnumIndexerName ( fileName ) ) ;
88
+ }
89
+
90
+ public getCachedVariableIndexer ( fileName : string ) : ICachedContent < VariableIndexer > | null {
91
+ return this . getFromCache < VariableIndexer > ( this . getCachedVariableIndexerName ( fileName ) ) ;
92
+ }
93
+
94
+ public setCachedProp ( fileName : string , content : IPropDeclaration ) : void {
95
+ const version = this . languageService . getFileVersion ( fileName ) ;
96
+ this . cache . set ( this . getCachedPropName ( fileName , content . className , content . name ) , { content, version} ) ;
97
+ }
98
+
99
+ public setCachedVariable ( fileName : string , content : IVariableAssignment ) : void {
100
+ const version = this . languageService . getFileVersion ( fileName ) ;
101
+ this . cache . set ( this . getCachedVariableName ( fileName , content . name ) , { content, version} ) ;
102
+ }
103
+
104
+ public setCachedEnum ( fileName : string , content : IEnumDeclaration ) : void {
105
+ const version = this . languageService . getFileVersion ( fileName ) ;
106
+ this . cache . set ( this . getCachedEnumName ( fileName , content . name ) , { content, version} ) ;
107
+ }
108
+
109
+ public setCachedClass ( fileName : string , content : IClassDeclaration ) : void {
110
+ const version = this . languageService . getFileVersion ( fileName ) ;
111
+ this . cache . set ( this . getCachedClassName ( fileName , content . name ) , { version, content} ) ;
112
+ }
113
+
114
+ public setCachedFunction ( fileName : string , content : IFunctionDeclaration ) : void {
115
+ const version = this . languageService . getFileVersion ( fileName ) ;
116
+ this . cache . set ( this . getCachedFunctionName ( fileName , content . name ) , { version, content} ) ;
117
+ }
118
+
119
+ public setCachedClassIndexer ( fileName : string , content : ClassIndexer ) : void {
120
+ const version = this . languageService . getFileVersion ( fileName ) ;
121
+ this . cache . set ( this . getCachedClassIndexerName ( fileName ) , { version, content} ) ;
122
+ }
123
+
124
+ public setCachedFunctionIndexer ( fileName : string , content : FunctionIndexer ) : void {
125
+ const version = this . languageService . getFileVersion ( fileName ) ;
126
+ this . cache . set ( this . getCachedFunctionIndexerName ( fileName ) , { version, content} ) ;
127
+ }
128
+
129
+ public setCachedModuleDependencies ( fileName : string , content : IImportDeclaration [ ] ) : void {
130
+ const version = this . languageService . getFileVersion ( fileName ) ;
131
+ this . cache . set ( this . getCachedModuleDependenciesName ( fileName ) , { version, content} ) ;
132
+ }
133
+
134
+ public setCachedEnumIndexer ( fileName : string , content : EnumIndexer ) : void {
135
+ const version = this . languageService . getFileVersion ( fileName ) ;
136
+ this . cache . set ( this . getCachedEnumIndexerName ( fileName ) , { version, content} ) ;
137
+ }
138
+
139
+ public setCachedVariableIndexer ( fileName : string , content : VariableIndexer ) : void {
140
+ const version = this . languageService . getFileVersion ( fileName ) ;
141
+ this . cache . set ( this . getCachedVariableIndexerName ( fileName ) , { version, content} ) ;
142
+ }
143
+
144
+ public cachedVariableNeedsUpdate ( variable : IVariableAssignment ) : boolean {
145
+ const cache = this . getCachedVariable ( variable . filePath , variable . name ) ;
146
+ if ( cache == null ) return true ;
147
+
148
+ const version = this . languageService . getFileVersion ( variable . filePath ) ;
149
+ return version > cache . version ;
150
+ }
151
+
152
+ public cachedEnumNeedsUpdate ( enumDeclaration : IEnumDeclaration ) : boolean {
153
+ const cache = this . getCachedEnum ( enumDeclaration . filePath , enumDeclaration . name ) ;
154
+ if ( cache == null ) return true ;
155
+
156
+ const version = this . languageService . getFileVersion ( enumDeclaration . filePath ) ;
157
+ return version > cache . version ;
158
+ }
159
+
160
+ public cachedFunctionNeedsUpdate ( functionDeclaration : IFunctionDeclaration ) : boolean {
161
+ const cache = this . getCachedFunction ( functionDeclaration . filePath , functionDeclaration . name ) ;
162
+ if ( cache == null ) return true ;
163
+
164
+ const version = this . languageService . getFileVersion ( functionDeclaration . filePath ) ;
165
+ return version > cache . version ;
166
+ }
167
+
168
+ public cachedPropNeedsUpdate ( prop : IPropDeclaration ) : boolean {
169
+ const cache = this . getCachedProp ( prop . filePath , prop . className , prop . name ) ;
170
+ if ( cache == null ) return true ;
171
+
172
+ const version = this . languageService . getFileVersion ( prop . filePath ) ;
173
+ return version > cache . version ;
174
+ }
175
+
176
+ public cachedClassNeedsUpdate ( classDeclaration : IClassDeclaration ) : boolean {
177
+ const cache = this . getCachedClass ( classDeclaration . filePath , classDeclaration . name ) ;
178
+ if ( cache == null ) return true ;
179
+
180
+ const version = this . languageService . getFileVersion ( classDeclaration . filePath ) ;
181
+ return version > cache . version ;
182
+ }
183
+
184
+ public cachedFunctionIndexerNeedsUpdate ( filePath : string ) : boolean {
185
+ const cache = this . getCachedFunctionIndexer ( this . getCachedFunctionIndexerName ( filePath ) ) ;
186
+ if ( cache == null ) return true ;
187
+
188
+ const version = this . languageService . getFileVersion ( filePath ) ;
189
+ return version > cache . version ;
190
+ }
191
+
192
+ public cachedModuleDependenciesNeedsUpdate ( filePath : string ) : boolean {
193
+ const cache = this . getCachedModuleDependencies ( this . getCachedModuleDependenciesName ( filePath ) ) ;
194
+ if ( cache == null ) return true ;
195
+
196
+ const version = this . languageService . getFileVersion ( filePath ) ;
197
+ return version > cache . version ;
198
+ }
199
+
200
+ public cachedClassIndexerNeedsUpdate ( filePath : string ) : boolean {
201
+ const cache = this . getCachedClassIndexer ( this . getCachedClassIndexerName ( filePath ) ) ;
202
+ if ( cache == null ) return true ;
203
+
204
+ const version = this . languageService . getFileVersion ( filePath ) ;
205
+ return version > cache . version ;
206
+ }
207
+
208
+ public cachedEnumIndexerNeedsUpdate ( filePath : string ) : boolean {
209
+ const cache = this . getCachedEnumIndexer ( this . getCachedEnumIndexerName ( filePath ) ) ;
210
+ if ( cache == null ) return true ;
211
+
212
+ const version = this . languageService . getFileVersion ( filePath ) ;
213
+ return version > cache . version ;
214
+ }
215
+
216
+ public cachedVariableIndexerNeedsUpdate ( filePath : string ) : boolean {
217
+ const cache = this . getCachedVariableIndexer ( this . getCachedVariableIndexerName ( filePath ) ) ;
218
+ if ( cache == null ) return true ;
219
+
220
+ const version = this . languageService . getFileVersion ( filePath ) ;
221
+ return version > cache . version ;
222
+ }
223
+ }
0 commit comments