From 0a1ea891722fa868b203681e3910b93833da8d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Tue, 15 Sep 2020 08:33:14 +0200 Subject: [PATCH 1/2] Add typings and test for priming with promise --- src/__tests__/dataloader.test.js | 16 ++++++++++++++++ src/index.d.ts | 2 +- src/index.js | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) 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 6e29ef6..c34434c 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -56,7 +56,7 @@ declare class DataLoader { * 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 | Error): this; } declare namespace DataLoader { diff --git a/src/index.js b/src/index.js index 6250df9..25eba07 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); From 17c23de5e5834fa17da82c57d104b89831f646c5 Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Mon, 4 Jul 2022 15:50:19 -0500 Subject: [PATCH 2/2] add changeset --- .changeset/lucky-poets-fail.md | 5 +++++ 1 file changed, 5 insertions(+) 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