fix: 清理前端异步请求生命周期
- 页面卸载和弹窗关闭时终止 SSE - 为版本检查增加超时、中止与重复请求保护
This commit is contained in:
@@ -578,6 +578,9 @@ watch(
|
||||
{ flush: 'post' },
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
if (sending.value) {
|
||||
stopSse();
|
||||
}
|
||||
if (bubbleListScrollElement.value) {
|
||||
bubbleListScrollElement.value.removeEventListener(
|
||||
'scroll',
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue';
|
||||
import { onBeforeUnmount, ref, watch } from 'vue';
|
||||
|
||||
import { EasyFlowPanelModal } from '@easyflow/common-ui';
|
||||
import { $t } from '@easyflow/locales';
|
||||
@@ -18,6 +18,16 @@ const rules = {
|
||||
};
|
||||
const loading = ref(false);
|
||||
|
||||
const stopStreaming = () => {
|
||||
if (!loading.value) return;
|
||||
sseClient.abort();
|
||||
loading.value = false;
|
||||
};
|
||||
|
||||
const closeDialog = () => {
|
||||
dialogVisible.value = false;
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
loading.value = true;
|
||||
const data = {
|
||||
@@ -40,12 +50,28 @@ const handleSubmit = async () => {
|
||||
const delta = sseData.payload?.delta;
|
||||
formData.value.prompt += delta;
|
||||
},
|
||||
onError(error) {
|
||||
loading.value = false;
|
||||
console.error('提示词优化流式请求失败', error);
|
||||
},
|
||||
onFinished() {
|
||||
loading.value = false;
|
||||
},
|
||||
});
|
||||
};
|
||||
const handleReplace = () => {
|
||||
emit('success', formData.value.prompt);
|
||||
dialogVisible.value = false;
|
||||
closeDialog();
|
||||
};
|
||||
|
||||
watch(dialogVisible, (visible) => {
|
||||
if (!visible) {
|
||||
stopStreaming();
|
||||
}
|
||||
});
|
||||
|
||||
onBeforeUnmount(stopStreaming);
|
||||
|
||||
defineExpose({
|
||||
open(botId: string, systemPrompt: string) {
|
||||
formData.value = {
|
||||
@@ -80,7 +106,7 @@ defineExpose({
|
||||
</ElForm>
|
||||
|
||||
<template #footer>
|
||||
<ElButton @click="dialogVisible = false">
|
||||
<ElButton @click="closeDialog">
|
||||
{{ $t('button.cancel') }}
|
||||
</ElButton>
|
||||
<ElButton type="primary" @click="handleReplace" v-if="!loading">
|
||||
|
||||
@@ -351,6 +351,9 @@ watch(
|
||||
);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
if (sending.value || activeStreamSessionId.value) {
|
||||
abortActiveStream();
|
||||
}
|
||||
window.removeEventListener('click', closeSessionContextMenu);
|
||||
window.removeEventListener('resize', closeSessionContextMenu);
|
||||
window.removeEventListener('scroll', closeSessionContextMenu, true);
|
||||
|
||||
Reference in New Issue
Block a user