Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(preact) - Update @urql/preact implementation to match React bindings #1008

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-penguins-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/preact': minor
---

Update `@urql/preact` implementation to match `urql` React implementation. Internally these changes should align behaviour and updates slightly, but outwardly no changes should be apparent apart from how some updates are scheduled.
8 changes: 8 additions & 0 deletions packages/preact-urql/src/hooks/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const initialState = {
fetching: false,
stale: false,
error: undefined,
data: undefined,
extensions: undefined,
operation: undefined,
};
50 changes: 0 additions & 50 deletions packages/preact-urql/src/hooks/useImmediateEffect.test.ts

This file was deleted.

29 changes: 0 additions & 29 deletions packages/preact-urql/src/hooks/useImmediateEffect.ts

This file was deleted.

54 changes: 0 additions & 54 deletions packages/preact-urql/src/hooks/useImmediateState.test.ts

This file was deleted.

45 changes: 0 additions & 45 deletions packages/preact-urql/src/hooks/useImmediateState.ts

This file was deleted.

51 changes: 28 additions & 23 deletions packages/preact-urql/src/hooks/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { useCallback } from 'preact/hooks';
import { DocumentNode } from 'graphql';
import { useState, useCallback, useRef, useEffect } from 'preact/hooks';
import { pipe, toPromise } from 'wonka';

import {
Operation,
OperationResult,
OperationContext,
CombinedError,
createRequest,
OperationResult,
Operation,
} from '@urql/core';

import { useClient } from '../context';
import { useImmediateState } from './useImmediateState';
import { initialState } from './useQuery';
import { initialState } from './constants';

export interface UseMutationState<T> {
fetching: boolean;
Expand All @@ -29,21 +30,17 @@ export type UseMutationResponse<T, V> = [
) => Promise<OperationResult<T>>
];

export const useMutation = <T = any, V = object>(
export function useMutation<T = any, V = object>(
query: DocumentNode | string
): UseMutationResponse<T, V> => {
): UseMutationResponse<T, V> {
const isMounted = useRef(true);
const client = useClient();

const [state, setState] = useImmediateState<UseMutationState<T>>({
...initialState,
});
const [state, setState] = useState<UseMutationState<T>>(initialState);

const executeMutation = useCallback(
(variables?: V, context?: Partial<OperationContext>) => {
setState({
...initialState,
fetching: true,
});
setState({ ...initialState, fetching: true });

return pipe(
client.executeMutation(
Expand All @@ -52,20 +49,28 @@ export const useMutation = <T = any, V = object>(
),
toPromise
).then(result => {
setState({
fetching: false,
stale: !!result.stale,
data: result.data,
error: result.error,
extensions: result.extensions,
operation: result.operation,
});
if (isMounted.current) {
setState({
fetching: false,
stale: !!result.stale,
data: result.data,
error: result.error,
extensions: result.extensions,
operation: result.operation,
});
}
return result;
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[client, query, setState]
);

useEffect(() => {
return () => {
isMounted.current = false;
};
}, []);

return [state, executeMutation];
};
}
27 changes: 10 additions & 17 deletions packages/preact-urql/src/hooks/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,20 +262,22 @@ describe('useQuery', () => {
unmount();
});

expect(start).toBeCalledTimes(1);
expect(unsubscribe).toBeCalledTimes(1);
expect(start).toBeCalledTimes(2);
expect(unsubscribe).toBeCalledTimes(2);
});
});

describe('active teardown', () => {
it('sets fetching to false when the source ends', () => {
client.executeQuery.mockReturnValueOnce(empty);
render(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
act(() => {
render(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props })],
})
);
});
expect(client.executeQuery).toHaveBeenCalled();
expect(state).toMatchObject({ fetching: false });
});
Expand Down Expand Up @@ -313,15 +315,6 @@ describe('useQuery', () => {
})
);

/**
* Call update twice for the change to be detected.
*/
rerender(
h(Provider, {
value: client as any,
children: [h(QueryUser, { ...props, pause: true })],
})
);
rerender(
h(Provider, {
value: client as any,
Expand Down
Loading