Skip to content

Commit 030a4b8

Browse files
Merge pull request #491 from hamiltoes/feature/use-get-lazy
fix: useGet lazy
2 parents 0af97a1 + 9cd715c commit 030a4b8

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

docs/composition-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ And here's a look at each individual property:
281281
- `params` is a FeathersJS Params object OR a Composition API `ref` (or `computed`, since they return a `ref` instance) which returns a Params object.
282282
- Unlike the `useFind` utility, `useGet` does not currently have built-in debouncing.
283283
- `queryWhen` must be a `computed` property which returns a `boolean`. It provides a logical separation for preventing API requests apart from `null` in the `id`.
284-
- `lazy`, which is `true` by default, determines if the internal `watch` should fire immediately. By default a single query will be performed, independent of the watchers. Set `lazy: false` and the watchers on the `id` and `params` will fire immediately (currently this will cause duplicate queries to be performed, so it's not recommended).
284+
- `lazy`, which is `false` by default, determines if the internal `watch` should fire immediately. Set `lazy: true` and the query will not fire immediately. It will only fire on subsequent changes to the `id` or `params`.
285285
286286
### Returned Attributes
287287

src/useGet.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function get(options: UseGetOptions): UseGetData {
4646
params: null,
4747
queryWhen: computed((): boolean => true),
4848
local: false,
49-
lazy: true
49+
lazy: false
5050
}
5151
const { model, id, params, queryWhen, local, lazy } = Object.assign(
5252
{},
@@ -112,25 +112,16 @@ export default function get(options: UseGetOptions): UseGetData {
112112
}
113113
}
114114

115-
watch(
115+
watch([
116116
() => getId(),
117-
id => {
118-
get(id, getParams())
119-
},
120-
{ lazy }
121-
)
122-
watch(
123117
() => getParams(),
124-
params => {
125-
get(getId(), params)
118+
],
119+
([id, params]) => {
120+
get(id as string | number, params as Params)
126121
},
127122
{ lazy }
128123
)
129124

130-
if (lazy) {
131-
get(id, getParams())
132-
}
133-
134125
return {
135126
servicePath,
136127
...toRefs(state),

test/use/get.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Vuex from 'vuex'
1818
import { mount, shallowMount } from '@vue/test-utils'
1919
import InstrumentComponent from './InstrumentComponent'
2020
import { computed, isRef } from '@vue/composition-api'
21+
import { HookContext } from '@feathersjs/feathers'
2122
jsdom()
2223
require('events').EventEmitter.prototype._maxListeners = 100
2324

@@ -130,7 +131,7 @@ describe('use/get', function() {
130131
const { Instrument } = makeContext()
131132

132133
const id = null
133-
const instrumentData = useGet({ model: Instrument, id, lazy: true })
134+
const instrumentData = useGet({ model: Instrument, id })
134135
const { hasBeenRequested } = instrumentData
135136

136137
assert(isRef(hasBeenRequested))
@@ -151,4 +152,42 @@ describe('use/get', function() {
151152

152153
assert(hasBeenRequested.value === false, 'no request after get')
153154
})
155+
156+
it('API only hit once on initial render', async function() {
157+
const { makeServicePlugin, BaseModel } = feathersVuex(feathersClient, {
158+
serverAlias: 'useGet'
159+
})
160+
161+
class Dohickey extends BaseModel {
162+
public static modelName = 'Dohickey'
163+
}
164+
165+
const servicePath = 'dohickies'
166+
const store = new Vuex.Store({
167+
plugins: [
168+
makeServicePlugin({
169+
Model: Dohickey,
170+
servicePath,
171+
service: feathersClient.service(servicePath)
172+
})
173+
]
174+
})
175+
176+
let getCalls = 0
177+
feathersClient.service(servicePath).hooks({
178+
before: {
179+
get: [
180+
(ctx: HookContext) => {
181+
getCalls += 1
182+
ctx.result = { id: ctx.id }
183+
}
184+
]
185+
}
186+
})
187+
188+
useGet({ model: Dohickey, id: 42 })
189+
await new Promise(resolve => setTimeout(resolve, 100))
190+
191+
assert(getCalls === 1, '`get` called once')
192+
})
154193
})

0 commit comments

Comments
 (0)