From 2a1a729e8e17c56c11fec9e2d694cc64ab2da695 Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Wed, 21 Aug 2024 13:52:26 -0700 Subject: [PATCH] The subscriptions coalescer now publishes the final message of the inner iterator (#3132) # Summary What if a custom transport _returns_ its final value? Before this PR, the coalescer would not deliver the final message, if the `done` property and the `value` came in the same iteration. --- .../rpc-subscriptions-coalescer-test.ts | 17 ++++++++++++++++- .../src/rpc-subscriptions-coalescer.ts | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/rpc-subscriptions/src/__tests__/rpc-subscriptions-coalescer-test.ts b/packages/rpc-subscriptions/src/__tests__/rpc-subscriptions-coalescer-test.ts index f88dbce1855b..87ca87c3d77c 100644 --- a/packages/rpc-subscriptions/src/__tests__/rpc-subscriptions-coalescer-test.ts +++ b/packages/rpc-subscriptions/src/__tests__/rpc-subscriptions-coalescer-test.ts @@ -8,7 +8,7 @@ interface TestRpcSubscriptionNotifications { } describe('getRpcSubscriptionsWithSubscriptionCoalescing', () => { - let asyncGenerator: jest.Mock>; + let asyncGenerator: jest.Mock>; let createPendingSubscription: jest.Mock; let getDeduplicationKey: jest.Mock; let subscribe: jest.Mock; @@ -115,6 +115,21 @@ describe('getRpcSubscriptionsWithSubscriptionCoalescing', () => { await expect(messagePromiseA).resolves.toHaveProperty('value', 'hello'); await expect(messagePromiseB).resolves.toHaveProperty('value', 'hello'); }); + it('publishes the final message when the iterable returns', async () => { + expect.assertions(1); + asyncGenerator.mockImplementation( + // eslint-disable-next-line require-yield + async function* () { + return await Promise.resolve('hello'); + }, + ); + const iterable = await rpcSubscriptions + .thingNotifications({ payload: 'hello' }) + .subscribe({ abortSignal: new AbortController().signal }); + const iterator = iterable[Symbol.asyncIterator](); + const messagePromise = iterator.next(); + await expect(messagePromise).resolves.toHaveProperty('value', 'hello'); + }); it('aborting a subscription causes it to return', async () => { expect.assertions(1); asyncGenerator.mockImplementation(async function* () { diff --git a/packages/rpc-subscriptions/src/rpc-subscriptions-coalescer.ts b/packages/rpc-subscriptions/src/rpc-subscriptions-coalescer.ts index 1de90850b09e..bfcf66fa3448 100644 --- a/packages/rpc-subscriptions/src/rpc-subscriptions-coalescer.ts +++ b/packages/rpc-subscriptions/src/rpc-subscriptions-coalescer.ts @@ -108,7 +108,7 @@ export function getRpcSubscriptionsWithSubscriptionCoalescing