Skip to content

Commit 067171f

Browse files
committed
Not all sass cache should be cleanup the same so use a method in the class itself and register one handler per cache
1 parent 394c214 commit 067171f

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/core/sass.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ export async function compileWithCache(
307307
const cacheDir = useSessionCache
308308
? join(temp.baseDir, "sass")
309309
: quartoCacheDir("sass");
310-
const cache = await sassCache(cacheDir);
310+
// when using quarto session cache, we ensure to cleanup the cache files
311+
const cache = await sassCache(cacheDir, !!useSessionCache);
311312
return cache.getOrSet(input, loadPaths, temp, cacheIdentifier, compressed);
312313
} else {
313314
const outputFilePath = temp.createFile({ suffix: ".css" });

src/core/sass/cache.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ class SassCache {
156156
compressed,
157157
);
158158
}
159+
160+
// add a cleanup method to register a cleanup handler
161+
cleanup(removeCachePath: boolean = true) {
162+
onCleanup(() => {
163+
try {
164+
this.kv.close();
165+
if (removeCachePath) safeRemoveIfExists(this.path);
166+
} catch (error) {
167+
log.info("Error occurred during sass cache cleanup: " + error);
168+
}
169+
});
170+
}
159171
}
160172

161173
const currentSassCacheVersion = 1;
@@ -192,34 +204,20 @@ async function checkVersion(kv: Deno.Kv, path: string) {
192204
}
193205
}
194206

195-
let cleanupRegistered = false;
196-
197-
function ensureCleanup() {
198-
if (!cleanupRegistered) {
199-
cleanupRegistered = true;
200-
onCleanup(() => {
201-
for (const cache of Object.values(_sassCache)) {
202-
try {
203-
cache.kv.close();
204-
safeRemoveIfExists(cache.path);
205-
} catch (error) {
206-
log.info("Error occurred during sass cache cleanup: " + error);
207-
}
208-
}
209-
});
210-
}
211-
}
212-
213207
const _sassCache: Record<string, SassCache> = {};
214-
export async function sassCache(path: string): Promise<SassCache> {
208+
export async function sassCache(
209+
path: string,
210+
removeCachePath: boolean = false,
211+
): Promise<SassCache> {
215212
if (!_sassCache[path]) {
216213
log.debug(`Creating SassCache at ${path}`);
217214
ensureDirSync(path);
218215
const kvFile = join(path, "sass.kv");
219216
const kv = await Deno.openKv(kvFile);
220-
ensureCleanup();
221217
await checkVersion(kv, kvFile);
222218
_sassCache[path] = new SassCache(kv, path);
219+
// register cleanup for this cache
220+
_sassCache[path].cleanup(removeCachePath);
223221
}
224222
log.debug(`Returning SassCache at ${path}`);
225223
const result = _sassCache[path];

0 commit comments

Comments
 (0)