Y.Doc(optsopt)

A Yjs instance handles the state of shared data.

new Doc(optsopt)

Parameters:
Name Type Attributes Description
opts DocOpts <optional>

configuration

Extends

  • Observable.<string>

Members

nullable _item :Item

If this document is a subdocument - a document integrated into another document - then _item is defined.

_transaction :Transaction|null

_transactionCleanups :Array.<Transaction>

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

subdocs :Set.<Doc>

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

load()

Notify the parent document that you request to load data into this subdocument (if it is a subdocument).

load() might be used in the future to request any provider to load the most current data.

It is safe to call load() multiple times.

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 Doesn't log types that have not been defined (using ydoc.getType(..)).

Returns:
Object.<string, any>
Deprecated:
  • Do not use this method and rather call toJSON directly on the shared types.

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