fix(ui): снимать статус live при отсутствии обновлений
This commit is contained in:
1 parent
5734340db2
commit
f50a29f116
8 files changed
+103
-5
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import type { Page } from '@playwright/test'
|
||||
|
||||
// Keep a response open; the browser test controls sample delivery independently of HTTP routes.
|
||||
export async function installOpenLiveStream(page: Page, path: string, sample: unknown) {
|
||||
await page.clock.install()
|
||||
await page.addInitScript(({ path, sample }) => {
|
||||
const originalFetch = window.fetch.bind(window)
|
||||
window.fetch = async (input, init) => {
|
||||
const url = new URL(input instanceof Request ? input.url : String(input), location.href)
|
||||
if (url.pathname !== path) return originalFetch(input, init)
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
const send = () => controller.enqueue(new TextEncoder().encode('event: sample\ndata: ' + JSON.stringify(sample) + '\n\n'))
|
||||
Object.assign(window, { sendLiveSample: send })
|
||||
send()
|
||||
},
|
||||
})
|
||||
return new Response(stream, { headers: { 'Content-Type': 'text/event-stream' } })
|
||||
}
|
||||
}, { path, sample })
|
||||
}
|
||||
|
||||
export async function resumeLiveStream(page: Page) {
|
||||
await page.evaluate(() => (window as unknown as { sendLiveSample: () => void }).sendLiveSample())
|
||||
}
|
||||
Reference in new issue
Block a user