@@ -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
161173const 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-
213207const _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