* Loads a key, returning a `Promise` for the value represented by that key.
(key: K)
| 72 | * Loads a key, returning a `Promise` for the value represented by that key. |
| 73 | */ |
| 74 | load(key: K): Promise<V> { |
| 75 | if (key === null || key === undefined) { |
| 76 | throw new TypeError( |
| 77 | 'The loader.load() function must be called with a value, ' + |
| 78 | `but got: ${String(key)}.`, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | const batch = getCurrentBatch(this); |
| 83 | const cacheMap = this._cacheMap; |
| 84 | let cacheKey: ?C; |
| 85 | |
| 86 | // If caching and there is a cache-hit, return cached Promise. |
| 87 | if (cacheMap) { |
| 88 | cacheKey = this._cacheKeyFn(key); |
| 89 | const cachedPromise = cacheMap.get(cacheKey); |
| 90 | if (cachedPromise) { |
| 91 | const cacheHits = batch.cacheHits || (batch.cacheHits = []); |
| 92 | return new Promise(resolve => { |
| 93 | cacheHits.push(() => { |
| 94 | resolve(cachedPromise); |
| 95 | }); |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Otherwise, produce a new Promise for this key, and enqueue it to be |
| 101 | // dispatched along with the current batch. |
| 102 | batch.keys.push(key); |
| 103 | const promise = new Promise((resolve, reject) => { |
| 104 | batch.callbacks.push({ resolve, reject }); |
| 105 | }); |
| 106 | |
| 107 | // If caching, cache this promise. |
| 108 | if (cacheMap) { |
| 109 | cacheMap.set((cacheKey: any), promise); |
| 110 | } |
| 111 | |
| 112 | return promise; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Loads multiple keys, promising an array of values: |
no test coverage detected