Skip to content

Commit 3e031f2

Browse files
committed
remove cache hits
1 parent 37768bb commit 3e031f2

File tree

2 files changed

+3
-24
lines changed

2 files changed

+3
-24
lines changed

src/__tests__/dataloader.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Primary API', () => {
141141
expect(loadCalls).toEqual([['a', 'b', 'c']]);
142142
});
143143

144-
it('batches cached requests', async () => {
144+
it.skip('batches cached requests', async () => {
145145
const loadCalls: ReadonlyArray<number>[] = [];
146146
let resolveBatch = () => {};
147147
const identityLoader = new DataLoader<number, number>(keys => {
@@ -186,7 +186,7 @@ describe('Primary API', () => {
186186
expect(loadCalls).toEqual([[2]]);
187187
});
188188

189-
it('max batch size respects cached results', async () => {
189+
it.skip('max batch size respects cached results', async () => {
190190
const loadCalls: ReadonlyArray<number>[] = [];
191191
let resolveBatch = () => {};
192192
const identityLoader = new DataLoader<number, number>(

src/index.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ class DataLoader<K, V, C = K> {
7171

7272
// If there's nothing to load, resolve any cache hits and return early.
7373
if (batch.keys.length === 0) {
74-
resolveCacheHits(batch);
7574
return;
7675
}
7776

@@ -125,9 +124,6 @@ class DataLoader<K, V, C = K> {
125124
);
126125
}
127126

128-
// Resolve all cache hits in the same micro-task as freshly loaded values.
129-
resolveCacheHits(batch);
130-
131127
// Step through values, resolving or rejecting each Promise in the batch.
132128
for (let i = 0; i < batch.callbacks.length; i++) {
133129
const value = values[i]!;
@@ -178,8 +174,6 @@ class DataLoader<K, V, C = K> {
178174
// Private: do not cache individual loads if the entire batch dispatch fails,
179175
// but still reject each request so they do not hang.
180176
private failedDispatch(batch: Batch<K, V>, error: Error) {
181-
// Cache hits are resolved, even though the batch failed.
182-
resolveCacheHits(batch);
183177
for (let i = 0; i < batch.keys.length; i++) {
184178
this.clear(batch.keys[i]!);
185179
batch.callbacks[i]!.reject(error);
@@ -205,12 +199,7 @@ class DataLoader<K, V, C = K> {
205199
cacheKey = this._cacheKeyFn(key);
206200
const cachedPromise = cacheMap.get(cacheKey);
207201
if (cachedPromise) {
208-
const cacheHits = batch.cacheHits || (batch.cacheHits = []);
209-
return new Promise(resolve => {
210-
cacheHits.push(() => {
211-
resolve(cachedPromise);
212-
});
213-
});
202+
return cachedPromise;
214203
}
215204
}
216205

@@ -377,18 +366,8 @@ type Batch<K, V> = {
377366
resolve: (value: V) => void;
378367
reject: (error: Error) => void;
379368
}>;
380-
cacheHits?: Array<() => void>;
381369
};
382370

383-
// Private: Resolves the Promises for any cache hits in this batch.
384-
function resolveCacheHits<K, V>(batch: Batch<K, V>) {
385-
if (batch.cacheHits) {
386-
for (let i = 0; i < batch.cacheHits.length; i++) {
387-
batch.cacheHits[i]!();
388-
}
389-
}
390-
}
391-
392371
// Private: given the DataLoader's options, produce a valid max batch size.
393372
function getValidMaxBatchSize<K, V, C>(options?: Options<K, V, C>): number {
394373
const shouldBatch = !options || options.batch !== false;

0 commit comments

Comments
 (0)