Skip to content

Commit

Permalink
Add typings and test for priming with promise
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Sep 15, 2020
1 parent 249b2b9 commit 0a1ea89
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/dataloader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ describe('Primary API', () => {
expect(loadCalls).toEqual([ [ 'B' ] ]);
});

it('allows priming the cache with a promise', async () => {
const [ identityLoader, loadCalls ] = idLoader<string>();

identityLoader.prime('A', Promise.resolve('A'));

const [ a, b ] = await Promise.all([
identityLoader.load('A'),
identityLoader.load('B')
]);

expect(a).toBe('A');
expect(b).toBe('B');

expect(loadCalls).toEqual([ [ 'B' ] ]);
});

});

describe('Represents Errors', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ declare class DataLoader<K, V, C = K> {
* Adds the provied key and value to the cache. If the key already exists, no
* change is made. Returns itself for method chaining.
*/
prime(key: K, value: V | Error): this;
prime(key: K, value: V | PromiseLike<V> | Error): this;
}

declare namespace DataLoader {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class DataLoader<K, V, C = K> {
*
* To prime the cache with an error at a key, provide an Error instance.
*/
prime(key: K, value: V | Error): this {
prime(key: K, value: V | Promise<V> | Error): this {
var cacheMap = this._cacheMap;
if (cacheMap) {
var cacheKey = this._cacheKeyFn(key);
Expand Down

0 comments on commit 0a1ea89

Please sign in to comment.