Skip to content

AI-native design editor. Open-source Figma alternative.

fix: let a document opened in a background tab wait to be drawn (#1298)

Danila Poyarkov5b8d06ffdd1f
diff --git a/changes/1298/fixed.md b/changes/1298/fixed.md
new file mode 100644
index 000000000..ff32fe116
--- /dev/null
+++ b/changes/1298/fixed.md
@@ -0,0 +1 @@
+- A document still opening when you switch to another tab no longer fails with "The operation was timed out"; it finishes opening and is drawn when you return to its tab.
diff --git a/src/app/editor/preparation/controller.ts b/src/app/editor/preparation/controller.ts
index 337b1683d..8dede5fbd 100644
--- a/src/app/editor/preparation/controller.ts
+++ b/src/app/editor/preparation/controller.ts
@@ -1,4 +1,4 @@
-import { withTimeout } from 'es-toolkit/promise'
+import { TimeoutError } from 'es-toolkit/error'
import type { EditorPreparationEventEmitter } from '@/app/editor/preparation/events'
import type {
@@ -15,6 +15,13 @@ const PRESENTATION_TIMEOUT_MS = 10_000
interface PresentationWaiter {
sceneVersion: number
resolve: () => void
+ reject: (error: Error) => void
+ /** Runs only while a canvas shows the document, which is the only place it is presented. */
+ timer: ReturnType<typeof setTimeout> | null
+ /** When the timer last started, so hiding the tab keeps the time it already had. */
+ startedAt: number
+ /** The time left to draw the scene while shown. */
+ remainingMs: number
}
export interface EditorPreparationControllerOptions {
@@ -25,6 +32,11 @@ export interface EditorPreparationController {
begin(options: BeginEditorPreparation): EditorPreparationHandle
acknowledgePresentation(sceneVersion: number): void
waitForPresentation(id: number, sceneVersion: number): Promise<void>
+ /**
+ * A canvas started showing the document; returns its detach. A tab in the background has no
+ * canvas, so its scene waits to be presented until the tab is shown, without timing out.
+ */
+ attachCanvas(): () => void
dispose(): void
}
@@ -38,8 +50,31 @@ export function createEditorPreparationController(
let activeAbort: AbortController | null = null
let activeCancel: ((reason: EditorPreparationCancelReason) => void) | null = null
let presentedSceneVersion = -1
+ let canvases = 0
const presentationWaiters = new Map<number, PresentationWaiter>()
+ function settle(id: number, outcome: Error | null) {
+ const waiter = presentationWaiters.get(id)
+ if (!waiter) return
+ presentationWaiters.delete(id)
+ if (waiter.timer !== null) clearTimeout(waiter.timer)
+ if (outcome) waiter.reject(outcome)
+ else waiter.resolve()
+ }
+
+ function startTimer(id: number, waiter: PresentationWaiter) {
+ if (waiter.timer !== null) return
+ waiter.startedAt = Date.now()
+ waiter.timer = setTimeout(() => settle(id, new TimeoutError()), waiter.remainingMs)
+ }
+
+ function pauseTimer(waiter: PresentationWaiter) {
+ if (waiter.timer === null) return
+ clearTimeout(waiter.timer)
+ waiter.timer = null
+ waiter.remainingMs = Math.max(0, waiter.remainingMs - (Date.now() - waiter.startedAt))
+ }
+
const isActive = (id: number) => state.preparation?.id === id
return {
@@ -61,8 +96,7 @@ export function createEditorPreparationController(
events?.emit('preparation:started', state.preparation)
const clear = () => {
- presentationWaiters.get(id)?.resolve()
- presentationWaiters.delete(id)
+ settle(id, null)
if (isActive(id)) state.preparation = null
if (activeAbort === abort) activeAbort = null
if (activeCancel === cancel) activeCancel = null
@@ -120,28 +154,43 @@ export function createEditorPreparationController(
acknowledgePresentation(sceneVersion) {
presentedSceneVersion = Math.max(presentedSceneVersion, sceneVersion)
for (const [id, waiter] of presentationWaiters) {
- if (waiter.sceneVersion > presentedSceneVersion) continue
- waiter.resolve()
- presentationWaiters.delete(id)
+ if (waiter.sceneVersion <= presentedSceneVersion) settle(id, null)
}
},
waitForPresentation(id, sceneVersion) {
if (!isActive(id) || presentedSceneVersion >= sceneVersion) return Promise.resolve()
- return withTimeout(
- () =>
- new Promise<void>((resolve) => {
- presentationWaiters.set(id, { sceneVersion, resolve })
- }),
- presentationTimeoutMs
- ).finally(() => presentationWaiters.delete(id))
+ settle(id, null)
+ return new Promise<void>((resolve, reject) => {
+ const waiter: PresentationWaiter = {
+ sceneVersion,
+ resolve,
+ reject,
+ timer: null,
+ startedAt: 0,
+ remainingMs: presentationTimeoutMs
+ }
+ presentationWaiters.set(id, waiter)
+ if (canvases > 0) startTimer(id, waiter)
+ })
+ },
+ attachCanvas() {
+ canvases += 1
+ for (const [id, waiter] of presentationWaiters) startTimer(id, waiter)
+ let attached = true
+ return () => {
+ if (!attached) return
+ attached = false
+ canvases -= 1
+ if (canvases > 0) return
+ for (const waiter of presentationWaiters.values()) pauseTimer(waiter)
+ }
},
dispose() {
activeCancel?.('tab-closed')
activeAbort?.abort()
activeAbort = null
activeCancel = null
- for (const waiter of presentationWaiters.values()) waiter.resolve()
- presentationWaiters.clear()
+ for (const id of presentationWaiters.keys()) settle(id, null)
state.preparation = null
}
}
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index 358f215ba..2b70e32bd 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -98,6 +98,9 @@ const shouldSuspendRender = () =>
/** The pan and zoom of the scene's last drawn frame, which preview islands follow. */
const drawnView = shallowRef<ViewportTransform | null>(null)
+// The tab's scene is presented only here, so waiting for it times out only while it is shown.
+onUnmounted(store.preparationController.attachCanvas())
+
useCanvas(sceneCanvasRef, store, {
layer: 'scene',
onFrame: (view) => {
diff --git a/tests/engine/app/editor/preparation/controller.test.ts b/tests/engine/app/editor/preparation/controller.test.ts
index bce79ae22..3913532ff 100644
--- a/tests/engine/app/editor/preparation/controller.test.ts
+++ b/tests/engine/app/editor/preparation/controller.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, test } from 'bun:test'
+import { describe, expect, jest, test } from 'bun:test'
import { createEditorPreparationController } from '@/app/editor/preparation/controller'
import { createEditorPreparationEvents } from '@/app/editor/preparation/events'
@@ -83,6 +83,7 @@ describe('editor preparation controller', () => {
presentationTimeoutMs: 1
})
const handle = controller.begin({ kind: 'page-switch' })
+ controller.attachCanvas()
await expect(controller.waitForPresentation(handle.id, 7)).rejects.toHaveProperty(
'message',
@@ -91,6 +92,67 @@ describe('editor preparation controller', () => {
handle.fail({ code: 'render-failed', message: 'Timed out', retryable: true })
expect(state.preparation).toBeNull()
})
+ test('waits without a timeout while no canvas shows the document', async () => {
+ jest.useFakeTimers()
+ try {
+ const state = createInitialAppEditorState('page')
+ const controller = createEditorPreparationController(state, undefined, {
+ presentationTimeoutMs: 100
+ })
+ const handle = controller.begin({ kind: 'page-switch' })
+ let settled = false
+ const waiting = controller.waitForPresentation(handle.id, 7).finally(() => {
+ settled = true
+ })
+ // A tab opened in the background: nothing draws it, and that is not a failure.
+ jest.advanceTimersByTime(1_000)
+ await Promise.resolve()
+ expect(settled).toBe(false)
+
+ // Shown, it gets the usual time to draw; leaving it again pauses that.
+ const detach = controller.attachCanvas()
+ jest.advanceTimersByTime(50)
+ detach()
+ jest.advanceTimersByTime(1_000)
+ await Promise.resolve()
+ expect(settled).toBe(false)
+
+ controller.attachCanvas()
+ controller.acknowledgePresentation(7)
+ await waiting
+ handle.complete()
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
+ test('counts the time a tab was shown toward the limit, across visits', async () => {
+ jest.useFakeTimers()
+ try {
+ const state = createInitialAppEditorState('page')
+ const controller = createEditorPreparationController(state, undefined, {
+ presentationTimeoutMs: 100
+ })
+ const handle = controller.begin({ kind: 'page-switch' })
+ let failure: unknown = null
+ const waiting = controller.waitForPresentation(handle.id, 7).catch((error: unknown) => {
+ failure = error
+ })
+ // Two visits of 60 ms each: the scene had longer than its 100 ms to draw.
+ const detach = controller.attachCanvas()
+ jest.advanceTimersByTime(60)
+ detach()
+ controller.attachCanvas()
+ jest.advanceTimersByTime(60)
+ await Promise.resolve()
+ expect(failure).toHaveProperty('message', 'The operation was timed out')
+ controller.acknowledgePresentation(7)
+ await waiting
+ handle.fail({ code: 'render-failed', message: 'Timed out', retryable: true })
+ } finally {
+ jest.useRealTimers()
+ }
+ })
test('emits exactly one terminal outcome', () => {
const state = createInitialAppEditorState('page')
const events = createEditorPreparationEvents()