@@ -60,7 +60,7 @@ class DataLoader<K, V, C = K> {
60
60
// Private
61
61
_batchLoadFn : BatchLoadFn < K , V > ;
62
62
_options : ?Options < K , V , C > ;
63
- _promiseCache : CacheMap < C , Promise < V >> ;
63
+ _promiseCache : ? CacheMap < C , Promise < V >> ;
64
64
_queue : LoaderQueue < K , V > ;
65
65
66
66
/**
@@ -77,12 +77,12 @@ class DataLoader<K, V, C = K> {
77
77
// Determine options
78
78
var options = this . _options ;
79
79
var shouldBatch = ! options || options . batch !== false ;
80
- var shouldCache = ! options || options . cache !== false ;
80
+ var cache = this . _promiseCache ;
81
81
var cacheKey = getCacheKey ( options , key ) ;
82
82
83
83
// If caching and there is a cache-hit, return cached Promise.
84
- if ( shouldCache ) {
85
- var cachedPromise = this . _promiseCache . get ( cacheKey ) ;
84
+ if ( cache ) {
85
+ var cachedPromise = cache . get ( cacheKey ) ;
86
86
if ( cachedPromise ) {
87
87
return cachedPromise ;
88
88
}
@@ -108,8 +108,8 @@ class DataLoader<K, V, C = K> {
108
108
} ) ;
109
109
110
110
// If caching, cache this promise.
111
- if ( shouldCache ) {
112
- this . _promiseCache . set ( cacheKey , promise ) ;
111
+ if ( cache ) {
112
+ cache . set ( cacheKey , promise ) ;
113
113
}
114
114
115
115
return promise ;
@@ -148,8 +148,11 @@ class DataLoader<K, V, C = K> {
148
148
* method chaining.
149
149
*/
150
150
clear ( key : K ) : this {
151
- var cacheKey = getCacheKey ( this . _options , key ) ;
152
- this . _promiseCache . delete ( cacheKey ) ;
151
+ var cache = this . _promiseCache ;
152
+ if ( cache ) {
153
+ var cacheKey = getCacheKey ( this . _options , key ) ;
154
+ cache . delete ( cacheKey ) ;
155
+ }
153
156
return this ;
154
157
}
155
158
@@ -159,7 +162,10 @@ class DataLoader<K, V, C = K> {
159
162
* method chaining.
160
163
*/
161
164
clearAll ( ) : this {
162
- this . _promiseCache . clear ( ) ;
165
+ var cache = this . _promiseCache ;
166
+ if ( cache ) {
167
+ cache . clear ( ) ;
168
+ }
163
169
return this ;
164
170
}
165
171
@@ -168,19 +174,21 @@ class DataLoader<K, V, C = K> {
168
174
* exists, no change is made. Returns itself for method chaining.
169
175
*/
170
176
prime ( key : K , value : V ) : this {
171
- var cacheKey = getCacheKey ( this . _options , key ) ;
172
-
173
- // Only add the key if it does not already exist.
174
- if ( this . _promiseCache . get ( cacheKey ) === undefined ) {
175
- // Cache a rejected promise if the value is an Error, in order to match
176
- // the behavior of load(key).
177
- var promise = value instanceof Error ?
178
- Promise . reject ( value ) :
179
- Promise . resolve ( value ) ;
180
-
181
- this . _promiseCache . set ( cacheKey , promise ) ;
177
+ var cache = this . _promiseCache ;
178
+ if ( cache ) {
179
+ var cacheKey = getCacheKey ( this . _options , key ) ;
180
+
181
+ // Only add the key if it does not already exist.
182
+ if ( cache . get ( cacheKey ) === undefined ) {
183
+ // Cache a rejected promise if the value is an Error, in order to match
184
+ // the behavior of load(key).
185
+ var promise = value instanceof Error ?
186
+ Promise . reject ( value ) :
187
+ Promise . resolve ( value ) ;
188
+
189
+ cache . set ( cacheKey , promise ) ;
190
+ }
182
191
}
183
-
184
192
return this ;
185
193
}
186
194
}
@@ -327,7 +335,11 @@ function getCacheKey<K, V, C>(
327
335
// Private: given the DataLoader's options, produce a CacheMap to be used.
328
336
function getValidCacheMap < K , V , C > (
329
337
options : ?Options < K , V , C >
330
- ) : CacheMap < C , Promise < V >> {
338
+ ) : ?CacheMap < C , Promise < V >> {
339
+ var shouldCache = ! options || options . cache !== false ;
340
+ if ( ! shouldCache ) {
341
+ return null ;
342
+ }
331
343
var cacheMap = options && options . cacheMap ;
332
344
if ( ! cacheMap ) {
333
345
return new Map ( ) ;
0 commit comments