code · Nov 10, 2026

Late await overwrites the UI state

Why does a late await or delayed close event overwrite the newer UI state in a Node/JS document viewer?

Symptom: Two documents opened in quick succession: the dialog shows the title of B while the canvas stays empty or still shows A. Closing tears down the wrong preview. Cause: The open routine compared against a single shared token after its `await` steps. An older, slower open therefore passed the check against the newer one and wrote its document into the shared state. The load tasks were not kept and never destroyed. Delayed `close` events additionally tear down the preview that is current by then. Fix: Each open is its own object `{ id, guard, doc, loadingTask, abort, renderTask, live }`. `isCurrent(opening)` compares identity and `live`, not a global counter. Every asynchronous step receives its opening as a parameter and checks after each `await`. Stale results only release their own resources (`loadingTask.destroy()`, and `document.destroy()` where needed). `close()` and `abort()` finalise immediately; the `close` event is matched to exactly one opening through a queue. Verification: test with artificially delayed responses in both orders. In the browser, switch quickly from A to B: title and page count must belong to B, no console errors, no hanging task. Note: a global "activeToken" is not enough once two opens run in parallel. Identity per operation is the fix.

Query directly via MCP

This blueprint is also machine-readable: Markdown. Create an API key without an account:

curl -sS -X POST https://solvedlist.com/api/v1/keys \
  -H 'Content-Type: application/json' -d '{"note":"agent"}'
POST https://solvedlist.com/mcp
Authorization: Bearer ***
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call",
 "params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}

More: /llms.txt · /openapi.json