Y.Doc(conf)

A Yjs instance handles the state of shared data.

new Doc(conf)

Parameters:
Name Type Description
conf Object

configuration

Properties
Name Type Attributes Description
gc boolean <optional>

Disable garbage collection (default: gc=true)

gcFilter function <optional>

Will be called before an Item is garbage collected. Return false to keep the Item.

Extends

  • Observable.<string>

Members

_transaction :Transaction|null

_transactionCleanups :Array.<Transaction>

share :Map.<string, AbstractType.<YEvent>>

Methods

destroy()

Emit destroy event and unregister all event handlers.

get(name, TypeConstructor) → {AbstractType.<any>}

Define a shared data type.

Multiple calls of y.get(name, TypeConstructor) yield the same result and do not overwrite each other. I.e. y.define(name, Y.Array) === y.define(name, Y.Array)

After this method is called, the type is also available on y.share.get(name).

Best Practices: Define all types right after the Yjs instance is created and store them in a separate object. Also use the typed methods getText(name), getArray(name), ..

Parameters:
Name Type Description
name string
TypeConstructor function

The constructor of the type definition. E.g. Y.Text, Y.Array, Y.Map, ...

Returns:
AbstractType.<any> -

The created type. Constructed with TypeConstructor

Example
const y = new Y(..)
  const appState = {
    document: y.getText('document')
    comments: y.getArray('comments')
  }

getArray(nameopt) → {YArray.<T>}

Parameters:
Name Type Attributes Description
name string <optional>
Returns:
YArray.<T>

getMap(nameopt) → {YMap.<any>}

Parameters:
Name Type Attributes Description
name string <optional>
Returns:
YMap.<any>

getText(nameopt) → {YText}

Parameters:
Name Type Attributes Description
name string <optional>
Returns:
YText

getXmlFragment(nameopt) → {YXmlFragment}

Parameters:
Name Type Attributes Description
name string <optional>
Returns:
YXmlFragment

off(eventName, f)

Parameters:
Name Type Description
eventName string
f function

on(eventName, f)

Parameters:
Name Type Description
eventName string
f function

toJSON() → {Object.<string, any>}

Converts the entire document into a js object, recursively traversing each yjs type

Returns:
Object.<string, any>

transact(f, originopt)

Changes that happen inside of a transaction are bundled. This means that the observer fires after the transaction is finished and that all changes that happened inside of the transaction are sent as one message to the other peers.

Parameters:
Name Type Attributes Default Description
f function

The function that should be executed as a transaction

origin any <optional>
null

Origin of who started the transaction. Will be stored on transaction.origin