Yjs is a CRDT framework, not just a data structure. It ships shared types (Y.Text for collaborative text, Y.Array, Y.Map, Y.XmlFragment for richer documents) and a network-agnostic provider interface that hands the serialised binary updates off to whichever transport you've configured — WebSocket, WebRTC, IndexedDB for offline persistence, or all three at once.
Two ideas do most of the work. First, every character insertion is given an identifier composed of a client ID and a Lamport-style logical clock, so two clients can never produce colliding identifiers even with no coordination. Second, Yjs maintains a state vector — a compact summary of what each client has seen — that lets a peer ask "send me only the updates I'm missing" without re-transmitting the full document.
The library is intentionally small and stays close to the wire. The complete Y.Text implementation is only a few thousand lines, the binary update format is documented, and the same update bytes can be replayed by any client in any order and produce identical state. That predictability is why so many editors — including ShareCode's — sit on Yjs rather than building a custom sync layer.