ot

package
v0.0.0-...-0f24718 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2016 License: BSD-2-Clause Imports: 2 Imported by: 3

Documentation

Overview

Package ot provides operational transformation utilities for byte text collaboration.

The code is based on, and in part compatible with https://github.com/Operational-Transformation/ot.js by Tim Baumann (MIT License).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Doc  *Doc // the document
	Rev  int  // last acknowledged revision
	Wait Ops  // pending ops or nil
	Buf  Ops  // buffered ops or nil
	// Send is called when a new revision can be sent to the server.
	Send func(rev int, ops Ops)
}

Client represent a client document with synchronization mechanisms. The client has three states:

  1. A synchronized client sends applied ops immediately and …
  2. waits for an acknowledgement from the server, meanwhile buffering applied ops.
  3. The buffer is composed with new ops and sent immediately when the pending ack arrives.

func (*Client) Ack

func (c *Client) Ack() error

Ack acknowledges a pending server update and sends buffered updates if any. An error is returned if no update is pending.

func (*Client) Apply

func (c *Client) Apply(ops Ops) error

Apply applies ops to the document and buffers or sends the server update. An error is returned if the ops could not be applied.

func (*Client) Recv

func (c *Client) Recv(ops Ops) error

Recv receives server updates originating from other participants. An error is returned if the server update could not be applied.

type Doc

type Doc []byte

Doc represents a text document.

func (*Doc) Apply

func (doc *Doc) Apply(ops Ops) error

Apply applies the operation sequence ops to the document. An error is returned if applying ops failed.

type Op

type Op struct {
	// N signifies the operation type:
	// >  0: Retain  N bytes
	// <  0: Delete -N bytes
	// == 0: Noop or Insert string S
	N int
	S string
}

Op represents a single operation.

func (*Op) MarshalJSON

func (op *Op) MarshalJSON() ([]byte, error)

MarshalJSON encodes op either as json number or string.

func (*Op) UnmarshalJSON

func (op *Op) UnmarshalJSON(raw []byte) error

UnmarshalJSON decodes a json number or string into op.

type Ops

type Ops []Op

Ops represents a sequence of operations.

func Compose

func Compose(a, b Ops) (ab Ops, err error)

Compose returns an operation sequence composed from the consecutive ops a and b. An error is returned if the composition failed.

func Merge

func Merge(ops Ops) Ops

Merge attempts to merge consecutive operations in place and returns the sequence.

func Transform

func Transform(a, b Ops) (a1, b1 Ops, err error)

Transform returns two operation sequences derived from the concurrent ops a and b. An error is returned if the transformation failed.

func (Ops) Count

func (ops Ops) Count() (ret, del, ins int)

Count returns the number of retained, deleted and inserted bytes.

func (Ops) Equal

func (ops Ops) Equal(other Ops) bool

Equal returns if other equals ops.

type Server

type Server struct {
	Doc     *Doc
	History []Ops
}

Server represents shared document with revision history.

func (*Server) Recv

func (s *Server) Recv(rev int, ops Ops) (Ops, error)

Recv transforms, applies, and returns client ops and its revision. An error is returned if the ops could not be applied. Sending the derived ops to connected clients is the caller's responsibility.

func (*Server) Rev

func (s *Server) Rev() int

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL