Skip to content

Commit 76dcc22

Browse files
committed
refactor: improve polling mechanism for knowledge workflow action
1 parent 62b7ca1 commit 76dcc22

File tree

1 file changed

+28
-8
lines changed
  • ui/src/views/knowledge-workflow/component/action

1 file changed

+28
-8
lines changed

ui/src/views/knowledge-workflow/component/action/Result.vue

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,38 @@ const state = computed(() => {
2121
return 'PADDING'
2222
})
2323
const knowledge_action = ref<any>()
24+
let pollingTimer: any = null
25+
2426
const getKnowledgeWorkflowAction = () => {
25-
knowledgeApi.getWorkflowAction(props.knowledge_id, props.id).then((ok) => {
26-
knowledge_action.value = ok.data
27-
if (['SUCCESS', 'FAILURE', 'REVOKED'].includes(state.value)) {
28-
clearInterval(polling)
29-
}
30-
})
27+
knowledgeApi.getWorkflowAction(props.knowledge_id, props.id)
28+
.then((ok) => {
29+
knowledge_action.value = ok.data
30+
if (['SUCCESS', 'FAILURE', 'REVOKED'].includes(state.value)) {
31+
stopPolling()
32+
} else {
33+
// 请求完成后再设置下次轮询
34+
pollingTimer = setTimeout(getKnowledgeWorkflowAction, 2000)
35+
}
36+
})
37+
.catch(() => {
38+
// 错误时也继续轮询
39+
pollingTimer = setTimeout(getKnowledgeWorkflowAction, 2000)
40+
})
41+
}
42+
43+
const stopPolling = () => {
44+
if (pollingTimer) {
45+
clearTimeout(pollingTimer)
46+
pollingTimer = null
47+
}
3148
}
32-
const polling = setInterval(getKnowledgeWorkflowAction, 2000)
49+
50+
// 启动轮询
51+
getKnowledgeWorkflowAction()
3352
3453
onUnmounted(() => {
35-
clearInterval(polling)
54+
stopPolling()
3655
})
56+
3757
</script>
3858
<style lang="scss"></style>

0 commit comments

Comments
 (0)