Documentation
¶
Overview ¶
Package yjs implements the Yjs CRDT protocol in Go. It provides Doc, Text, Map, Array, and XmlFragment types with binary protocol support (update-v1, sync, awareness) compatible with the JavaScript yjs library and y-websocket relay. update-v2 is not yet implemented; update-v1 is the only supported format for v0.1.x.
Index ¶
- func ApplyUpdate(d *Doc, update []byte, origin interface{}) error
- func EncodeStateAsUpdate(d *Doc, encodedStateVector []byte) ([]byte, error)
- func EncodeStateVector(d *Doc) ([]byte, error)
- type Array
- func (a *Array) Delete(index, length uint64)
- func (a *Array) Get(index uint64) (interface{}, bool)
- func (a *Array) Insert(index uint64, values ...interface{})
- func (a *Array) Len() uint64
- func (a *Array) Observe(fn func(*ArrayEvent))
- func (a *Array) Push(values ...interface{})
- func (a *Array) ToSlice() []interface{}
- type ArrayEvent
- type Awareness
- func (a *Awareness) ApplyUpdate(data []byte, origin interface{}) error
- func (a *Awareness) Destroy()
- func (a *Awareness) EncodeAwarenessUpdate() []byte
- func (a *Awareness) EncodeUpdate(clientIDs []uint64) []byte
- func (a *Awareness) GetLocalState() map[string]interface{}
- func (a *Awareness) GetStates() map[uint64]map[string]interface{}
- func (a *Awareness) OnChange(h ChangeHandler)
- func (a *Awareness) SetLocalState(state map[string]interface{})
- func (a *Awareness) SetLocalStateField(field string, value interface{})
- type ChangeHandler
- type Doc
- func (d *Doc) ClientID() uint64
- func (d *Doc) GetArray(name string) *Array
- func (d *Doc) GetMap(name string) *Map
- func (d *Doc) GetText(name string) *Text
- func (d *Doc) GetXmlFragment(name string) *XmlFragment
- func (d *Doc) OnUpdate(h UpdateHandler) func()
- func (d *Doc) Snapshot() ([]byte, error)
- func (d *Doc) Transact(fn func(), origin interface{})
- type ID
- type Item
- type Map
- type MapEvent
- type SharedType
- type StructStore
- type Text
- type TextEvent
- type UpdateHandler
- type XmlFragment
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyUpdate ¶
ApplyUpdate applies a v1 binary update received from a remote peer.
func EncodeStateAsUpdate ¶
EncodeStateAsUpdate encodes the full document state as a v1 update, optionally filtered to only items after encodedStateVector.
func EncodeStateVector ¶
EncodeStateVector returns the binary v1 state vector for document d.
Types ¶
type Array ¶
type Array struct {
// contains filtered or unexported fields
}
Array is a CRDT ordered sequence of arbitrary values.
func (*Array) Observe ¶
func (a *Array) Observe(fn func(*ArrayEvent))
Observe registers a handler for changes to this Array.
type ArrayEvent ¶
type ArrayEvent struct {
Origin interface{}
}
ArrayEvent carries information about a change to an Array type.
type Awareness ¶
type Awareness struct {
// contains filtered or unexported fields
}
Awareness implements the Yjs awareness protocol: ephemeral shared state (cursors, user info) keyed by clientID. The binary format is: varuint(count), (varuint(clientID), varuint(clock), varstring(JSON(state)))*.
func NewAwareness ¶
NewAwareness creates an Awareness instance bound to doc.
func (*Awareness) ApplyUpdate ¶
ApplyUpdate applies a remote awareness update binary.
func (*Awareness) Destroy ¶
func (a *Awareness) Destroy()
Destroy cleans up the awareness instance (removes local state).
func (*Awareness) EncodeAwarenessUpdate ¶
EncodeAwarenessUpdate encodes all current states (all known clients).
func (*Awareness) EncodeUpdate ¶
EncodeUpdate encodes the awareness states for the given client IDs as binary. Binary format: varuint(count), then (varuint(clientID), varuint(clock), varstring(JSON))*.
func (*Awareness) GetLocalState ¶
GetLocalState returns this client's current state, or nil if removed.
func (*Awareness) OnChange ¶
func (a *Awareness) OnChange(h ChangeHandler)
OnChange registers a handler for awareness state changes.
func (*Awareness) SetLocalState ¶
SetLocalState sets this client's local state. Pass nil to remove.
func (*Awareness) SetLocalStateField ¶
SetLocalStateField sets a single field in the local state map.
type ChangeHandler ¶
type ChangeHandler func(added, updated, removed []uint64, origin interface{})
ChangeHandler is called when awareness states change. added/updated/removed are slices of clientIDs that changed.
type Doc ¶
type Doc struct {
// contains filtered or unexported fields
}
Doc is the root Yjs document. It owns a StructStore, manages transactions, and holds named shared types (Text, Map, Array, XmlFragment).
func NewDoc ¶
func NewDoc() *Doc
NewDoc creates a new document with a random clientID.
ClientIDs are 32-bit unsigned integers (matching Yjs/lib0 behavior) so they remain within JavaScript's safe-integer range for cross-runtime interop.
func NewDocWithClientID ¶
NewDocWithClientID creates a document with a specific clientID. Useful for tests.
func RestoreDoc ¶ added in v0.1.1
RestoreDoc reconstructs a Doc from a snapshot produced by Doc.Snapshot.
func (*Doc) GetXmlFragment ¶
func (d *Doc) GetXmlFragment(name string) *XmlFragment
GetXmlFragment returns the named XmlFragment type, creating it if it does not exist. GetXmlFragment returns a shared XmlFragment rooted at the given name.
In v0.1.x the returned fragment supports only Len(); see the XmlFragment godoc. Use GetText, GetMap, or GetArray for functional shared types until XmlFragment mutation lands in v0.2.
func (*Doc) OnUpdate ¶
func (d *Doc) OnUpdate(h UpdateHandler) func()
OnUpdate registers a handler that is called after each transaction with the binary v1 update bytes that should be broadcast to peers.
It returns an unsubscribe function. Call it to remove the handler; subsequent transactions will not invoke it. This prevents handler leaks across connect/disconnect cycles when the same Doc is reused.
type ID ¶
ID uniquely identifies a struct in the document. Client is the peer's numeric ID; Clock is monotonically increasing per client.
type Item ¶
type Item struct {
ID ID
// Left and Right are the previous/next live items in insertion order.
Left *Item
Right *Item
// OriginLeft and OriginRight are the IDs of the items that were to the
// left and right of this item when it was created (causal context for
// the LSST conflict resolution algorithm).
OriginLeft *ID
OriginRight *ID
// Parent is the YType that owns this item.
Parent interface{} // *Text | *Map | *Array | etc.
// ParentSub is the map key when this item lives inside a Map.
ParentSub *string
// Content: one of string, []interface{}, etc.
Kind contentKind
Content interface{} // string for contentString, interface{} for contentAny, etc.
Length uint64
Deleted bool
// contains filtered or unexported fields
}
Item is a node in the doubly-linked list that forms a Yjs type's content. Each item corresponds to one Insert operation with its causal context (originLeft, originRight) for LSST-based conflict resolution.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a CRDT key-value store. Values are arbitrary Go values encoded as Yjs ContentAny. Only the most-recent item per key (by clock) is live.
type MapEvent ¶
type MapEvent struct {
// Origin is the transaction origin passed to Transact.
Origin interface{}
}
MapEvent carries information about a change to a Map type.
In v0.1.x the event carries only Origin. Per-key change details (action, old value, new value) are planned for v0.2.
type SharedType ¶
type SharedType interface {
// contains filtered or unexported methods
}
SharedType is implemented by Text, Map, Array, XmlFragment.
type StructStore ¶
type StructStore struct {
// contains filtered or unexported fields
}
StructStore holds all items per client, sorted by clock. This matches Yjs StructStore exactly.
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text is a CRDT text sequence. Internally it is a doubly-linked list of Items, each holding a string fragment.
func (*Text) Insert ¶
Insert inserts s at the given UTF-16 code-unit index (Yjs uses UTF-16 length). In practice for ASCII content, index equals the rune offset.
type TextEvent ¶
type TextEvent struct {
// Origin is the transaction origin passed to Transact.
Origin interface{}
}
TextEvent carries information about a change to a Text type.
In v0.1.x the event carries only Origin. Detailed delta computation (retain/insert/delete operations per the Yjs delta format) is planned for v0.2 once the observer infrastructure matures.
type UpdateHandler ¶
type UpdateHandler func(update []byte, origin interface{})
UpdateHandler is called whenever the local document produces a new update.
type XmlFragment ¶
type XmlFragment struct {
// contains filtered or unexported fields
}
XmlFragment represents a shared XML tree rooted in the document.
In v0.1.x, XmlFragment is a PLACEHOLDER: only Len() is implemented. Mutations (child insertion, attribute setting, iteration) are planned for v0.2. For current work use Text, Map, or Array.
func (*XmlFragment) Len ¶
func (x *XmlFragment) Len() uint64
Len returns the number of non-deleted child nodes.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package protocol implements the Yjs binary wire format: LEB128 unsigned varints, variable-length byte arrays, and UTF-8 strings, matching lib0/encoding exactly.
|
Package protocol implements the Yjs binary wire format: LEB128 unsigned varints, variable-length byte arrays, and UTF-8 strings, matching lib0/encoding exactly. |
|
Package transport — in-process test relay, reusable in Tier 2 integration tests.
|
Package transport — in-process test relay, reusable in Tier 2 integration tests. |