feat: 增加中断流.fix: 中断流导致的服务端错误

This commit is contained in:
archer
2023-03-22 22:09:40 +08:00
parent 5ec303610c
commit af35e17fdb
4 changed files with 81 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
import React, { useCallback, useState, useRef, useMemo } from 'react';
import React, { useCallback, useState, useRef, useMemo, useEffect } from 'react';
import { useRouter } from 'next/router';
import Image from 'next/image';
import {
@@ -88,6 +88,16 @@ const Chat = ({ chatId }: { chatId: string }) => {
}, [chatData]);
const { pushChatHistory } = useChatStore();
// 中断请求
const controller = useRef(new AbortController());
useEffect(() => {
controller.current = new AbortController();
return () => {
console.log('close========');
// eslint-disable-next-line react-hooks/exhaustive-deps
controller.current?.abort();
};
}, [chatId]);
// 滚动到底部
const scrollToBottom = useCallback(() => {
@@ -212,7 +222,8 @@ const Chat = ({ chatId }: { chatId: string }) => {
};
})
}));
}
},
abortSignal: controller.current
});
// 保存对话信息