-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.ts
More file actions
168 lines (146 loc) · 4.39 KB
/
cache.ts
File metadata and controls
168 lines (146 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as fs from "fs";
import * as path from "path";
import type * as vscode from "vscode";
import { CSS_MODULES_CACHE_FILENAME, DEBOUNCE_TIMER } from "../config";
import {
type CacheJsonObject,
ClassNameCache,
ClassNameRangeMap,
ModulePathCache,
ModulePathCacheSet,
PathMapCache,
} from "../types/cache";
export default class Cache {
static pathMapCache = new PathMapCache();
/**
* Cache mapping from imported CSS module paths (relative to workspace)
* to the set of document paths that import them.
*/
static modulePathCache = new ModulePathCache(this.pathMapCache);
/**
* Cache mapping from imported CSS module paths (relative to workspace)
* to the set of document paths that import them.
*/
static classNameCache = new ClassNameCache(this.pathMapCache, { max: 3 });
private static _context: vscode.ExtensionContext;
/**
* The current extension context.
*/
public static get context(): vscode.ExtensionContext {
return Cache._context;
}
public static set context(value: vscode.ExtensionContext) {
Cache._context = value;
}
static async clearCache() {
if (!this.context.storageUri) {
return false;
}
this.modulePathCache.clear();
this.classNameCache.clear();
this.pathMapCache.clear();
const cacheFilePath = path.join(
this.context.storageUri.fsPath,
CSS_MODULES_CACHE_FILENAME
);
const cacheAsObject: CacheJsonObject = {
pathMapCache: [],
modulePathCache: {},
classNameCache: {},
};
try {
fs.mkdirSync(this.context.storageUri.fsPath, { recursive: true });
fs.writeFileSync(
cacheFilePath,
JSON.stringify(cacheAsObject, null, 2),
"utf-8"
);
} catch (error) {
console.error("Error clearing cache:", error);
return false;
}
return true;
}
static saveCacheDebounceId: NodeJS.Timeout;
/**
* Saves the current cache to a JSON file in the extension’s storage directory.
* Each key is a CSS module file, and the value is a list of documents that import it.
*/
static async saveCache() {
clearTimeout(this.saveCacheDebounceId);
this.saveCacheDebounceId = setTimeout(() => {
this._saveCache().catch(console.error);
}, DEBOUNCE_TIMER.CACHE);
}
static async _saveCache() {
if (!this.context.storageUri) {
return false;
}
const cacheFilePath = path.join(
this.context.storageUri.fsPath,
CSS_MODULES_CACHE_FILENAME
);
const cacheAsObject: CacheJsonObject = {
pathMapCache: [],
modulePathCache: {},
classNameCache: {},
};
cacheAsObject.pathMapCache = this.pathMapCache;
for (const [key, valueSet] of this.modulePathCache.entries()) {
cacheAsObject.modulePathCache[key] = [...valueSet];
}
for (const [key, valueSet] of this.classNameCache.entries()) {
cacheAsObject.classNameCache[key] = Object.fromEntries(valueSet);
}
try {
fs.mkdirSync(this.context.storageUri.fsPath, { recursive: true });
fs.writeFileSync(
cacheFilePath,
JSON.stringify(cacheAsObject, null, 2),
"utf-8"
);
} catch (error) {
console.error("Error saving cache:", error);
}
}
/**
* Loads the cache from a JSON file stored in the extension’s storage directory.
* If no file exists or loading fails, the cache remains empty.
*/
static async loadCache() {
if (!this.context.storageUri) {
return false;
}
const cacheFilePath = path.join(
this.context.storageUri.fsPath,
CSS_MODULES_CACHE_FILENAME
);
if (!fs.existsSync(cacheFilePath)) {
return false;
}
try {
const raw = fs.readFileSync(cacheFilePath, "utf-8");
const parsed: { [K in keyof CacheJsonObject]?: CacheJsonObject[K] } =
JSON.parse(raw);
this.pathMapCache.setArray(parsed.pathMapCache ?? []);
this.modulePathCache.setMap(
Object.entries(parsed.modulePathCache ?? {}).map(
([key, valueArray]) => [
key,
new ModulePathCacheSet(this.pathMapCache, valueArray),
]
)
);
this.classNameCache.setMap(
Object.entries(parsed.classNameCache ?? {}).map(([key, value]) => [
key,
new ClassNameRangeMap(Object.entries(value)),
])
);
} catch (error) {
console.error("Error loading cache:", error);
return false;
}
return true;
}
}