File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -1413,6 +1413,41 @@ describe('cachified', () => {
1413
1413
expect ( cache . get ( 'test-2' ) ) . toBe ( undefined ) ;
1414
1414
} ) ;
1415
1415
1416
+ it ( 'de-duplicates batched cache calls' , async ( ) => {
1417
+ const cache = new Map < string , CacheEntry > ( ) ;
1418
+
1419
+ function getValues ( indexes : number [ ] , callId : number ) {
1420
+ const batch = createBatch ( ( freshIndexes : number [ ] ) =>
1421
+ freshIndexes . map ( ( i ) => `value-${ i } -call-${ callId } ` ) ,
1422
+ ) ;
1423
+
1424
+ return Promise . all (
1425
+ indexes . map ( ( index ) =>
1426
+ cachified ( {
1427
+ cache,
1428
+ key : `test-${ index } ` ,
1429
+ ttl : Infinity ,
1430
+ getFreshValue : batch . add ( index ) ,
1431
+ } ) ,
1432
+ ) ,
1433
+ ) ;
1434
+ }
1435
+
1436
+ const batch1 = getValues ( [ 1 , 2 , 3 ] , 1 ) ;
1437
+ const batch2 = getValues ( [ 1 , 2 , 5 ] , 2 ) ;
1438
+
1439
+ expect ( await batch1 ) . toEqual ( [
1440
+ 'value-1-call-1' ,
1441
+ 'value-2-call-1' ,
1442
+ 'value-3-call-1' ,
1443
+ ] ) ;
1444
+ expect ( await batch2 ) . toEqual ( [
1445
+ 'value-1-call-1' ,
1446
+ 'value-2-call-1' ,
1447
+ 'value-5-call-2' ,
1448
+ ] ) ;
1449
+ } ) ;
1450
+
1416
1451
it ( 'does not invoke onValue when value comes from cache' , async ( ) => {
1417
1452
const cache = new Map < string , CacheEntry > ( ) ;
1418
1453
const onValue = jest . fn ( ) ;
Original file line number Diff line number Diff line change 4
4
Cache ,
5
5
CacheEntry ,
6
6
createContext ,
7
+ HANDLE ,
7
8
} from './common' ;
8
9
import { CACHE_EMPTY , getCachedValue } from './getCachedValue' ;
9
10
import { getFreshValue } from './getFreshValue' ;
@@ -53,6 +54,8 @@ export async function cachified<Value>(
53
54
if ( pendingValues . has ( key ) ) {
54
55
const { value : pendingRefreshValue , metadata } = pendingValues . get ( key ) ! ;
55
56
if ( ! shouldRefresh ( metadata ) ) {
57
+ /* Notify batch that we handled this call using pending value */
58
+ context . getFreshValue [ HANDLE ] ?.( ) ;
56
59
report ( { name : 'getFreshValueHookPending' } ) ;
57
60
const value = await pendingRefreshValue ;
58
61
report ( { name : 'done' , value } ) ;
You can’t perform that action at this time.
0 commit comments