Y.Y(conf)

A Yjs instance handles the state of shared data.

new Y(conf)

Parameters:
Name Type Description
conf Object | undefined

configuration

Extends

  • Observable.<string>

Members

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

Methods

protected 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

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(name) → {YArray.<T>}

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

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

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

getText(name) → {YText}

Parameters:
Name Type Description
name string
Returns:
YText

getXmlFragment(name) → {YXmlFragment}

Parameters:
Name Type Description
name string
Returns:
YXmlFragment

off(eventName, f)

Parameters:
Name Type Description
eventName string
f function

on(eventName, f)

Parameters:
Name Type Description
eventName string
f function

transact(f)

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 Description
f function

The function that should be executed as a transaction