This commit is contained in:
josc146
2024-08-05 19:01:06 +08:00
parent c496f87fe6
commit 6877a1b7bc
22 changed files with 134 additions and 184 deletions
-27
View File
@@ -1,27 +0,0 @@
export async function fetchSSE(resource, options) {
const { onMessage, onStart, onEnd, onError, ...fetchOptions } = options
const resp = await fetch(resource, fetchOptions).catch(async (err) => {
await onError(err)
})
if (!resp) return
if (!resp.ok) {
await onError(resp)
return
}
let hasStarted = false
const reader = resp.body.getReader()
let result
while (!(result = await reader.read()).done) {
const chunk = result.value
const str = new TextDecoder().decode(chunk)
if (!hasStarted) {
const str = new TextDecoder().decode(chunk)
hasStarted = true
await onStart(str)
}
let data = JSON.parse(str)
onMessage(data)
if (data.done) break
}
await onEnd()
}
-1
View File
@@ -33,7 +33,6 @@ export async function fetchSSE(resource, options) {
console.debug('not common response', error)
}
if (fakeSseData) {
console.log('FAKE')
parser.feed(new TextEncoder().encode(fakeSseData))
break
}