refactor: improve polling mechanism for knowledge workflow action

This commit is contained in:
CaptainB 2025-11-26 09:41:20 +08:00
parent 62b7ca189b
commit 76dcc22ae6

View File

@ -21,18 +21,38 @@ const state = computed(() => {
return 'PADDING'
})
const knowledge_action = ref<any>()
let pollingTimer: any = null
const getKnowledgeWorkflowAction = () => {
knowledgeApi.getWorkflowAction(props.knowledge_id, props.id).then((ok) => {
knowledge_action.value = ok.data
if (['SUCCESS', 'FAILURE', 'REVOKED'].includes(state.value)) {
clearInterval(polling)
}
})
knowledgeApi.getWorkflowAction(props.knowledge_id, props.id)
.then((ok) => {
knowledge_action.value = ok.data
if (['SUCCESS', 'FAILURE', 'REVOKED'].includes(state.value)) {
stopPolling()
} else {
//
pollingTimer = setTimeout(getKnowledgeWorkflowAction, 2000)
}
})
.catch(() => {
//
pollingTimer = setTimeout(getKnowledgeWorkflowAction, 2000)
})
}
const polling = setInterval(getKnowledgeWorkflowAction, 2000)
const stopPolling = () => {
if (pollingTimer) {
clearTimeout(pollingTimer)
pollingTimer = null
}
}
//
getKnowledgeWorkflowAction()
onUnmounted(() => {
clearInterval(polling)
stopPolling()
})
</script>
<style lang="scss"></style>