Skip to content

Commit 8670f7f

Browse files
FredyCtrojanowski
authored andcommitted
feat(useSubscription): add skip option (#98)
1 parent 31d4f97 commit 8670f7f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ const { data, error, loading } = useSubscription(MY_SUBSCRIPTION, {
275275
});
276276
```
277277

278+
In some cases you might want to subscribe only after you have all the information available, eg. only when user has selected necessary filters. Since hooks cannot be used conditionally, it would lead to unnecessary complicated patterns.
279+
280+
Instead, you can use the `skip` option which turns the subsciption dormant until toggled again. It will also unsubscribe if there was any previous subscription active and throw away previous result.
278281

279282
## useApolloClient
280283

src/__tests__/useSubscription-test.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Operation } from 'apollo-link';
33
import { MockSubscriptionLink } from 'apollo-link-mock';
44
import gql from 'graphql-tag';
55
import React from 'react';
6-
import { cleanup, render } from 'react-testing-library';
6+
import { act, cleanup, render } from 'react-testing-library';
77

88
import { ApolloProvider, useSubscription } from '..';
99
import createClient from '../__testutils__/createClient';
@@ -327,3 +327,39 @@ it('should re-subscription if variables have changed', async () => {
327327

328328
expect(count).toBe(5);
329329
});
330+
331+
it('should not subscribe if skip option is enabled', async () => {
332+
const variables = { completed: true };
333+
334+
class MockSubscriptionLinkOverride extends MockSubscriptionLink {
335+
request(req: Operation) {
336+
expect(req.variables).toEqual(variables);
337+
return super.request(req);
338+
}
339+
}
340+
341+
const link = new MockSubscriptionLinkOverride();
342+
343+
const client = createClient({ link });
344+
345+
const Component = () => {
346+
const { data, loading, error } = useSubscription(
347+
FILTERED_TASKS_SUBSCRIPTION,
348+
{ variables, skip: true }
349+
);
350+
expect(loading).toBe(true);
351+
expect(error).toBeUndefined();
352+
expect(data).toBeUndefined();
353+
return null;
354+
};
355+
356+
render(
357+
<ApolloProvider client={client}>
358+
<Component />
359+
</ApolloProvider>
360+
);
361+
362+
act(() => {
363+
link.simulateResult(complitedResults[0]);
364+
});
365+
});

src/useSubscription.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface OnSubscriptionDataOptions<TData> {
2222

2323
export interface SubscriptionHookOptions<TData, TVariables>
2424
extends Omit<SubscriptionOptions<TVariables>, 'query'> {
25+
skip?: boolean;
2526
onSubscriptionData?: OnSubscriptionData<TData>;
2627
}
2728

@@ -57,6 +58,9 @@ export function useSubscription<TData = any, TVariables = OperationVariables>(
5758

5859
useEffect(
5960
() => {
61+
if (options.skip === true) {
62+
return;
63+
}
6064
const subscription = client
6165
.subscribe({
6266
...options,

0 commit comments

Comments
 (0)