@@ -14,12 +14,12 @@ export type BatchLoadFn<K, V> =
14
14
15
15
// Optionally turn off batching or caching or provide a cache key function or a
16
16
// custom cache instance.
17
- export type Options < K , V > = {
17
+ export type Options < K , V , C = K > = {
18
18
batch ?: boolean ;
19
19
maxBatchSize ? : number ;
20
20
cache ? : boolean ;
21
- cacheKeyFn ?: ( key : any ) => any ;
22
- cacheMap ?: CacheMap < K , Promise < V >> ;
21
+ cacheKeyFn ?: ( key : K ) => C ;
22
+ cacheMap ?: CacheMap < C , Promise < V >> ;
23
23
} ;
24
24
25
25
// If a custom cache is provided, it must be of this type (a subset of ES6 Map).
@@ -40,10 +40,10 @@ export type CacheMap<K, V> = {
40
40
* different access permissions and consider creating a new instance per
41
41
* web request.
42
42
*/
43
- class DataLoader < K , V > {
43
+ class DataLoader < K , V , C = K > {
44
44
constructor (
45
45
batchLoadFn : BatchLoadFn < K , V > ,
46
- options ?: Options < K , V >
46
+ options ?: Options < K , V , C >
47
47
) {
48
48
if ( typeof batchLoadFn !== 'function' ) {
49
49
throw new TypeError (
@@ -59,8 +59,8 @@ class DataLoader<K, V> {
59
59
60
60
// Private
61
61
_batchLoadFn : BatchLoadFn < K , V > ;
62
- _options : ?Options < K , V > ;
63
- _promiseCache : CacheMap < K , Promise < V >> ;
62
+ _options : ?Options < K , V , C > ;
63
+ _promiseCache : CacheMap < C , Promise < V >> ;
64
64
_queue : LoaderQueue < K , V > ;
65
65
66
66
/**
@@ -78,8 +78,7 @@ class DataLoader<K, V> {
78
78
var options = this . _options ;
79
79
var shouldBatch = ! options || options . batch !== false ;
80
80
var shouldCache = ! options || options . cache !== false ;
81
- var cacheKeyFn = options && options . cacheKeyFn ;
82
- var cacheKey = cacheKeyFn ? cacheKeyFn ( key ) : key ;
81
+ var cacheKey = getCacheKey ( options , key ) ;
83
82
84
83
// If caching and there is a cache-hit, return cached Promise.
85
84
if ( shouldCache ) {
@@ -143,9 +142,8 @@ class DataLoader<K, V> {
143
142
* Clears the value at `key` from the cache, if it exists. Returns itself for
144
143
* method chaining.
145
144
*/
146
- clear ( key : K ) : DataLoader < K , V > {
147
- var cacheKeyFn = this . _options && this . _options . cacheKeyFn ;
148
- var cacheKey = cacheKeyFn ? cacheKeyFn ( key ) : key ;
145
+ clear ( key : K ) : this {
146
+ var cacheKey = getCacheKey ( this . _options , key ) ;
149
147
this . _promiseCache . delete ( cacheKey ) ;
150
148
return this ;
151
149
}
@@ -155,7 +153,7 @@ class DataLoader<K, V> {
155
153
* invalidations across this particular `DataLoader`. Returns itself for
156
154
* method chaining.
157
155
*/
158
- clearAll ( ) : DataLoader < K , V > {
156
+ clearAll ( ) : this {
159
157
this . _promiseCache . clear ( ) ;
160
158
return this ;
161
159
}
@@ -164,9 +162,8 @@ class DataLoader<K, V> {
164
162
* Adds the provided key and value to the cache. If the key already
165
163
* exists, no change is made. Returns itself for method chaining.
166
164
*/
167
- prime ( key : K , value : V ) : DataLoader < K , V > {
168
- var cacheKeyFn = this . _options && this . _options . cacheKeyFn ;
169
- var cacheKey = cacheKeyFn ? cacheKeyFn ( key ) : key ;
165
+ prime ( key : K , value : V ) : this {
166
+ var cacheKey = getCacheKey ( this . _options , key ) ;
170
167
171
168
// Only add the key if it does not already exist.
172
169
if ( this . _promiseCache . get ( cacheKey ) === undefined ) {
@@ -224,7 +221,7 @@ var resolvedPromise;
224
221
225
222
// Private: given the current state of a Loader instance, perform a batch load
226
223
// from its current queue.
227
- function dispatchQueue < K , V > ( loader : DataLoader < K , V > ) {
224
+ function dispatchQueue < K , V > ( loader : DataLoader < K , V , any > ) {
228
225
// Take the current loader queue, replacing it with an empty queue.
229
226
var queue = loader . _queue ;
230
227
loader . _queue = [ ] ;
@@ -245,7 +242,7 @@ function dispatchQueue<K, V>(loader: DataLoader<K, V>) {
245
242
}
246
243
247
244
function dispatchQueueBatch < K , V > (
248
- loader : DataLoader < K , V > ,
245
+ loader : DataLoader < K , V , any > ,
249
246
queue : LoaderQueue < K , V >
250
247
) {
251
248
// Collect all keys to be loaded in this dispatch
@@ -303,7 +300,7 @@ function dispatchQueueBatch<K, V>(
303
300
// Private: do not cache individual loads if the entire batch dispatch fails,
304
301
// but still reject each request so they do not hang.
305
302
function failedDispatch < K , V > (
306
- loader : DataLoader < K , V > ,
303
+ loader : DataLoader < K , V , any > ,
307
304
queue : LoaderQueue < K , V > ,
308
305
error : Error
309
306
) {
@@ -313,10 +310,19 @@ function failedDispatch<K, V>(
313
310
} ) ;
314
311
}
315
312
313
+ // Private: produce a cache key for a given key (and options)
314
+ function getCacheKey < K , V , C > (
315
+ options : ?Options < K , V , C > ,
316
+ key : K
317
+ ) : C {
318
+ var cacheKeyFn = options && options . cacheKeyFn ;
319
+ return cacheKeyFn ? cacheKeyFn ( key ) : ( key : any ) ;
320
+ }
321
+
316
322
// Private: given the DataLoader's options, produce a CacheMap to be used.
317
- function getValidCacheMap < K , V > (
318
- options : ?Options < K , V >
319
- ) : CacheMap < K , Promise < V >> {
323
+ function getValidCacheMap < K , V , C > (
324
+ options : ?Options < K , V , C >
325
+ ) : CacheMap < C , Promise < V >> {
320
326
var cacheMap = options && options . cacheMap ;
321
327
if ( ! cacheMap ) {
322
328
return new Map ( ) ;
0 commit comments