fix(desktop): satisfy curly rule in catalog download code
This commit is contained in:
committed by
brooklyn!
parent
5832a6eb93
commit
7aba2cbbe9
@@ -180,6 +180,7 @@ export async function fetchCatalog(kind: CatalogKind): Promise<CatalogEntry[]> {
|
|||||||
// get the same deadline; an actually stalled download still terminates.
|
// get the same deadline; an actually stalled download still terminates.
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
let timer: ReturnType<typeof setTimeout> | undefined
|
let timer: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
const armDeadline = () => {
|
const armDeadline = () => {
|
||||||
clearTimeout(timer)
|
clearTimeout(timer)
|
||||||
timer = setTimeout(() => controller.abort(), 60_000)
|
timer = setTimeout(() => controller.abort(), 60_000)
|
||||||
@@ -211,8 +212,14 @@ export async function fetchCatalog(kind: CatalogKind): Promise<CatalogEntry[]> {
|
|||||||
while (true) {
|
while (true) {
|
||||||
const { done, value } = await reader.read()
|
const { done, value } = await reader.read()
|
||||||
|
|
||||||
if (done) break
|
if (done) {
|
||||||
if (value.byteLength > 0) armDeadline()
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.byteLength > 0) {
|
||||||
|
armDeadline()
|
||||||
|
}
|
||||||
|
|
||||||
chunks.push(decoder.decode(value, { stream: true }))
|
chunks.push(decoder.decode(value, { stream: true }))
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ beforeEach(() => {
|
|||||||
vi.spyOn(AbortSignal, 'timeout').mockImplementation(ms => {
|
vi.spyOn(AbortSignal, 'timeout').mockImplementation(ms => {
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
setTimeout(() => controller.abort(new DOMException('Timed out', 'TimeoutError')), ms)
|
setTimeout(() => controller.abort(new DOMException('Timed out', 'TimeoutError')), ms)
|
||||||
|
|
||||||
return controller.signal
|
return controller.signal
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -28,6 +29,7 @@ function serveStream() {
|
|||||||
'fetch',
|
'fetch',
|
||||||
vi.fn(async (_url, options) => {
|
vi.fn(async (_url, options) => {
|
||||||
signal = options.signal
|
signal = options.signal
|
||||||
|
|
||||||
return new Response(
|
return new Response(
|
||||||
new ReadableStream<Uint8Array>({
|
new ReadableStream<Uint8Array>({
|
||||||
start(controller) {
|
start(controller) {
|
||||||
@@ -40,6 +42,7 @@ function serveStream() {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
send: (text: string) => writer.enqueue(encoder.encode(text)),
|
send: (text: string) => writer.enqueue(encoder.encode(text)),
|
||||||
close: () => writer.close(),
|
close: () => writer.close(),
|
||||||
@@ -53,10 +56,12 @@ describe('catalog download liveness', () => {
|
|||||||
'finishes a progressing %s download beyond the old total deadline',
|
'finishes a progressing %s download beyond the old total deadline',
|
||||||
async kind => {
|
async kind => {
|
||||||
const stream = serveStream()
|
const stream = serveStream()
|
||||||
|
|
||||||
const result = fetchCatalog(kind).then(
|
const result = fetchCatalog(kind).then(
|
||||||
value => ({ value }),
|
value => ({ value }),
|
||||||
error => ({ error })
|
error => ({ error })
|
||||||
)
|
)
|
||||||
|
|
||||||
await vi.advanceTimersByTimeAsync(40_000)
|
await vi.advanceTimersByTimeAsync(40_000)
|
||||||
stream.send('[{"name":"slow-')
|
stream.send('[{"name":"slow-')
|
||||||
await vi.advanceTimersByTimeAsync(40_000)
|
await vi.advanceTimersByTimeAsync(40_000)
|
||||||
@@ -103,8 +108,13 @@ describe('catalog download liveness', () => {
|
|||||||
vi.fn(async () => new Response(body))
|
vi.fn(async () => new Response(body))
|
||||||
)
|
)
|
||||||
const result = await fetchCatalog('skills').catch(error => error)
|
const result = await fetchCatalog('skills').catch(error => error)
|
||||||
if (body === '[]') expect(result).toEqual([])
|
|
||||||
else expect(result).toBeInstanceOf(SyntaxError)
|
if (body === '[]') {
|
||||||
|
expect(result).toEqual([])
|
||||||
|
} else {
|
||||||
|
expect(result).toBeInstanceOf(SyntaxError)
|
||||||
|
}
|
||||||
|
|
||||||
expect(vi.getTimerCount()).toBe(0)
|
expect(vi.getTimerCount()).toBe(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user