Repository navigation
Description
Problem
WaveEditorDocument.apply captures a snapshot, awaits engine.transform, and then unconditionally records the old snapshot and replaces the current document. A replace, undo, redo, or another completed transform during that wait is not detected. A delayed formatting response can discard newer text and replace its undo history with the older state.
The plain DOM adapter has the same conflict: its busy flag prevents another formatting command but leaves typing and setValue available. The awaited result then overwrites input.value. A result can also arrive after the controller has been destroyed.
Code references:
- editor/core/document.ts — apply, replace, and restore
- editor/core/engine.ts — asynchronous adapter contract
- editor/dom/index.ts — apply, setValue, and destroy
Reproduction
Use an adapter whose promise is resolved explicitly by the test rather than using a timer:
- Create
WaveEditorDocument('old', 0, 3). - Start
apply('bold', delayedEngine)without awaiting completion. - Call
replace('new user input'). - Resolve the adapter with the bold transformation of the original snapshot, then await
apply. - Inspect the document and call
undo().
A focused Node.js execution of the current core modules produced:
After the delayed result: content = "**old**"
After undo: content = "old"
The newer text is absent from both results. The core reproduction used the repository modules with only local import suffixes adjusted for direct TypeScript execution; it did not run the browser UI.
For the DOM regression, start a delayed toolbar transform, type new text or call setValue, and then release the promise.
Expected behavior and scope
An asynchronous transformation must only commit against the document revision it was computed from, or participate in a documented serialized/rebased operation policy. Do not silently discard a newer edit. A rejected or obsolete result must not mutate undo/redo history.
Apply the same stale-result protection to the plain DOM controller and invalidate pending UI work on destruction. Keep adapter errors distinguishable from an obsolete result, and handle any rejection in the event-handler path rather than leaving an unhandled promise.
This is local editor state coordination, not server persistence, collaborative editing, or a new transport protocol.
Completion criteria
- Add deferred-promise tests for a pending transform followed by
replace,undo, orredo; later content and history are preserved. - Cover overlapping
applycalls resolved in reverse order and document the chosen conflict/serialization behavior. - A normal uncontended transform still updates content and selection and supports undo/redo.
- A failed or obsolete transform does not add a misleading undo snapshot or clear valid redo history.
- DOM tests cover typing and
setValueduring a pending transform, plus completion afterdestroywithout DOM changes, focus changes, or callbacks. - Clear busy state on all completion paths without blocking ordinary editing indefinitely.
- Keep the framework-independent core free of DOM dependencies and run the editor package tests.
Difficulty: medium.
Activity