Skip to content

Commit cdcb753

Browse files
committed
Simplify useFind
- Moves the defaults into the `useFind` function. - Removes properties from the returned object that are provided in the arguments. There’s no need to pass back the exact things that were passed in.
1 parent 07a8385 commit cdcb753

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/useFind.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,11 @@ import {
1313
import { getQueryInfo, getItemsFromQueryInfo, Params } from './utils'
1414
import debounce from 'lodash/debounce'
1515

16-
const defaults = {
17-
model: null,
18-
params: null,
19-
queryWhen: (): boolean => true,
20-
qid: 'default',
21-
local: false,
22-
lazy: false
23-
}
24-
2516
interface UseFindOptions {
2617
model: Function
2718
params: Params | Ref<Params>
2819
fetchParams?: Params | Ref<Params>
29-
name?: string
30-
queryWhen?: Function
20+
queryWhen?: Ref<Function>
3121
qid?: string
3222
lazy?: boolean
3323
}
@@ -50,15 +40,20 @@ interface UseFindData {
5040
isLocal: Ref<boolean>
5141
qid: Ref<string>
5242
debounceTime: Ref<number>
53-
find: Function
54-
findInStore: Function
5543
latestQuery: Ref<object>
5644
paginationData: Ref<object>
57-
queryWhen: Ref<boolean>
5845
error: Ref<Error>
5946
}
6047

6148
export default function find(options: UseFindOptions): UseFindData {
49+
const defaults = {
50+
model: null,
51+
params: null,
52+
qid: 'default',
53+
queryWhen: computed((): boolean => true),
54+
local: false,
55+
lazy: false
56+
}
6257
const { model, params, queryWhen, qid, local, lazy } = Object.assign(
6358
{},
6459
defaults,
@@ -122,7 +117,6 @@ export default function find(options: UseFindOptions): UseFindData {
122117
return model.findInStore(getterParams).data
123118
}
124119
}),
125-
queryWhen: computed<boolean>(() => queryWhen()),
126120
paginationData: computed(() => {
127121
return model.store.state[model.servicePath].pagination
128122
}),
@@ -131,7 +125,7 @@ export default function find(options: UseFindOptions): UseFindData {
131125

132126
function find<T>(params: Params): T {
133127
params = isRef(params) ? params.value : params
134-
if (computes.queryWhen.value) {
128+
if (queryWhen.value && !state.isLocal) {
135129
state.isFindPending = true
136130
state.haveBeenRequestedOnce = true
137131

@@ -181,8 +175,6 @@ export default function find(options: UseFindOptions): UseFindData {
181175

182176
return {
183177
...computes,
184-
...toRefs(state),
185-
find: model.find,
186-
findInStore: model.findInStore
178+
...toRefs(state)
187179
}
188180
}

0 commit comments

Comments
 (0)