Skip to content

Commit 3289d17

Browse files
committed
Adds CacheController for cache invalidation
- Replaces the `Cancellable` interface
1 parent 2bcf10b commit 3289d17

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

src/env/node/git/sub-providers/commits.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { filterMap, first, join, last, some } from '../../../../system/iterable'
5454
import { Logger } from '../../../../system/logger';
5555
import { getLogScope } from '../../../../system/logger.scope';
5656
import { isFolderGlob, stripFolderGlob } from '../../../../system/path';
57-
import type { Cancellable } from '../../../../system/promiseCache';
57+
import type { CacheController } from '../../../../system/promiseCache';
5858
import { maybeStopWatch } from '../../../../system/stopwatch';
5959
import { createDisposable } from '../../../../system/unifiedDisposable';
6060
import type { CachedLog, TrackedGitDocument } from '../../../../trackers/trackedDocument';
@@ -183,7 +183,7 @@ export class CommitsGitSubProvider implements GitCommitsSubProvider {
183183

184184
const scope = getLogScope();
185185

186-
const getCore = async (cancellable?: Cancellable) => {
186+
const getCore = async (cacheable?: CacheController) => {
187187
try {
188188
// Use for-each-ref with %(HEAD) to mark current branch with *
189189
const result = await this.git.exec(
@@ -242,7 +242,7 @@ export class CommitsGitSubProvider implements GitCommitsSubProvider {
242242

243243
return { refs: refs };
244244
} catch (ex) {
245-
cancellable?.cancelled();
245+
cacheable?.invalidate();
246246
debugger;
247247
if (isCancellationError(ex)) throw ex;
248248

src/env/node/git/sub-providers/contributors.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { log } from '../../../../system/decorators/log';
1414
import { Logger } from '../../../../system/logger';
1515
import { getLogScope } from '../../../../system/logger.scope';
1616
import { normalizePath } from '../../../../system/path';
17-
import type { Cancellable } from '../../../../system/promiseCache';
17+
import type { CacheController } from '../../../../system/promiseCache';
1818
import { createDisposable } from '../../../../system/unifiedDisposable';
1919
import type { Git } from '../git';
2020
import { gitConfigsLog } from '../git';
@@ -46,7 +46,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
4646

4747
const scope = getLogScope();
4848

49-
const getCore = async (cancellable?: Cancellable): Promise<GitContributorsResult> => {
49+
const getCore = async (cacheable?: CacheController): Promise<GitContributorsResult> => {
5050
const contributors = new Map<string, GitContributor>();
5151

5252
try {
@@ -95,7 +95,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
9595

9696
for await (const c of parser.parseAsync(stream)) {
9797
if (signal?.aborted || cancellation?.isCancellationRequested) {
98-
cancellable?.cancelled();
98+
cacheable?.invalidate();
9999
break;
100100
}
101101

@@ -176,7 +176,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
176176
: undefined,
177177
};
178178
} catch (ex) {
179-
cancellable?.cancelled();
179+
cacheable?.invalidate();
180180
Logger.error(ex, scope);
181181
debugger;
182182

@@ -234,7 +234,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
234234
options = { ...options, all: true };
235235
}
236236

237-
const getCore = async (cancellable?: Cancellable) => {
237+
const getCore = async (cacheable?: CacheController) => {
238238
try {
239239
// eventually support `--group=author --group=trailer:co-authored-by`
240240
const args = ['shortlog', '-s', '-e', '-n'];
@@ -262,7 +262,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
262262
const shortlog = parseShortlog(result.stdout, repoPath, await currentUserPromise);
263263
return shortlog.contributors;
264264
} catch (ex) {
265-
cancellable?.cancelled();
265+
cacheable?.invalidate();
266266
Logger.error(ex, scope);
267267
debugger;
268268

src/env/node/git/sub-providers/remotes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class RemotesGitSubProvider extends RemotesGitProviderBase implements Git
5050
);
5151
return remotes;
5252
} catch (ex) {
53-
cancellable.cancelled();
53+
cancellable.invalidate();
5454
Logger.error(ex, scope);
5555
return [];
5656
}

src/env/node/git/sub-providers/tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class TagsGitSubProvider implements GitTagsSubProvider {
8080

8181
return { values: tags };
8282
} catch (ex) {
83-
cancellable.cancelled();
83+
cancellable.invalidate();
8484
Logger.error(ex, scope);
8585
if (isCancellationError(ex)) throw ex;
8686

src/plus/ai/aiProviderService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ export class AIProviderService implements AIService, Disposable {
11011101
variables: result.data.variables as (keyof PromptTemplateContext<T>)[],
11021102
} satisfies PromptTemplate<T>;
11031103
} catch (ex) {
1104-
cancellable.cancelled();
1104+
cancellable.invalidate();
11051105
if (!(ex instanceof AuthenticationRequiredError)) {
11061106
debugger;
11071107
Logger.error(ex, scope, String(ex));

src/plus/integrations/providers/github/sub-providers/contributors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { isUserMatch } from '../../../../../git/utils/user.utils';
1212
import { log } from '../../../../../system/decorators/log';
1313
import { Logger } from '../../../../../system/logger';
1414
import { getLogScope } from '../../../../../system/logger.scope';
15-
import type { Cancellable } from '../../../../../system/promiseCache';
15+
import type { CacheController } from '../../../../../system/promiseCache';
1616
import type { GitHubGitProviderInternal } from '../githubGitProvider';
1717

1818
export class ContributorsGitSubProvider implements GitContributorsSubProvider {
@@ -40,7 +40,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
4040

4141
const scope = getLogScope();
4242

43-
const getCore = async (cancellable?: Cancellable): Promise<GitContributorsResult> => {
43+
const getCore = async (cacheable?: CacheController): Promise<GitContributorsResult> => {
4444
const contributors: GitContributor[] = [];
4545

4646
try {
@@ -195,7 +195,7 @@ export class ContributorsGitSubProvider implements GitContributorsSubProvider {
195195
cancelled: cancellation?.isCancellationRequested ? { reason: 'cancelled' } : undefined,
196196
};
197197
} catch (ex) {
198-
cancellable?.cancelled();
198+
cacheable?.invalidate();
199199
Logger.error(ex, scope);
200200
debugger;
201201

src/plus/integrations/providers/github/sub-providers/tags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class TagsGitSubProvider implements GitTagsSubProvider {
9292
cursor = result.paging.cursor;
9393
}
9494
} catch (ex) {
95-
cancellable.cancelled();
95+
cancellable.invalidate();
9696
Logger.error(ex, scope);
9797
debugger;
9898

src/system/promiseCache.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1421
interface 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

Comments
 (0)