Documentation
¶
Overview ¶
Package ygo is a pure-Go port of the Yjs CRDT framework.
ygo is binary-protocol compatible with the npm yjs package (V1 update encoding), allowing JavaScript clients to synchronize seamlessly with Go servers and vice versa.
Pure-Go means no CGO, so gomobile bind works for iOS/Android targets.
Status: pre-alpha. Public API is unstable.
See https://github.com/Deln0r/ygo for documentation and examples.
Index ¶
- Constants
- func ApplyUpdate(d *Doc, raw []byte) error
- func ApplyUpdateV2(d *Doc, raw []byte) error
- func EncodeDiff(d *Doc, remoteSVBytes []byte) ([]byte, error)
- func EncodeDiffV2(d *Doc, remoteSVBytes []byte) ([]byte, error)
- func EncodeStateAsUpdate(d *Doc) []byte
- func EncodeStateAsUpdateV2(d *Doc) []byte
- func EncodeStateVector(d *Doc) []byte
- func HasPending(d *Doc) bool
- func MergeUpdates(updates [][]byte) ([]byte, error)
- func MissingSV(d *Doc) []byte
- type Array
- type Attrs
- type Awareness
- type Branch
- type ChunkKind
- type DeltaOp
- type Doc
- type Map
- type Options
- type Text
- type Transaction
- type TransactionMut
- type XmlElement
- type XmlFragment
- type XmlText
Constants ¶
const ( ChunkString = types.ChunkString ChunkEmbed = types.ChunkEmbed )
Text.Range emits chunks of either of these kinds.
const DefaultAwarenessTimeout = awareness.DefaultTimeout
DefaultAwarenessTimeout is the y-protocols convention for stale awareness-entry eviction (30 seconds). Pass to Awareness.SweepOutdated.
const MaxClientID = doc.MaxClientID
MaxClientID is the upper bound on Doc.ClientID values (2^53 - 1, matching JS Yjs's safe-integer range).
const Version = "0.0.0-dev"
Version is the current ygo version.
Variables ¶
This section is empty.
Functions ¶
func ApplyUpdate ¶
ApplyUpdate decodes raw and integrates it into d. Items whose dependencies the local store has not yet seen queue in the per-doc pending buffer and drain automatically on subsequent ApplyUpdate calls that satisfy them.
Use HasPending / MissingSV to inspect the queue.
func ApplyUpdateV2 ¶
ApplyUpdateV2 decodes V2 wire bytes and integrates them into d. Pending-buffer semantics identical to ApplyUpdate (V1) — items missing causal dependencies queue silently and drain on subsequent ApplyUpdate / ApplyUpdateV2 calls.
V1 and V2 are NOT wire-interchangeable. Calling ApplyUpdateV2 on V1 bytes (or ApplyUpdate on V2 bytes) is undefined behaviour — either errors loudly or yields a semantically-wrong Update that fails integrate. Per the docs there is no autodetect; the caller must know which version they have via the surrounding transport metadata.
func EncodeDiff ¶
EncodeDiff returns the wire-encoded V1 update covering the blocks d has that the remote (per remoteSVBytes) does not. A nil remoteSVBytes is treated as the empty SV — emit everything.
remoteSVBytes is the V1 wire-encoded form of the remote's state vector (the same shape EncodeStateVector produces).
func EncodeDiffV2 ¶
EncodeDiffV2 is the V2 analogue of EncodeDiff. State-vector argument shape is identical (still V1 wire-encoded SV); only the outgoing update bytes use the V2 column layout.
func EncodeStateAsUpdate ¶
EncodeStateAsUpdate returns the wire-encoded V1 update carrying the doc's full state. Apply to a fresh peer doc to bring it up to speed in one shot; same bytes interoperate with JS Yjs's Y.encodeStateAsUpdate.
func EncodeStateAsUpdateV2 ¶
EncodeStateAsUpdateV2 returns the wire-encoded V2 update carrying the doc's full state. V2 is the column-oriented alternative wire format used by Y.encodeStateAsUpdateV2 / Hocuspocus-V2 paths and some adopters' on-disk persistence layers (y-leveldb, y-indexeddb, SQLite/Postgres Hocuspocus adapters).
V1 and V2 are NOT wire-interchangeable — see ApplyUpdateV2.
func EncodeStateVector ¶
EncodeStateVector returns the wire-encoded V1 state vector of d. Sync-protocol callers send this to peers as "here's what I have; send me everything else."
func HasPending ¶
HasPending reports whether d has any queued items awaiting causal dependencies.
func MergeUpdates ¶
MergeUpdates decodes every blob in updates in order, applies them to a fresh Doc, and returns a single V1 update blob equivalent to the merged state. Returns nil for an empty input.
Used by persistence layers for compaction (Flush) and by transports that want to batch-coalesce updates before sending.
func MissingSV ¶
MissingSV returns the wire-encoded V1 state vector identifying the clocks d needs to receive in order to drain its pending buffer. An empty result means the queue is empty.
Sync-protocol callers send this to peers as a re-fetch request: "I am stuck on items that need updates past this clock."
Types ¶
type Attrs ¶
Attrs is the format-attribute map used by Text's rich-text API and DeltaOp. Keys are arbitrary strings; values are JSON-serializable scalars. A nil value clears the attribute on the affected range.
type Awareness ¶
Awareness tracks per-client ephemeral state (cursors, names, selections). Independent of any Doc; the local clientID is passed at construction. Embedders typically pair an Awareness with a Doc and use d.ClientID() as the awareness clientID.
func NewAwareness ¶
NewAwareness returns a fresh Awareness for the given local clientID. Use d.ClientID() to keep the awareness layer in sync with the doc.
type Branch ¶
Branch is the low-level shared-data container the types layer wraps. Most callers should use the typed constructors (NewMap, NewArray, NewText, NewXmlFragment) rather than building Branches directly. Re-exported here for advanced cases (custom shared-type implementations, observability code).
type DeltaOp ¶
DeltaOp is one Quill-style delta operation produced by Text.ToDelta and (future) consumed by Text.ApplyDelta.
type Doc ¶
Doc is a single CRDT replica — the local view of a collaborative document. Construct with NewDoc; mutate via WriteTxn; read via ReadTxn. See the doc-comment on the underlying type for the full concurrency contract.
func NewDoc ¶
func NewDoc() *Doc
NewDoc returns a fresh Doc with default options and a random client identifier.
func NewDocWithOptions ¶
NewDocWithOptions returns a fresh Doc with the given options.
type Map ¶
Shared-type wrappers — re-exported from internal/types.
func NewMap ¶
NewMap, NewArray, NewText, NewXmlFragment return wrappers bound to the root branch with the given name in d. The branch is lazily created on first call; subsequent calls with the same name return wrappers pointing at the same underlying state.
Per-branch type discipline: a branch should be used as ONE type (Map OR Array OR Text OR XML). Mixing types on the same root branch produces undefined behaviour.
type Options ¶
Options bundles per-Doc settings (deterministic ClientID, GC disable). The zero value is the recommended configuration.
type Transaction ¶
type Transaction = doc.Transaction
Transaction is a read-only transaction holding the doc's read lock for its lifetime. Created by Doc.ReadTxn; released by Close.
type TransactionMut ¶
type TransactionMut = doc.TransactionMut
TransactionMut is a write transaction holding the doc's write lock. Created by Doc.WriteTxn; released by Commit.
type XmlElement ¶
type XmlElement = types.XmlElement
Shared-type wrappers — re-exported from internal/types.
func NewXmlElement ¶
func NewXmlElement(d *Doc, name string) *XmlElement
NewXmlElement wraps a branch as an XmlElement. Typically used for root-level XML where the branch was constructed via d.Branch(name); nested elements should be constructed via XmlFragment.InsertXmlElement / XmlElement.InsertXmlElement which set TypeRef and Name automatically.
type XmlFragment ¶
type XmlFragment = types.XmlFragment
Shared-type wrappers — re-exported from internal/types.
func NewXmlFragment ¶
func NewXmlFragment(d *Doc, name string) *XmlFragment
Directories
¶
| Path | Synopsis |
|---|---|
|
Package benchmarks ports the dmonad/crdt-benchmarks B1-B4 workload suite to ygo.
|
Package benchmarks ports the dmonad/crdt-benchmarks B1-B4 workload suite to ygo. |
|
cmd
|
|
|
gen-go-fixtures
command
gen-go-fixtures generates reverse-direction wire-format fixtures: Go encodes Doc state via EncodeStateAsUpdate (V1) and EncodeStateAsUpdateV2, captures the bytes + expected state, and writes them as JSON.
|
gen-go-fixtures generates reverse-direction wire-format fixtures: Go encodes Doc state via EncodeStateAsUpdate (V1) and EncodeStateAsUpdateV2, captures the bytes + expected state, and writes them as JSON. |
|
ygo-server
command
Command ygo-server is the stand-alone WebSocket sync server for ygo documents.
|
Command ygo-server is the stand-alone WebSocket sync server for ygo documents. |
|
Package gomobile is the bytes-in/bytes-out subset of the ygo API designed to survive `gomobile bind`.
|
Package gomobile is the bytes-in/bytes-out subset of the ygo API designed to survive `gomobile bind`. |
|
internal
|
|
|
awareness
Package awareness implements the y-protocols Awareness layer — the ephemeral, presence-style sibling of the document CRDT used to track per-client transient state like cursor positions, user names, and selection ranges.
|
Package awareness implements the y-protocols Awareness layer — the ephemeral, presence-style sibling of the document CRDT used to track per-client transient state like cursor positions, user names, and selection ranges. |
|
block
Package block defines the building blocks of a Yjs document.
|
Package block defines the building blocks of a Yjs document. |
|
doc
Package doc owns the Doc and Transaction types — the document container plus its mutation-lifecycle wrapper.
|
Package doc owns the Doc and Transaction types — the document container plus its mutation-lifecycle wrapper. |
|
encoding
Package encoding implements the V1 wire format yrs and JS Yjs use for state vectors, delete sets, and document updates.
|
Package encoding implements the V1 wire format yrs and JS Yjs use for state vectors, delete sets, and document updates. |
|
lib0
Package lib0 implements the binary encoding format used by Yjs.
|
Package lib0 implements the binary encoding format used by Yjs. |
|
store
Package store implements the per-client block storage that owns the memory for every Item in a Yjs document.
|
Package store implements the per-client block storage that owns the memory for every Item in a Yjs document. |
|
sync
Package sync implements the y-protocols/sync wire format and the Hocuspocus outer message envelope.
|
Package sync implements the y-protocols/sync wire format and the Hocuspocus outer message envelope. |
|
types
Package types holds the user-facing shared CRDT collection types: Map, Array, Text, XmlElement / XmlFragment / XmlText.
|
Package types holds the user-facing shared CRDT collection types: Map, Array, Text, XmlElement / XmlFragment / XmlText. |
|
utf16
Package utf16 provides UTF-16 code-unit length and offset helpers over Go's UTF-8 strings.
|
Package utf16 provides UTF-16 code-unit length and offset helpers over Go's UTF-8 strings. |
|
Package persist defines the storage contract for ygo documents and provides helpers that turn stored update logs back into live Docs.
|
Package persist defines the storage contract for ygo documents and provides helpers that turn stored update logs back into live Docs. |
|
sqlite
Package sqlite is the reference persist.Store implementation backed by modernc.org/sqlite.
|
Package sqlite is the reference persist.Store implementation backed by modernc.org/sqlite. |
|
Package server implements the y-websocket / Hocuspocus-compatible WebSocket sync server for ygo documents.
|
Package server implements the y-websocket / Hocuspocus-compatible WebSocket sync server for ygo documents. |