Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix/dupe teardown subscription (#542)

* avoid duplicate network-requests for subscriptions (prevent getCurrentValue of contacting server)

* add changeset

* save some bytes

* Update selfish-scissors-float.md

authored by

Jovi De Croock and committed by
GitHub
8877e0fb a1935c98

+27 -15
+5
.changeset/selfish-scissors-float.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Adds a one-tick delay to the subscriptionExchange to prevent unnecessary early tear downs.
+3 -1
packages/core/src/exchanges/subscription.test.ts
··· 47 47 expect(unsubscribe).toHaveBeenCalled(); 48 48 }); 49 49 50 - it('should tear down the operation if the source subscription ends', () => { 50 + it('should tear down the operation if the source subscription ends', async () => { 51 51 const reexecuteOperation = jest.fn(); 52 52 const unsubscribe = jest.fn(); 53 53 ··· 68 68 subscriptionExchange({ forwardSubscription })(exchangeArgs), 69 69 publish 70 70 ); 71 + 72 + await Promise.resolve(); 71 73 72 74 expect(unsubscribe).not.toHaveBeenCalled(); 73 75 expect(reexecuteOperation).toHaveBeenCalled();
+19 -14
packages/core/src/exchanges/subscription.ts
··· 73 73 74 74 return make<OperationResult>(({ next, complete }) => { 75 75 let isComplete = false; 76 + let sub; 76 77 77 - const sub = observableish.subscribe({ 78 - next: result => next(makeResult(operation, result)), 79 - error: err => next(makeErrorResult(operation, err)), 80 - complete: () => { 81 - if (!isComplete) { 82 - isComplete = true; 83 - client.reexecuteOperation({ 84 - ...operation, 85 - operationName: 'teardown', 86 - }); 78 + Promise.resolve().then(() => { 79 + if (isComplete) return; 80 + 81 + sub = observableish.subscribe({ 82 + next: result => next(makeResult(operation, result)), 83 + error: err => next(makeErrorResult(operation, err)), 84 + complete: () => { 85 + if (!isComplete) { 86 + isComplete = true; 87 + client.reexecuteOperation({ 88 + ...operation, 89 + operationName: 'teardown', 90 + }); 87 91 88 - complete(); 89 - } 90 - }, 92 + complete(); 93 + } 94 + }, 95 + }) 91 96 }); 92 97 93 98 return () => { 94 99 isComplete = true; 95 - sub.unsubscribe(); 100 + if (sub) sub.unsubscribe(); 96 101 }; 97 102 }); 98 103 };