@@ -7,8 +7,15 @@ interface CacheEntry<V> {
77 accessTTL : number | undefined ;
88}
99
10- export interface Cancellable {
11- cancelled ( ) : void ;
10+ export class CacheController {
11+ #invalidated = false ;
12+ get invalidated ( ) : boolean {
13+ return this . #invalidated;
14+ }
15+
16+ invalidate ( ) : void {
17+ this . #invalidated = true ;
18+ }
1219}
1320
1421interface PromiseCacheOptions {
@@ -32,7 +39,7 @@ export class PromiseCache<K, V> {
3239
3340 async getOrCreate (
3441 key : K ,
35- factory : ( cancellable : Cancellable ) => Promise < V > ,
42+ factory : ( cacheable : CacheController ) => Promise < V > ,
3643 options ?: {
3744 /** TTL (time-to-live) in milliseconds since creation */
3845 createTTL ?: number ;
@@ -56,10 +63,10 @@ export class PromiseCache<K, V> {
5663 return entry . promise ;
5764 }
5865
59- let cancelled = false ;
60- const promise = factory ( { cancelled : ( ) => ( cancelled = true ) } ) ;
66+ const cacheable = new CacheController ( ) ;
67+ const promise = factory ( cacheable ) ;
6168 void promise . finally ( ( ) => {
62- if ( cancelled ) {
69+ if ( cacheable . invalidated ) {
6370 this . cache . delete ( key ) ;
6471 }
6572 } ) ;
@@ -152,15 +159,15 @@ export class PromiseMap<K, V> {
152159 * Gets a promise from the cache or creates a new one using the factory function.
153160 * Failed promises are automatically removed from the cache.
154161 */
155- getOrCreate ( key : K , factory : ( cancellable : Cancellable ) => Promise < V > ) : Promise < V > {
162+ getOrCreate ( key : K , factory : ( cacheable : CacheController ) => Promise < V > ) : Promise < V > {
156163 let promise = this . cache . get ( key ) ;
157164 if ( promise == null ) {
158- let cancelled = false ;
159- promise = factory ( { cancelled : ( ) => ( cancelled = true ) } ) ;
165+ const cacheable = new CacheController ( ) ;
166+ promise = factory ( cacheable ) ;
160167 // Automatically remove failed promises from the cache
161168 promise . catch ( ( ) => this . cache . delete ( key ) ) ;
162169 void promise . finally ( ( ) => {
163- if ( cancelled ) {
170+ if ( cacheable . invalidated ) {
164171 this . cache . delete ( key ) ;
165172 }
166173 } ) ;
@@ -283,7 +290,7 @@ export class RepoPromiseCacheMap<K, V> {
283290 getOrCreate (
284291 repoPath : string ,
285292 key : K ,
286- factory : ( cancellable : Cancellable ) => Promise < V > ,
293+ factory : ( cacheable : CacheController ) => Promise < V > ,
287294 options ?: {
288295 /** TTL (time-to-live) in milliseconds since creation */
289296 createTTL ?: number ;
@@ -358,7 +365,7 @@ export class RepoPromiseMap<K, V> {
358365 * @param key - The cache key within the repository (inner key)
359366 * @param factory - Factory function to create the value if not cached
360367 */
361- getOrCreate ( repoPath : string , key : K , factory : ( cancellable : Cancellable ) => Promise < V > ) : Promise < V > {
368+ getOrCreate ( repoPath : string , key : K , factory : ( cacheable : CacheController ) => Promise < V > ) : Promise < V > {
362369 let repoCache = this . cache . get ( repoPath ) ;
363370 if ( repoCache == null ) {
364371 repoCache = new PromiseMap < K , V > ( ) ;
0 commit comments