* perf(canvas): find guide owners from an index instead of walking the page Drawing and hit-testing guides visited every layer of the page on each frame to find the few that carry guides; on a large page that took about 1.2s of a load's rendering. The graph now keeps the set of layers with guides current from its node events, and both read it. * perf(layout): write only changed geometry and request one render per layout pass Layout wrote every laid-out layer's geometry whether or not it changed, so each pass over a large page notified every graph listener for every layer; a page load spent about 1s handling those updates. Layout now writes only the fields that changed, and the updates a layout pass makes ask for a single render, queued for before the next frame, instead of bumping the editor's reactive versions once per layer. * test(canvas): drop the graph double the guide tests no longer use * perf(fig): copy an opened file once on the main thread instead of three times Opening a .fig in the browser copied the file for a main-thread fallback the caller's buffer already served, then copied it twice more for the worker, which parses from one and keeps the other as the original archive. A 109MB file briefly took over 400MB on the main thread. The fallback now reuses the caller's buffer, and the worker receives one copy and makes its archive copy itself. * docs(changelog): note faster large-page loads * fix(app): run recovery and autosave after edits, not redraws Recovery and autosave watched sceneVersion, a render counter that every requestRender bumps: opening a file, layout passes, fonts, and each layer a page loads. Safari has no File System Access API, so its opened files have no writable source and always get recovery snapshots. Loading a page also invalidated the original archive, so a snapshot encoded the whole document again; on the Preline UI kit's CMS page that ran about 100 s on the main thread in WebKit before failing. Recovery, autosave, and saved versions now follow the content revision of createDocumentChanges. A page's layers loading from the opened file, which runs inside applyImportedStateDuring on both the worker and main-thread paths, no longer counts as an edit for that revision or for the original archive, and asks for one render rather than one per layer. In WebKit the CMS page switch drops from 105-133 s to 21 s, and its longest main-thread stall from 90-117 s to 1.4 s. * refactor(app): stop storing a version with recovery snapshots Adopting a snapshot now protects the document's current content revision, so nothing reads the version a snapshot was taken at. Snapshots saved by earlier versions carry a sceneVersion field instead; dropping the field keeps the metadata type true for both. Tests tell snapshots apart by the bytes they were built with.
123 lines
3.2 KiB
TypeScript
123 lines
3.2 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import { reactive, ref } from 'vue'
|
|
|
|
import { createAutosave } from '@/app/document/autosave/create'
|
|
|
|
function deferred() {
|
|
let resolve: (() => void) | null = null
|
|
const promise = new Promise<void>((done) => {
|
|
resolve = done
|
|
})
|
|
return { promise, resolve: () => resolve?.() }
|
|
}
|
|
|
|
function setup(saveCurrentDocument: (version: number) => Promise<void>) {
|
|
const state = reactive({ autosaveEnabled: true })
|
|
const version = ref(0)
|
|
let savedVersion = 0
|
|
let writable = true
|
|
const autosave = createAutosave({
|
|
state,
|
|
version: () => version.value,
|
|
getSavedVersion: () => savedVersion,
|
|
hasWritableSource: () => writable,
|
|
saveCurrentDocument: async (version) => {
|
|
await saveCurrentDocument(version)
|
|
savedVersion = version
|
|
}
|
|
})
|
|
return {
|
|
state,
|
|
version,
|
|
autosave,
|
|
setWritable: (value: boolean) => {
|
|
writable = value
|
|
}
|
|
}
|
|
}
|
|
|
|
describe('document autosave', () => {
|
|
test('skips saved versions and documents without writable sources', async () => {
|
|
const versions: number[] = []
|
|
const { version, autosave, setWritable } = setup(async (saved) => {
|
|
versions.push(saved)
|
|
})
|
|
|
|
await autosave.requestSave(0)
|
|
setWritable(false)
|
|
version.value = 1
|
|
await autosave.requestSave(1)
|
|
expect(versions).toEqual([])
|
|
|
|
setWritable(true)
|
|
await autosave.requestSave(1)
|
|
version.value = 0
|
|
await autosave.requestSave(0)
|
|
await autosave.requestSave(1)
|
|
expect(versions).toEqual([1])
|
|
autosave.disposeAutosave()
|
|
})
|
|
|
|
test('coalesces 100 edits during an in-flight save into the latest version', async () => {
|
|
const firstSave = deferred()
|
|
const started: number[] = []
|
|
const { version, autosave } = setup(async (saved) => {
|
|
started.push(saved)
|
|
if (started.length === 1) await firstSave.promise
|
|
})
|
|
|
|
version.value = 1
|
|
const pending = autosave.requestSave(1)
|
|
await Promise.resolve()
|
|
for (let next = 2; next <= 101; next++) {
|
|
version.value = next
|
|
void autosave.requestSave(next)
|
|
}
|
|
firstSave.resolve()
|
|
await pending
|
|
|
|
expect(started).toEqual([1, 101])
|
|
autosave.disposeAutosave()
|
|
})
|
|
|
|
test('retries the current version after a failed save', async () => {
|
|
let attempts = 0
|
|
const { version, autosave } = setup(async () => {
|
|
attempts++
|
|
if (attempts === 1) throw new Error('write failed')
|
|
})
|
|
version.value = 1
|
|
|
|
await expect(autosave.requestSave(1)).rejects.toThrow('write failed')
|
|
await autosave.requestSave(1)
|
|
|
|
expect(attempts).toBe(2)
|
|
autosave.disposeAutosave()
|
|
})
|
|
|
|
test('preserves a newer requested version when the active save fails', async () => {
|
|
const firstSave = deferred()
|
|
const started: number[] = []
|
|
const { version, autosave } = setup(async (saved) => {
|
|
started.push(saved)
|
|
if (saved === 1) {
|
|
await firstSave.promise
|
|
throw new Error('write failed')
|
|
}
|
|
})
|
|
|
|
version.value = 1
|
|
const failed = autosave.requestSave(1)
|
|
await Promise.resolve()
|
|
version.value = 2
|
|
void autosave.requestSave(2)
|
|
firstSave.resolve()
|
|
await expect(failed).rejects.toThrow('write failed')
|
|
await Promise.resolve()
|
|
|
|
expect(started).toEqual([1, 2])
|
|
autosave.disposeAutosave()
|
|
})
|
|
})
|