import { CacheTransactionImpl } from './cache-transaction.js'; import type { Cache, CommittingTransaction, CachedEntityRevision, DefaultRegistry, CacheTransactionDebugAPIs, } from './index.js'; export class CommittingTransactionImpl< CacheKeyRegistry extends DefaultRegistry, Key extends keyof CacheKeyRegistry = keyof CacheKeyRegistry, $Debug = unknown, UserExtensionData = unknown, Context extends object = object > extends CacheTransactionImpl< CacheKeyRegistry, Key, $Debug, UserExtensionData, Context > implements CommittingTransaction< CacheKeyRegistry, Key, $Debug, UserExtensionData, Context > { #txRetainedEntryRevisions: Map< Key, CachedEntityRevision[] >; $debug?: ($Debug & CacheTransactionDebugAPIs) | undefined; cache: { clearRevisions(id: Key): void; appendRevisions( id: Key, revisions: CachedEntityRevision[] ): void; }; mergedEntryRevisions(): Map< Key, CachedEntityRevision[] > { return this.#txRetainedEntryRevisions; } updateRevisions( localRevisionsMap: Map[]> ): void { this.setLocalRevisions(localRevisionsMap); } constructor( originalCache: Cache< CacheKeyRegistry, Key, $Debug, UserExtensionData, Context >, cacheRevisionsBeforeTransaction: Map< Key, CachedEntityRevision[] > ) { super(originalCache); this.setRevisionsBeforeTransactionStart(cacheRevisionsBeforeTransaction); const appendRevisions = ( cacheKey: Key, revisions: CachedEntityRevision[] ) => this.#appendRevisions(cacheKey, revisions); const clearRevisions = (cacheKey: Key) => this.#clearRevisions(cacheKey); this.cache = { clearRevisions, appendRevisions, }; this.#txRetainedEntryRevisions = new Map< Key, CachedEntityRevision[] >(); } #appendRevisions( cacheKey: Key, revisions: CachedEntityRevision[] ) { if (this.#txRetainedEntryRevisions.has(cacheKey)) { const appendedRevisions = this.#txRetainedEntryRevisions.get(cacheKey)?.concat(revisions) || []; this.#txRetainedEntryRevisions.set(cacheKey, appendedRevisions); } else { this.#txRetainedEntryRevisions.set(cacheKey, revisions); } } #clearRevisions(cacheKey: Key) { if (this.#txRetainedEntryRevisions.has(cacheKey)) { this.#txRetainedEntryRevisions.delete(cacheKey); } } }