leaps: github.com/Jeffail/leaps/lib/text Index | Files

package text

import "github.com/Jeffail/leaps/lib/text"

Package text - Contains logic for manipulating text based operational transforms, applying them to documents and caching them within state machines for easy management. This package has utilities for both servers and clients.


Package Files

ot_buffer.go package.go transforms.go


var (
    ErrTransformNegDelete = errors.New("transform contained negative delete")
    ErrTransformOOB       = errors.New("transform position was out of document bounds")
    ErrTransformTooLong   = errors.New("transform insert length exceeded the limit")
    ErrTransformTooOld    = errors.New("transform diff greater than transform archive")
    ErrTransformSkipped   = errors.New("transform version beyond latest")

Errors for the internal Operational Transform model.

func ApplyTransform Uses

func ApplyTransform(content *[]rune, ot *OTransform) error

ApplyTransform - Apply a specific transform to some content.

func FixOutOfDateTransform Uses

func FixOutOfDateTransform(sub, pre *OTransform)

FixOutOfDateTransform - When a transform created for a specific version is later determined to come after one or more other transforms it can be fixed. This fix translates the transform such that being applied in the correct order will preserve the original intention.

In order to apply these fixes this function should be called with the target transform and the actual versioned transform that the target currently 'believes' it is. So, for example, if the transform was written for version 7 and was actually 10 you would call FixOutOfDateTransform in this order:

FixOutOfDateTransform(target, version7) FixOutOfDateTransform(target, version8) FixOutOfDateTransform(target, version9)

Once the transform is adjusted through this fix it can be harmlessly dispatched to all other clients which will end up with the same document as the client that submitted this transform.

NOTE: These fixes do not regard or alter the versions of either transform.

func FixPrematureTransform Uses

func FixPrematureTransform(unapplied, unsent *OTransform)

FixPrematureTransform - Used by clients to fix incoming and outgoing transforms when local changes have been applied to a document before being routed through the server.

In order for a client UI to be unblocking it must apply local changes as the user types them before knowing the correct order of the change. Therefore, it is possible to apply a local change before receiving incoming transforms that are meant to be applied beforehand.

As a solution to those situations this function allows a client to alter and incoming interations such that if they were to be applied to the local document after our local change they would result in the same document. The outgoing transform is also modified for sending out to the server.

It is possible that the local change has already been dispatched to the server, in which case it is the servers responsibility to fix the transform so that other clients end up at the same result.

NOTE: These fixes do not regard or alter the versions of either transform.

func MergeTransforms Uses

func MergeTransforms(first, second *OTransform) bool

MergeTransforms - Takes two transforms (the next to be sent, and the one that follows) and attempts to merge them into one transform. This will not be possible with some combinations, and the function returns a boolean to indicate whether the merge was successful.

type OTBuffer Uses

type OTBuffer struct {
    Version   int
    Applied   []OTransform
    Unapplied []OTransform
    // contains filtered or unexported fields

OTBuffer - Buffers a growing stack of operational transforms, adjusting any out of date transforms as they are added. When the stack is flushed into a document any expired transforms are deleted, transforms that are not yet expired will be kept as they are used for adjustments.

func NewOTBuffer Uses

func NewOTBuffer(content string, config OTBufferConfig) *OTBuffer

NewOTBuffer - Create a buffer of operational transforms for a document set to version 1. Takes the initial content of the document in order to perform size and bounds checks on incoming transforms.

func (*OTBuffer) FlushTransforms Uses

func (m *OTBuffer) FlushTransforms(content *string, secondsRetention int64) (bool, error)

FlushTransforms - apply all unapplied transforms and append them to the applied stack, then remove old entries from the applied stack. Accepts retention as an indicator for how many seconds applied transforms should be retained. Returns a bool indicating whether any changes were applied.

func (*OTBuffer) GetVersion Uses

func (m *OTBuffer) GetVersion() int

GetVersion - returns the current version of the document.

func (*OTBuffer) IsDirty Uses

func (m *OTBuffer) IsDirty() bool

IsDirty - Check if there is any unapplied transforms.

func (*OTBuffer) PushTransform Uses

func (m *OTBuffer) PushTransform(ot OTransform) (OTransform, int, error)

PushTransform - Inserts a transform onto the unapplied stack and increments the version number of the document. Whilst doing so it fixes the transform in relation to earlier transforms it was unaware of, this fixed version gets sent back for distributing across other clients.

type OTBufferConfig Uses

type OTBufferConfig struct {
    MaxDocumentSize    uint64 `json:"max_document_size" yaml:"max_document_size"`
    MaxTransformLength uint64 `json:"max_transform_length" yaml:"max_transform_length"`

OTBufferConfig - Holds configuration options for a transform model.

func NewOTBufferConfig Uses

func NewOTBufferConfig() OTBufferConfig

NewOTBufferConfig - Returns a default OTBufferConfig.

type OTransform Uses

type OTransform struct {
    Position  int    `json:"position"`
    Delete    int    `json:"num_delete"`
    Insert    string `json:"insert"`
    Version   int    `json:"version"`
    TReceived int64  `json:"received,omitempty"`

OTransform - A representation of a transformation relating to a leaps document. This can either be a text addition, a text deletion, or both.

Package text imports 4 packages (graph) and is imported by 4 packages. Updated 2018-02-23. Refresh now. Tools for package owners.