Skip to content

Commit

Permalink
feat(useSubscription): add skip option (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredyC authored and trojanowski committed Feb 27, 2019
1 parent 31d4f97 commit 8670f7f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ const { data, error, loading } = useSubscription(MY_SUBSCRIPTION, {
});
```

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.

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.

## useApolloClient

Expand Down
38 changes: 37 additions & 1 deletion src/__tests__/useSubscription-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Operation } from 'apollo-link';
import { MockSubscriptionLink } from 'apollo-link-mock';
import gql from 'graphql-tag';
import React from 'react';
import { cleanup, render } from 'react-testing-library';
import { act, cleanup, render } from 'react-testing-library';

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

expect(count).toBe(5);
});

it('should not subscribe if skip option is enabled', async () => {
const variables = { completed: true };

class MockSubscriptionLinkOverride extends MockSubscriptionLink {
request(req: Operation) {
expect(req.variables).toEqual(variables);
return super.request(req);
}
}

const link = new MockSubscriptionLinkOverride();

const client = createClient({ link });

const Component = () => {
const { data, loading, error } = useSubscription(
FILTERED_TASKS_SUBSCRIPTION,
{ variables, skip: true }
);
expect(loading).toBe(true);
expect(error).toBeUndefined();
expect(data).toBeUndefined();
return null;
};

render(
<ApolloProvider client={client}>
<Component />
</ApolloProvider>
);

act(() => {
link.simulateResult(complitedResults[0]);
});
});
4 changes: 4 additions & 0 deletions src/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface OnSubscriptionDataOptions<TData> {

export interface SubscriptionHookOptions<TData, TVariables>
extends Omit<SubscriptionOptions<TVariables>, 'query'> {
skip?: boolean;
onSubscriptionData?: OnSubscriptionData<TData>;
}

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

useEffect(
() => {
if (options.skip === true) {
return;
}
const subscription = client
.subscribe({
...options,
Expand Down

0 comments on commit 8670f7f

Please sign in to comment.