10
10
import type { Options } from '..' ;
11
11
const DataLoader = require ( '..' ) ;
12
12
13
- function idLoader < K , C > (
13
+ function idLoader < K , C = K > (
14
14
options ?: Options < K , K , C >
15
15
) : [ DataLoader < K , K , C > , Array < $ReadOnlyArray < K >> ] {
16
16
const loadCalls = [ ] ;
@@ -24,7 +24,7 @@ function idLoader<K, C>(
24
24
describe ( 'Primary API' , ( ) => {
25
25
26
26
it ( 'builds a really really simple data loader' , async ( ) => {
27
- const identityLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
27
+ const identityLoader = new DataLoader < number , number > ( async keys => keys ) ;
28
28
29
29
const promise1 = identityLoader . load ( 1 ) ;
30
30
expect ( promise1 ) . toBeInstanceOf ( Promise ) ;
@@ -35,7 +35,7 @@ describe('Primary API', () => {
35
35
36
36
it ( 'references the loader as "this" in the batch function' , async ( ) => {
37
37
let that ;
38
- const loader = new DataLoader ( async function ( keys ) {
38
+ const loader = new DataLoader < number , number > ( async function ( keys ) {
39
39
that = this ;
40
40
return keys ;
41
41
} ) ;
@@ -47,7 +47,7 @@ describe('Primary API', () => {
47
47
} ) ;
48
48
49
49
it ( 'supports loading multiple keys in one call' , async ( ) => {
50
- const identityLoader = new DataLoader ( keys => Promise . resolve ( keys ) ) ;
50
+ const identityLoader = new DataLoader < number , number > ( async keys => keys ) ;
51
51
52
52
const promiseAll = identityLoader . loadMany ( [ 1 , 2 ] ) ;
53
53
expect ( promiseAll ) . toBeInstanceOf ( Promise ) ;
@@ -63,7 +63,7 @@ describe('Primary API', () => {
63
63
} ) ;
64
64
65
65
it ( 'batches multiple requests' , async ( ) => {
66
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
66
+ const [ identityLoader , loadCalls ] = idLoader < number > ( ) ;
67
67
68
68
const promise1 = identityLoader . load ( 1 ) ;
69
69
const promise2 = identityLoader . load ( 2 ) ;
@@ -76,7 +76,7 @@ describe('Primary API', () => {
76
76
} ) ;
77
77
78
78
it ( 'batches multiple requests with max batch sizes' , async ( ) => {
79
- const [ identityLoader , loadCalls ] = idLoader ( { maxBatchSize : 2 } ) ;
79
+ const [ identityLoader , loadCalls ] = idLoader < number > ( { maxBatchSize : 2 } ) ;
80
80
81
81
const promise1 = identityLoader . load ( 1 ) ;
82
82
const promise2 = identityLoader . load ( 2 ) ;
@@ -92,7 +92,7 @@ describe('Primary API', () => {
92
92
} ) ;
93
93
94
94
it ( 'coalesces identical requests' , async ( ) => {
95
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
95
+ const [ identityLoader , loadCalls ] = idLoader < number > ( ) ;
96
96
97
97
const promise1a = identityLoader . load ( 1 ) ;
98
98
const promise1b = identityLoader . load ( 1 ) ;
@@ -107,7 +107,7 @@ describe('Primary API', () => {
107
107
} ) ;
108
108
109
109
it ( 'caches repeated requests' , async ( ) => {
110
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
110
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
111
111
112
112
const [ a , b ] = await Promise . all ( [
113
113
identityLoader . load ( 'A' ) ,
@@ -143,7 +143,7 @@ describe('Primary API', () => {
143
143
} ) ;
144
144
145
145
it ( 'clears single value in loader' , async ( ) => {
146
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
146
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
147
147
148
148
const [ a , b ] = await Promise . all ( [
149
149
identityLoader . load ( 'A' ) ,
@@ -169,7 +169,7 @@ describe('Primary API', () => {
169
169
} ) ;
170
170
171
171
it ( 'clears all values in loader' , async ( ) => {
172
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
172
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
173
173
174
174
const [ a , b ] = await Promise . all ( [
175
175
identityLoader . load ( 'A' ) ,
@@ -195,7 +195,7 @@ describe('Primary API', () => {
195
195
} ) ;
196
196
197
197
it ( 'allows priming the cache' , async ( ) => {
198
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
198
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
199
199
200
200
identityLoader . prime ( 'A' , 'A' ) ;
201
201
@@ -211,7 +211,7 @@ describe('Primary API', () => {
211
211
} ) ;
212
212
213
213
it ( 'does not prime keys that already exist' , async ( ) => {
214
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
214
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
215
215
216
216
identityLoader . prime ( 'A' , 'X' ) ;
217
217
@@ -232,7 +232,7 @@ describe('Primary API', () => {
232
232
} ) ;
233
233
234
234
it ( 'allows forcefully priming the cache' , async ( ) => {
235
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
235
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
236
236
237
237
identityLoader . prime ( 'A' , 'X' ) ;
238
238
@@ -337,7 +337,7 @@ describe('Represents Errors', () => {
337
337
} ) ;
338
338
339
339
it ( 'Handles priming the cache with an error' , async ( ) => {
340
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
340
+ const [ identityLoader , loadCalls ] = idLoader < number > ( ) ;
341
341
342
342
identityLoader . prime ( 1 , new Error ( 'Error: 1' ) ) ;
343
343
@@ -427,7 +427,7 @@ describe('Represents Errors', () => {
427
427
describe ( 'Accepts any kind of key' , ( ) => {
428
428
429
429
it ( 'Accepts objects as keys' , async ( ) => {
430
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
430
+ const [ identityLoader , loadCalls ] = idLoader < { } > ( ) ;
431
431
432
432
const keyA = { } ;
433
433
const keyB = { } ;
@@ -471,7 +471,7 @@ describe('Accepts options', () => {
471
471
472
472
// Note: mirrors 'batches multiple requests' above.
473
473
it ( 'May disable batching' , async ( ) => {
474
- const [ identityLoader , loadCalls ] = idLoader ( { batch : false } ) ;
474
+ const [ identityLoader , loadCalls ] = idLoader < number > ( { batch : false } ) ;
475
475
476
476
const promise1 = identityLoader . load ( 1 ) ;
477
477
const promise2 = identityLoader . load ( 2 ) ;
@@ -485,7 +485,7 @@ describe('Accepts options', () => {
485
485
486
486
// Note: mirror's 'caches repeated requests' above.
487
487
it ( 'May disable caching' , async ( ) => {
488
- const [ identityLoader , loadCalls ] = idLoader ( { cache : false } ) ;
488
+ const [ identityLoader , loadCalls ] = idLoader < string > ( { cache : false } ) ;
489
489
490
490
const [ a , b ] = await Promise . all ( [
491
491
identityLoader . load ( 'A' ) ,
@@ -523,7 +523,7 @@ describe('Accepts options', () => {
523
523
} ) ;
524
524
525
525
it ( 'Keys are repeated in batch when cache disabled' , async ( ) => {
526
- const [ identityLoader , loadCalls ] = idLoader ( { cache : false } ) ;
526
+ const [ identityLoader , loadCalls ] = idLoader < string > ( { cache : false } ) ;
527
527
528
528
const [ values1 , values2 , values3 , values4 ] = await Promise . all ( [
529
529
identityLoader . load ( 'A' ) ,
@@ -545,7 +545,7 @@ describe('Accepts options', () => {
545
545
it ( 'Does not interact with a cache when cache is disabled' , ( ) => {
546
546
const promiseX = Promise . resolve ( 'X' ) ;
547
547
const cacheMap = new Map ( [ [ 'X' , promiseX ] ] ) ;
548
- const [ identityLoader ] = idLoader ( { cache : false , cacheMap } ) ;
548
+ const [ identityLoader ] = idLoader < string > ( { cache : false , cacheMap } ) ;
549
549
550
550
identityLoader . prime ( 'A' , 'A' ) ;
551
551
expect ( cacheMap . get ( 'A' ) ) . toBe ( undefined ) ;
@@ -558,7 +558,7 @@ describe('Accepts options', () => {
558
558
it ( 'Complex cache behavior via clearAll()' , async ( ) => {
559
559
// This loader clears its cache as soon as a batch function is dispatched.
560
560
const loadCalls = [ ] ;
561
- const identityLoader = new DataLoader ( keys => {
561
+ const identityLoader = new DataLoader < string , string > ( keys => {
562
562
identityLoader . clearAll ( ) ;
563
563
loadCalls . push ( keys ) ;
564
564
return Promise . resolve ( keys ) ;
@@ -588,9 +588,11 @@ describe('Accepts options', () => {
588
588
return Object . keys ( key ) . sort ( ) . map ( k => k + ':' + key [ k ] ) . join ( ) ;
589
589
}
590
590
591
+ type Obj = { [ string ] : number } ;
592
+
591
593
it ( 'Accepts objects with a complex key' , async ( ) => {
592
594
const identityLoadCalls = [ ] ;
593
- const identityLoader = new DataLoader ( keys => {
595
+ const identityLoader = new DataLoader < Obj , Obj , string > ( keys => {
594
596
identityLoadCalls . push ( keys ) ;
595
597
return Promise . resolve ( keys ) ;
596
598
} , { cacheKeyFn : cacheKey } ) ;
@@ -608,7 +610,7 @@ describe('Accepts options', () => {
608
610
609
611
it ( 'Clears objects with complex key' , async ( ) => {
610
612
const identityLoadCalls = [ ] ;
611
- const identityLoader = new DataLoader ( keys => {
613
+ const identityLoader = new DataLoader < Obj , Obj , string > ( keys => {
612
614
identityLoadCalls . push ( keys ) ;
613
615
return Promise . resolve ( keys ) ;
614
616
} , { cacheKeyFn : cacheKey } ) ;
@@ -627,7 +629,7 @@ describe('Accepts options', () => {
627
629
628
630
it ( 'Accepts objects with different order of keys' , async ( ) => {
629
631
const identityLoadCalls = [ ] ;
630
- const identityLoader = new DataLoader ( keys => {
632
+ const identityLoader = new DataLoader < Obj , Obj , string > ( keys => {
631
633
identityLoadCalls . push ( keys ) ;
632
634
return Promise . resolve ( keys ) ;
633
635
} , { cacheKeyFn : cacheKey } ) ;
@@ -651,7 +653,8 @@ describe('Accepts options', () => {
651
653
} ) ;
652
654
653
655
it ( 'Allows priming the cache with an object key' , async ( ) => {
654
- const [ identityLoader , loadCalls ] = idLoader ( { cacheKeyFn : cacheKey } ) ;
656
+ const [ identityLoader , loadCalls ] =
657
+ idLoader < Obj , string > ( { cacheKeyFn : cacheKey } ) ;
655
658
656
659
const key1 = { id : 123 } ;
657
660
const key2 = { id : 123 } ;
@@ -693,7 +696,7 @@ describe('Accepts options', () => {
693
696
it ( 'Accepts a custom cache map implementation' , async ( ) => {
694
697
const aCustomMap = new SimpleMap ( ) ;
695
698
const identityLoadCalls = [ ] ;
696
- const identityLoader = new DataLoader ( keys => {
699
+ const identityLoader = new DataLoader < string , string > ( keys => {
697
700
identityLoadCalls . push ( keys ) ;
698
701
return Promise . resolve ( keys ) ;
699
702
} , { cacheMap : aCustomMap } ) ;
@@ -747,7 +750,7 @@ describe('Accepts options', () => {
747
750
describe ( 'It is resilient to job queue ordering' , ( ) => {
748
751
749
752
it ( 'batches loads occuring within promises' , async ( ) => {
750
- const [ identityLoader , loadCalls ] = idLoader ( ) ;
753
+ const [ identityLoader , loadCalls ] = idLoader < string > ( ) ;
751
754
752
755
await Promise . all ( [
753
756
identityLoader . load ( 'A' ) ,
@@ -767,19 +770,22 @@ describe('It is resilient to job queue ordering', () => {
767
770
768
771
it ( 'can call a loader from a loader' , async ( ) => {
769
772
const deepLoadCalls = [ ] ;
770
- const deepLoader = new DataLoader ( keys => {
773
+ const deepLoader = new DataLoader <
774
+ $ReadOnlyArray < string > ,
775
+ $ReadOnlyArray < string >
776
+ > ( keys => {
771
777
deepLoadCalls . push ( keys ) ;
772
778
return Promise . resolve ( keys ) ;
773
779
} ) ;
774
780
775
781
const aLoadCalls = [ ] ;
776
- const aLoader = new DataLoader ( keys => {
782
+ const aLoader = new DataLoader < string , string > ( keys => {
777
783
aLoadCalls . push ( keys ) ;
778
784
return deepLoader . load ( keys ) ;
779
785
} ) ;
780
786
781
787
const bLoadCalls = [ ] ;
782
- const bLoader = new DataLoader ( keys => {
788
+ const bLoader = new DataLoader < string , string > ( keys => {
783
789
bLoadCalls . push ( keys ) ;
784
790
return deepLoader . load ( keys ) ;
785
791
} ) ;
0 commit comments