Skip to content

Commit a100816

Browse files
committed
fix: fixed xhr abort
1 parent 42de1e0 commit a100816

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/injected/proxy/handle.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ export function proxyFakeXhrInstance(this: ProxyXMLHttpRequest, options: Options
200200
// event source stream
201201
if (isEventSource) {
202202
this.responseText = ''
203-
for await (const item of asyncGenerator(matchItem.chunks!, matchItem.chunkInterval)) {
203+
for await (const item of asyncGenerator(matchItem.chunks!, matchItem.chunkInterval, controller.signal)) {
204204
const str = formatChunk(item, matchItem.chunkTemplate)
205205
this.responseText += str
206206
handleStateChange.call(this, XMLHttpRequest.LOADING)

src/tools/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ export function formatChunk(chunk: string, tpl = 'data: $1\n\n') {
1212

1313
export async function* asyncGenerator(data: unknown[], delay = 1000, signal?: AbortSignal) {
1414
const list = data.map(item => typeof item !== 'string' ? JSON.stringify(item) : item)
15+
let rejectReason
16+
signal?.addEventListener('abort', (event: any) => {
17+
rejectReason = event.target.reason
18+
})
1519
for (const item of list) {
20+
if (rejectReason) {
21+
throw new Error(rejectReason)
22+
}
1623
await new Promise(resolve => setTimeout(resolve, delay))
1724
yield item
1825
}

0 commit comments

Comments
 (0)