Skip to content

Commit c9c4b96

Browse files
no6vstocaaro
andauthored
refactor(api-graphql): set observers earlier to ensure handling unsubscription properly (#14290)
* refactor(api-graphql): set observers earlier to ensure handling unsubscription properly refs: aws-amplify/amplify-data#554 * Update to keep the set call closer to its original location --------- Co-authored-by: Aaron S <[email protected]> Co-authored-by: Aaron S. <[email protected]>
1 parent 429cf37 commit c9c4b96

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/api-graphql/__tests__/AWSAppSyncRealTimeProvider.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,28 @@ describe('AWSAppSyncRealTimeProvider', () => {
992992
expect(fakeWebSocketInterface.hasClosed).resolves.toBeUndefined();
993993
});
994994

995+
test('unsubscription message should be sent even if unsubscribe immediately', async () => {
996+
expect.assertions(1);
997+
998+
const sendUnsubscriptionMessageSpy = jest
999+
.spyOn(provider as any, '_sendUnsubscriptionMessage');
1000+
1001+
provider
1002+
.subscribe({
1003+
appSyncGraphqlEndpoint: 'ws://localhost:8080',
1004+
})
1005+
.subscribe({ error: () => {} })
1006+
.unsubscribe();
1007+
1008+
await fakeWebSocketInterface?.standardConnectionHandshake();
1009+
await fakeWebSocketInterface?.startAckMessage();
1010+
1011+
expect(sendUnsubscriptionMessageSpy).toHaveBeenNthCalledWith(
1012+
1,
1013+
fakeWebSocketInterface?.webSocket.subscriptionId,
1014+
);
1015+
});
1016+
9951017
test('failure to ack before timeout', async () => {
9961018
expect.assertions(1);
9971019

packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export abstract class AWSWebSocketProvider {
158158

159159
let subscriptionStartInProgress = false;
160160
const subscriptionId = amplifyUuid();
161+
161162
const startSubscription = () => {
162163
if (!subscriptionStartInProgress) {
163164
subscriptionStartInProgress = true;
@@ -396,9 +397,6 @@ export abstract class AWSWebSocketProvider {
396397
}) {
397398
const { query, variables } = options;
398399

399-
const { additionalCustomHeaders, libraryConfigHeaders } =
400-
await additionalHeadersFromOptions(options);
401-
402400
this.subscriptionObserverMap.set(subscriptionId, {
403401
observer,
404402
query: query ?? '',
@@ -407,6 +405,9 @@ export abstract class AWSWebSocketProvider {
407405
startAckTimeoutId: undefined,
408406
});
409407

408+
const { additionalCustomHeaders, libraryConfigHeaders } =
409+
await additionalHeadersFromOptions(options);
410+
410411
const serializedSubscriptionMessage =
411412
await this._prepareSubscriptionPayload({
412413
options,

0 commit comments

Comments
 (0)