Build operational transformation for plain text: compose, transform, a server history and a client that buffers edits
Problem
Google Docs-style editors let two people type into the same text at once. With operational transformation (OT), every edit is an operation, and when two operations were made concurrently on the same version, one is transformed past the other so both sides end with the same text. The classic design (Jupiter, used by Google Wave and the ot.js library) has one central server that orders operations, and clients that keep at most one operation in flight.
const a = new TextOperation().retain(3).insert("A").retain(2);
const b = new TextOperation().retain(3).insert("B").retain(2);
const [aPrime, bPrime] = TextOperation.transform(a, b);
bPrime.apply(a.apply("abcde")); // "abcABde"
aPrime.apply(b.apply("abcde")); // "abcABde"Ref. UR-1444 · © UIRound · uiround.com/questions/build-operational-transformation-for-plain-text-compose-transform-a-server-history-and-a-client-that-buffers
Unlock this question
The full problem, its tests and the tested solution are part of Pro.
- 2
- code listings
- 6
- edge cases
- 5
- follow-up questions
What's inside
- The full problem, with examples
- Tests that check your solution
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Card worldwide
- UPI and net banking in India
- One-time payment, nothing renews
Stuck? Take a hint
- 1Write an operation as a list that covers the whole old document: retain n characters, insert a string, delete n characters. apply, compose and transform are all one loop that walks two such lists, taking the shorter piece and keeping the rest for the next step.
Try it in the editor
Run your code as often as you like. Submitting it to the full test suite is part of Pro.
Private, and saved in this browser as you type. All your notes are on My lists.