Skip to content

Add typings and test for priming with promise #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-poets-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dataloader": patch
---

Fix types for priming cache with promise
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 provided 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