Add HTML5 setup SSE updates
This commit is contained in:
@@ -2021,6 +2021,24 @@ async def html5_project_setup_summary(project_id: str) -> Response:
|
||||
)
|
||||
|
||||
|
||||
@app.get("/html5/projects/{project_id}/setup/events")
|
||||
async def html5_project_setup_events(project_id: str, once: bool = False) -> StreamingResponse:
|
||||
async def stream_setup():
|
||||
while True:
|
||||
setup = _project_setup_response(project_id)
|
||||
yield _html5_sse_event("setup-summary", render_html5_setup_summary(project_id, setup))
|
||||
yield _html5_sse_event("setup-import-job", render_html5_import_job(project_id, _html5_latest_import_job(project_id)))
|
||||
if once:
|
||||
break
|
||||
await asyncio.sleep(2)
|
||||
|
||||
return StreamingResponse(
|
||||
stream_setup(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
@app.post("/html5/projects/{project_id}/setup/source")
|
||||
async def html5_project_setup_source(project_id: str, request: Request) -> Response:
|
||||
form = await _html5_form_data(request)
|
||||
@@ -8392,6 +8410,19 @@ def _html5_operation_jobs() -> list[OperationJob]:
|
||||
return sorted(_operations.jobs.values(), key=lambda job: job.updated_at, reverse=True)[:50]
|
||||
|
||||
|
||||
def _html5_latest_import_job(project_id: str) -> OperationJob | None:
|
||||
jobs = [
|
||||
job
|
||||
for job in _operations.jobs.values()
|
||||
if job.payload.get("project_id") == project_id and _operation_value(getattr(job, "kind", "")) == "SERVER_IMPORT"
|
||||
]
|
||||
return max(jobs, key=lambda job: job.updated_at) if jobs else None
|
||||
|
||||
|
||||
def _operation_value(value: object) -> str:
|
||||
return str(getattr(value, "value", value))
|
||||
|
||||
|
||||
def _html5_sse_event(event: str, fragment: str) -> str:
|
||||
data = "\n".join(f"data: {line}" for line in fragment.splitlines())
|
||||
return f"event: {event}\n{data}\n\n"
|
||||
|
||||
Reference in New Issue
Block a user