fix(app): roll the preview back on a throwing accept or a non-trap failure
This commit is contained in:
1 parent
173be5949a
commit
bc43a1b86b
3 files changed
+85
-34
No files matched your search
@@ -32,12 +32,6 @@ export type ReplaceOutcome<R> =
|
||||
| { ok: false; reason: 'core'; failure: CoreFailure }
|
||||
| { ok: false; reason: 'refused'; refusal: R }
|
||||
|
||||
/** A trap or a stopped module: no handle to roll back, the rebuild uses the kept input. */
|
||||
const TRAPPED_KINDS: ReadonlySet<string> = new Set([
|
||||
'internal',
|
||||
'core_unavailable',
|
||||
])
|
||||
|
||||
/**
|
||||
* A stored version loaded into the core (`PreviewSheet`, RUST.md §12): the
|
||||
* core checks each drawing's sha256 against its row, the fingerprint, the caps
|
||||
@@ -87,14 +81,10 @@ export class CataloguePreview {
|
||||
parseOutcome<null>(handle.value.replace_document(text)),
|
||||
)
|
||||
if (!replaced.ok) return { ok: false, reason: 'core', failure: replaced }
|
||||
const outcome = this.judge(accept)
|
||||
const generation = this.runtime.generation
|
||||
const outcome = this.judgeOrRollback(previous, accept)
|
||||
if (outcome.ok) this.input = { ...this.input, document: text }
|
||||
else if (
|
||||
outcome.reason === 'refused' ||
|
||||
!TRAPPED_KINDS.has(outcome.failure.kind)
|
||||
) {
|
||||
this.rollback(previous)
|
||||
}
|
||||
else if (!this.trappedSince(generation)) this.rollback(previous)
|
||||
return outcome
|
||||
}
|
||||
|
||||
@@ -132,6 +122,26 @@ export class CataloguePreview {
|
||||
this.discardHandle()
|
||||
}
|
||||
|
||||
/** A trap poisons the runtime: it is no longer ready, or already a newer instance. */
|
||||
private trappedSince(generation: number): boolean {
|
||||
return (
|
||||
this.runtime.status !== 'ready' || this.runtime.generation !== generation
|
||||
)
|
||||
}
|
||||
|
||||
/** `accept` throwing must not leave the handle on the candidate text. */
|
||||
private judgeOrRollback<R>(
|
||||
previous: string,
|
||||
accept: ((state: PreviewState) => Acceptance<R>) | undefined,
|
||||
): ReplaceOutcome<R> {
|
||||
try {
|
||||
return this.judge(accept)
|
||||
} catch (error) {
|
||||
this.rollback(previous)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
private judge<R>(
|
||||
accept: ((state: PreviewState) => Acceptance<R>) | undefined,
|
||||
): ReplaceOutcome<R> {
|
||||
@@ -143,7 +153,7 @@ export class CataloguePreview {
|
||||
: { ok: false, reason: 'refused', refusal: verdict.refusal }
|
||||
}
|
||||
|
||||
/** Puts the handle back on the kept text; if even that fails the handle is dropped and rebuilt. */
|
||||
/** Puts the handle back on the kept text; if that fails the handle is dropped and rebuilt. */
|
||||
private rollback(previous: string): void {
|
||||
const handle = this.current()
|
||||
const restored = handle.ok
|
||||
|
||||
@@ -28,22 +28,17 @@ export interface CleaningPreview {
|
||||
|
||||
const NAME = { fr: 'Aperçu', en: 'Preview' }
|
||||
|
||||
/**
|
||||
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's
|
||||
* composed tree and never the original file. The caller frees `preview` on close.
|
||||
*/
|
||||
export function composeCleaningPreview(
|
||||
drawing: StoredDrawing,
|
||||
): CoreOutcome<CleaningPreview> {
|
||||
const runtime = coreRuntime()
|
||||
const empty = runtime.call<unknown>(
|
||||
/** The throwaway document: the empty sheet plus one detail view naming the drawing. */
|
||||
function cleaningDocument(
|
||||
viewId: string,
|
||||
assetId: string,
|
||||
): CoreOutcome<string> {
|
||||
const empty = coreRuntime().call<unknown>(
|
||||
'sheet.empty',
|
||||
JSON.stringify({ species_id: 'preview', name: NAME }),
|
||||
)
|
||||
if (!empty.ok) return empty
|
||||
const assetId = generateUuid()
|
||||
const viewId = newElementId('view')
|
||||
const edited = applyEdit(JSON.stringify(empty.value), {
|
||||
return applyEdit(JSON.stringify(empty.value), {
|
||||
op: 'insert',
|
||||
element: {
|
||||
kind: 'view',
|
||||
@@ -57,17 +52,28 @@ export function composeCleaningPreview(
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!edited.ok) return edited
|
||||
}
|
||||
|
||||
/**
|
||||
* A throwaway draft holding only the cleaned drawing, so the dialog renders the core's
|
||||
* composed tree and never the original file. The caller frees `preview` on close.
|
||||
*/
|
||||
export function composeCleaningPreview(
|
||||
drawing: StoredDrawing,
|
||||
): CoreOutcome<CleaningPreview> {
|
||||
const assetId = generateUuid()
|
||||
const viewId = newElementId('view')
|
||||
const document = cleaningDocument(viewId, assetId)
|
||||
if (!document.ok) return document
|
||||
const preview = new CataloguePreview(
|
||||
runtime,
|
||||
{ status: 'draft', document: edited.value },
|
||||
coreRuntime(),
|
||||
{ status: 'draft', document: document.value },
|
||||
[],
|
||||
)
|
||||
const bytes = new TextEncoder().encode(drawing.stored)
|
||||
const added = preview.addDrawing({ assetId, sha256: drawing.sha256, bytes })
|
||||
if (!added.ok) {
|
||||
preview.free()
|
||||
return added
|
||||
}
|
||||
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
|
||||
if (added.ok)
|
||||
return { ok: true, value: { preview, viewId, scope: `fv.${viewId}.` } }
|
||||
preview.free()
|
||||
return added
|
||||
}
|
||||
@@ -202,4 +202,39 @@ describe('replaceDocument accept hook', () => {
|
||||
expect(stateOf(preview)).toEqual(before)
|
||||
preview.free()
|
||||
})
|
||||
|
||||
it('rolls back when accept throws, and rethrows', () => {
|
||||
const preview = draft()
|
||||
const before = stateOf(preview)
|
||||
expect(() =>
|
||||
preview.replaceDocument(renamed(preview.document, 'Boom'), () => {
|
||||
throw new Error('boom')
|
||||
}),
|
||||
).toThrow('boom')
|
||||
expect(stateOf(preview)).toEqual(before)
|
||||
forceRebuild()
|
||||
expect(stateOf(preview)).toEqual(before)
|
||||
preview.free()
|
||||
})
|
||||
|
||||
it('rolls back on a non-trap internal failure of state', () => {
|
||||
const preview = draft()
|
||||
const before = stateOf(preview)
|
||||
const handle = (
|
||||
preview as unknown as { handle: { call(op: string, i: string): string } }
|
||||
).handle
|
||||
const real = handle.call.bind(handle)
|
||||
let failed = false
|
||||
handle.call = (op, input) => {
|
||||
if (op === 'preview.state' && !failed) {
|
||||
failed = true
|
||||
return '{"ok":false,"kind":"internal"}'
|
||||
}
|
||||
return real(op, input)
|
||||
}
|
||||
const outcome = preview.replaceDocument(renamed(preview.document, 'Perdu'))
|
||||
expect(outcome).toMatchObject({ ok: false, failure: { kind: 'internal' } })
|
||||
expect(stateOf(preview)).toEqual(before)
|
||||
preview.free()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user