From fae38f14702e925d1e59051d7e5cb3a9a78bfde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Mon, 4 Jul 2022 22:51:11 +0200 Subject: [PATCH] fix: add typings and test for priming with promise (#252) * Add typings and test for priming with promise * add changeset Co-authored-by: Saihajpreet Singh --- .changeset/lucky-poets-fail.md | 5 +++++ src/__tests__/dataloader.test.js | 16 ++++++++++++++++ src/index.d.ts | 2 +- src/index.js | 2 +- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 .changeset/lucky-poets-fail.md diff --git a/.changeset/lucky-poets-fail.md b/.changeset/lucky-poets-fail.md new file mode 100644 index 0000000..ad4715a --- /dev/null +++ b/.changeset/lucky-poets-fail.md @@ -0,0 +1,5 @@ +--- +"dataloader": patch +--- + +Fix types for priming cache with promise diff --git a/src/__tests__/dataloader.test.js b/src/__tests__/dataloader.test.js index 47b2059..e53483f 100644 --- a/src/__tests__/dataloader.test.js +++ b/src/__tests__/dataloader.test.js @@ -380,6 +380,22 @@ describe('Primary API', () => { expect(loadCalls).toEqual([ [ 'B' ] ]); }); + it('allows priming the cache with a promise', async () => { + const [ identityLoader, loadCalls ] = idLoader(); + + 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', () => { diff --git a/src/index.d.ts b/src/index.d.ts index ee3c8b6..6709279 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -56,7 +56,7 @@ declare class DataLoader { * 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 | Error): this; } declare namespace DataLoader { diff --git a/src/index.js b/src/index.js index 85689c8..87dade9 100644 --- a/src/index.js +++ b/src/index.js @@ -178,7 +178,7 @@ class DataLoader { * * 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 | Error): this { var cacheMap = this._cacheMap; if (cacheMap) { var cacheKey = this._cacheKeyFn(key);