Skip to content

Commit a71a28f

Browse files
committed
ui: Fix autogenview to cancel and discard invalid and stale API requests
This introduces change in AutogenView to do the following: - Cancel old API requests when the route is changed (between different list view or list and resource views) - In the handler of an API call made in the fetchData(), to check if the json received is for the current route/id or apiName - Add console log for debugging which may be removed if this passes code review and testing Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent feb9509 commit a71a28f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

ui/src/views/AutogenView.vue

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ import { ref, reactive, toRaw } from 'vue'
442442
import { api } from '@/api'
443443
import { mixinDevice } from '@/utils/mixin.js'
444444
import { genericCompare } from '@/utils/sort.js'
445+
import { sourceToken } from '@/utils/request'
445446
import store from '@/store'
446447
import eventBus from '@/config/eventBus'
447448
@@ -621,6 +622,9 @@ export default {
621622
next()
622623
},
623624
beforeRouteLeave (to, from, next) {
625+
console.log('DEBUG - Due to route change, cancelling any on-going API request', this.apiName)
626+
sourceToken.cancel()
627+
sourceToken.init()
624628
this.currentPath = this.$route.fullPath
625629
next()
626630
},
@@ -918,19 +922,37 @@ export default {
918922
break
919923
}
920924
}
921-
this.itemCount = 0
925+
var apiItemCount = 0
922926
for (const key in json[responseName]) {
923927
if (key === 'count') {
924-
this.itemCount = json[responseName].count
928+
apiItemCount = json[responseName].count
925929
continue
926930
}
927931
objectName = key
928932
break
929933
}
934+
935+
if ((this.apiName.toLowerCase() + 'response') !== responseName.toLowerCase()) {
936+
console.log('DEBUG - Discarding API response as expected API response key does not match response name:', responseName)
937+
return
938+
}
939+
if ('id' in this.$route.params && this.$route.params.id !== params.id) {
940+
console.log('DEBUG - Discarding API response as its `id` does not match $route.param.id')
941+
return
942+
}
943+
if (this.dataView && apiItemCount > 1) {
944+
console.log('DEBUG - Discarding API response as got more than one item in data view', this.$route.params, this.items)
945+
return
946+
}
947+
930948
this.items = json[responseName][objectName]
931949
if (!this.items || this.items.length === 0) {
932950
this.items = []
933951
}
952+
this.itemCount = apiItemCount
953+
if (this.itemCount !== this.items.length) {
954+
console.log('WARN: API items length does not match the API return count, something is wrong')
955+
}
934956
935957
if (['listTemplates', 'listIsos'].includes(this.apiName) && this.items.length > 1) {
936958
this.items = [...new Map(this.items.map(x => [x.id, x])).values()]
@@ -979,6 +1001,10 @@ export default {
9791001
}
9801002
}
9811003
}).catch(error => {
1004+
if (!error || !error.message) {
1005+
console.log('API request likely got cancelled due to route change:', this.apiName)
1006+
return
1007+
}
9821008
if ([401].includes(error.response.status)) {
9831009
return
9841010
}

0 commit comments

Comments
 (0)