Skip to content

Commit f3df78b

Browse files
committed
refactor(@angular/build): use namespaced Cache abstraction in i18n inliner
Updates I18nInliner to instantiate and use a namespaced Cache<TransformedFileResult> instance (cacheStore.createCache('transforms')) instead of directly invoking lower-level CacheStore methods (get/set). Using the Cache abstraction ensures standard namespace partitioning within the angular-i18n persistent cache store, automatic in-flight promise management, and proper get/put promise semantics.
1 parent 625ee76 commit f3df78b

1 file changed

Lines changed: 19 additions & 19 deletions

File tree

packages/angular/build/src/tools/esbuild/i18n-inliner.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { serialize } from 'node:v8';
1212
import { calculateHash, createContentHash, initializeHash } from '../../utils/hash';
1313
import { WorkerPool } from '../../utils/worker-pool';
1414
import { type BuildOutputFile, BuildOutputFileType, createOutputFile } from './bundler-files';
15-
import { type PersistentCacheStore, createPersistentCacheStore } from './cache';
15+
import { type Cache, type PersistentCacheStore, createPersistentCacheStore } from './cache';
1616

1717
/**
1818
* A keyword used to indicate if a JavaScript file may require inlining of translations.
@@ -125,7 +125,8 @@ interface CacheCheckItem {
125125
export class I18nInliner {
126126
#cacheInitFailed = false;
127127
#workerPool: WorkerPool;
128-
#cache: PersistentCacheStore | undefined;
128+
#cacheStore: PersistentCacheStore | undefined;
129+
#cache: Cache<TransformedFileResult> | undefined;
129130
readonly #localizeFiles: ReadonlyMap<string, BuildOutputFile>;
130131
readonly #unmodifiedFiles: Array<BuildOutputFile>;
131132

@@ -241,7 +242,7 @@ export class I18nInliner {
241242
for (const { locale, translation, translationIntegrity } of windowLocales) {
242243
localeBlobs.set(locale, serializeTranslation(translation));
243244

244-
if (this.#cache) {
245+
if (this.#cacheStore) {
245246
localeCacheBases.set(
246247
locale,
247248
calculateHash(
@@ -277,7 +278,10 @@ export class I18nInliner {
277278
hasher.update(fileCacheKeyBase);
278279
cacheKey = hasher.digest();
279280

280-
cachedResultPromise = this.#cache.get(cacheKey).catch(() => null);
281+
cachedResultPromise = this.#cache
282+
.get(cacheKey)
283+
.then((val) => val ?? null)
284+
.catch(() => null);
281285
}
282286

283287
cacheChecks.push({
@@ -424,18 +428,13 @@ export class I18nInliner {
424428
const cacheKey = matchingEntry?.cacheKey;
425429

426430
if (this.#cache && cacheKey) {
427-
// `CacheStore.set` may return `this` synchronously or a `Promise<this>`.
428-
// `Promise.resolve` normalizes both return values into a Promise so `Promise.allSettled`
429-
// can safely handle any synchronous or asynchronous cache store errors.
430431
cachePromises.push(
431-
Promise.resolve(
432-
this.#cache.set(cacheKey, {
433-
file: filename,
434-
code: res.code,
435-
map: res.map,
436-
messages: res.messages,
437-
}),
438-
),
432+
this.#cache.put(cacheKey, {
433+
file: filename,
434+
code: res.code,
435+
map: res.map,
436+
messages: res.messages,
437+
}),
439438
);
440439
}
441440

@@ -520,7 +519,7 @@ export class I18nInliner {
520519
* @returns A void promise that resolves when closing is complete.
521520
*/
522521
async close(): Promise<void> {
523-
await Promise.allSettled([this.#cache?.close(), this.#workerPool.destroy()]);
522+
await Promise.allSettled([this.#cacheStore?.close(), this.#workerPool.destroy()]);
524523
}
525524

526525
/**
@@ -530,7 +529,7 @@ export class I18nInliner {
530529
* @returns A promise that resolves once the cache initialization process is complete.
531530
*/
532531
private async initCache(): Promise<void> {
533-
if (this.#cache || this.#cacheInitFailed) {
532+
if (this.#cacheStore || this.#cacheInitFailed) {
534533
return;
535534
}
536535

@@ -542,11 +541,12 @@ export class I18nInliner {
542541

543542
// Initialize a persistent cache for i18n transformations.
544543
try {
545-
const [, cache] = await Promise.all([
544+
const [, cacheStore] = await Promise.all([
546545
initializeHash(),
547546
createPersistentCacheStore(join(persistentCachePath, 'angular-i18n')),
548547
]);
549-
this.#cache = cache;
548+
this.#cacheStore = cacheStore;
549+
this.#cache = cacheStore.createCache('transforms');
550550
} catch {
551551
this.#cacheInitFailed = true;
552552

0 commit comments

Comments
 (0)