text

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 4 Imported by: 6

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

View Source
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.

Functions

func ApplyTransform added in v0.7.0

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

ApplyTransform - Apply a specific transform to some content.

func FixOutOfDateTransform

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

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

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.

Types

type OTBuffer

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

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

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

func (m *OTBuffer) GetVersion() int

GetVersion - returns the current version of the document.

func (*OTBuffer) IsDirty

func (m *OTBuffer) IsDirty() bool

IsDirty - Check if there is any unapplied transforms.

func (*OTBuffer) PushTransform

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

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

func NewOTBufferConfig() OTBufferConfig

NewOTBufferConfig - Returns a default OTBufferConfig.

type OTransform

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.

Jump to

Keyboard shortcuts

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