Skip to content

Commit 4847848

Browse files
authored
useController() takes configuration options (#1179)
- options object makes it more discoverable and explicit what is being set while allowing for potential future options - add action creators - createInvalidate - createReset BREAKING CHANGE: useController(true) -> useController({ throttle: true })
1 parent e11b59c commit 4847848

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { actionTypes } from '@rest-hooks/core';
2+
import type { EndpointInterface } from '@rest-hooks/endpoint';
3+
import type { InvalidateAction } from '@rest-hooks/core';
4+
5+
export default function createInvalidate<E extends EndpointInterface>(
6+
endpoint: E,
7+
{ args }: { args: readonly [...Parameters<E>] },
8+
): InvalidateAction {
9+
return {
10+
type: actionTypes.INVALIDATE_TYPE,
11+
meta: {
12+
key: endpoint.key(...args),
13+
},
14+
};
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { actionTypes } from '@rest-hooks/core';
2+
import type { ResetAction } from '@rest-hooks/core';
3+
4+
export default function createReset(): ResetAction {
5+
return {
6+
type: actionTypes.RESET_TYPE,
7+
date: new Date(),
8+
};
9+
}

packages/experimental/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export { default as useFetcher } from './useFetcher';
22
export { default as useController } from './useController';
33
export type { Controller } from './useController';
44
export { default as createFetch } from './createFetch';
5+
export { default as createInvalidate } from './createFetch';
6+
export { default as createReset } from './createFetch';
57
export { default as Resource } from './rest/Resource';
68
export { default as BaseResource } from './rest/BaseResource';
79
import { schema, Entity } from '@rest-hooks/endpoint';

packages/experimental/src/useController.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { EndpointInterface } from '@rest-hooks/endpoint';
22
import { DispatchContext } from '@rest-hooks/core';
33
import { useContext, useCallback } from 'react';
4-
import { actionTypes } from '@rest-hooks/core';
54

65
import createFetch from './createFetch';
6+
import createInvalidate from './createInvalidate';
7+
import createReset from './createReset';
78
import { UpdateFunction } from './types';
89

910
/**
1011
* Imperative control of Rest Hooks store
1112
* @see https://resthooks.io/docs/api/useController
1213
*/
13-
export default function useController(throttle = false): Controller {
14+
export default function useController({
15+
throttle = false,
16+
}: {
17+
throttle?: boolean;
18+
} = {}): Controller {
1419
const dispatch = useContext(DispatchContext);
1520

1621
// TODO: elevate into CacheProvider and simply useContext
@@ -36,22 +41,17 @@ export default function useController(throttle = false): Controller {
3641
...args: readonly [...Parameters<E>] | readonly [null]
3742
) =>
3843
args[0] !== null
39-
? dispatch({
40-
type: actionTypes.INVALIDATE_TYPE,
41-
meta: {
42-
key: endpoint.key(...args),
43-
},
44-
})
44+
? dispatch(
45+
createInvalidate(endpoint, {
46+
args: args as readonly [...Parameters<E>],
47+
}),
48+
)
4549
: Promise.resolve(),
4650
[dispatch],
4751
);
4852

4953
const resetEntireStore = useCallback(
50-
() =>
51-
dispatch({
52-
type: actionTypes.RESET_TYPE,
53-
date: new Date(),
54-
}),
54+
() => dispatch(createReset()),
5555
[dispatch],
5656
);
5757

packages/experimental/src/useFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export default function useFetcher(
1313
endpoint: E,
1414
...args: readonly [...Parameters<E>]
1515
) => ReturnType<E> {
16-
return useController(throttle).fetch;
16+
return useController({ throttle }).fetch;
1717
}

0 commit comments

Comments
 (0)