Skip to content

Commit

Permalink
fix: fixed xhr abort
Browse files Browse the repository at this point in the history
  • Loading branch information
hans000 committed Sep 1, 2024
1 parent 42de1e0 commit a100816
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/injected/proxy/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function proxyFakeXhrInstance(this: ProxyXMLHttpRequest, options: Options
// event source stream
if (isEventSource) {
this.responseText = ''
for await (const item of asyncGenerator(matchItem.chunks!, matchItem.chunkInterval)) {
for await (const item of asyncGenerator(matchItem.chunks!, matchItem.chunkInterval, controller.signal)) {
const str = formatChunk(item, matchItem.chunkTemplate)
this.responseText += str
handleStateChange.call(this, XMLHttpRequest.LOADING)
Expand Down
7 changes: 7 additions & 0 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export function formatChunk(chunk: string, tpl = 'data: $1\n\n') {

export async function* asyncGenerator(data: unknown[], delay = 1000, signal?: AbortSignal) {
const list = data.map(item => typeof item !== 'string' ? JSON.stringify(item) : item)
let rejectReason
signal?.addEventListener('abort', (event: any) => {
rejectReason = event.target.reason
})
for (const item of list) {
if (rejectReason) {
throw new Error(rejectReason)
}
await new Promise(resolve => setTimeout(resolve, delay))
yield item
}
Expand Down

0 comments on commit a100816

Please sign in to comment.