Skip to content

Commit 5d95de8

Browse files
committed
feat: expose access to internal pending values cache
to support inspection of the pending state for complex debugging cases
1 parent 249df12 commit 5d95de8

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/cachified.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CacheEntry,
1414
GetFreshValue,
1515
createCacheEntry,
16+
getPendingValuesCache,
1617
} from './index';
1718
import { Deferred } from './createBatch';
1819
import { delay, report } from './testHelpers';
@@ -1112,6 +1113,23 @@ describe('cachified', () => {
11121113
expect(await getValue(() => 'FOUR')).toBe('THREE');
11131114
});
11141115

1116+
it('provides access to internal pending values cache', async () => {
1117+
const cache = new Map<string, CacheEntry>();
1118+
const pendingValuesCache = getPendingValuesCache(cache);
1119+
const d = new Deferred<string>();
1120+
1121+
cachified({ cache, key: 'test', ttl: 5, getFreshValue: () => d.promise });
1122+
await delay(0); // pending values are not set immediately
1123+
1124+
expect(pendingValuesCache.get('test')).toEqual(
1125+
expect.objectContaining({
1126+
metadata: anyMetadata,
1127+
value: expect.any(Promise),
1128+
resolve: expect.any(Function),
1129+
}),
1130+
);
1131+
});
1132+
11151133
it('uses stale cache while revalidating', async () => {
11161134
const cache = new Map<string, CacheEntry>();
11171135
const reporter = createReporter();
@@ -1679,7 +1697,7 @@ describe('cachified', () => {
16791697
});
16801698

16811699
it('supports trace ids', async () => {
1682-
expect.assertions(6);
1700+
expect.assertions(7);
16831701

16841702
const cache = new Map<string, CacheEntry>();
16851703
const traceId1 = Symbol();
@@ -1698,6 +1716,11 @@ describe('cachified', () => {
16981716
});
16991717
await delay(0);
17001718

1719+
// on pending values cache
1720+
expect(getPendingValuesCache(cache).get('test-1')?.metadata.traceId).toBe(
1721+
traceId1,
1722+
);
1723+
17011724
d.resolve('ONE');
17021725
expect(await value).toBe('ONE');
17031726

src/cachified.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const pendingValuesByCache = new WeakMap<Cache, Map<string, any>>();
1818
/**
1919
* Get the internal pending values cache for a given cache
2020
*/
21-
function getPendingValuesCache(cache: Cache) {
21+
export function getPendingValuesCache(cache: Cache) {
2222
if (!pendingValuesByCache.has(cache)) {
2323
pendingValuesByCache.set(cache, new Map());
2424
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type {
1010
export { staleWhileRevalidate, totalTtl, createCacheEntry } from './common';
1111
export * from './reporter';
1212
export { createBatch } from './createBatch';
13-
export { cachified } from './cachified';
13+
export { cachified, getPendingValuesCache } from './cachified';
1414
export { cachified as default } from './cachified';
1515
export { shouldRefresh, isExpired } from './isExpired';
1616
export { assertCacheEntry } from './assertCacheEntry';

0 commit comments

Comments
 (0)