Skip to content

Commit ad2ce11

Browse files
author
Guillaume Chau
committed
Better loading state handling
1 parent b5b16dd commit ad2ce11

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/smart-apollo.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export class SmartQuery extends SmartApollo {
149149
'throttle',
150150
'debounce',
151151
]
152+
loading = false
152153

153154
constructor (vm, key, options, autostart = true) {
154155
// Options object callback
@@ -196,15 +197,19 @@ export class SmartQuery extends SmartApollo {
196197
})
197198
}
198199

200+
this.maySetLoading()
201+
202+
super.executeApollo(variables)
203+
}
204+
205+
maySetLoading (force = false) {
199206
const currentResult = this.observer.currentResult()
200-
if (currentResult.loading) {
207+
if (force || currentResult.loading) {
201208
if (!this.loading) {
202209
this.applyLoadingModifier(1)
203210
}
204211
this.loading = true
205212
}
206-
207-
super.executeApollo(variables)
208213
}
209214

210215
nextResult (result) {
@@ -253,6 +258,7 @@ export class SmartQuery extends SmartApollo {
253258

254259
fetchMore (...args) {
255260
if (this.observer) {
261+
this.maySetLoading(true)
256262
return this.observer.fetchMore(...args)
257263
}
258264
}
@@ -268,21 +274,27 @@ export class SmartQuery extends SmartApollo {
268274
refetch (variables) {
269275
variables && (this.options.variables = variables)
270276
if (this.observer) {
271-
return this.observer.refetch(variables)
277+
const result = this.observer.refetch(variables)
278+
this.maySetLoading()
279+
return result
272280
}
273281
}
274282

275283
setVariables (variables, tryFetch) {
276284
this.options.variables = variables
277285
if (this.observer) {
278-
return this.observer.setVariables(variables, tryFetch)
286+
const result = this.observer.setVariables(variables, tryFetch)
287+
this.maySetLoading()
288+
return result
279289
}
280290
}
281291

282292
setOptions (options) {
283293
Object.assign(this.options, options)
284294
if (this.observer) {
285-
return this.observer.setOptions(options)
295+
const result = this.observer.setOptions(options)
296+
this.maySetLoading()
297+
return result
286298
}
287299
}
288300

0 commit comments

Comments
 (0)