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

@@ -3,8 +3,9 @@ interface StreamFetchProps {
url: string;
data: any;
onMessage: (text: string) => void;
abortSignal: AbortController;
}
export const streamFetch = ({ url, data, onMessage }: StreamFetchProps) =>
export const streamFetch = ({ url, data, onMessage, abortSignal }: StreamFetchProps) =>
new Promise(async (resolve, reject) => {
try {
const res = await fetch(url, {
@@ -13,7 +14,8 @@ export const streamFetch = ({ url, data, onMessage }: StreamFetchProps) =>
'Content-Type': 'application/json',
Authorization: getToken() || ''
},
body: JSON.stringify(data)
body: JSON.stringify(data),
signal: abortSignal.signal
});
const reader = res.body?.getReader();
if (!reader) return;