Y(room, conf)

A Yjs instance handles the state of shared data.

new Y(room, conf)

Parameters:
Name Type Description
room string

Users in the same room share the same content

conf Object

configuration

Members

room :String

The room name that this Yjs instance connects to.

Methods

define(name, TypeConstructor) → {Type}

Define a shared data type.

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

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

Best Practices: Either define all types right after the Yjs instance is created or always use y.define(..) when accessing a type.

Parameters:
Name Type Description
name String
TypeConstructor function

The constructor of the type definition

Returns:
Type -

The created type. Constructed with TypeConstructor

Example
// Option 1
  const y = new Y(..)
  y.define('myArray', YArray)
  y.define('myMap', YMap)
  // .. when accessing the type use y._map.get(name)
  y.share.myArray.insert(..)
  y.share.myMap.set(..)

  // Option2
  const y = new Y(..)
  // .. when accessing the type use `y.define(..)`
  y.define('myArray', YArray).insert(..)
  y.define('myMap', YMap).set(..)

destroy()

Disconnect from the room, and destroy all traces of this Yjs instance.

exportModel() → {ArrayBuffer}

Encode the Yjs model to ArrayBuffer

Returns:
ArrayBuffer -

The Yjs model as ArrayBuffer

get(name)

Get a defined type. The type must be defined locally. First define the type with define.

This returns the same value as y.share[name]

Parameters:
Name Type Description
name String

The typename

importModel(decoder)

Read the Decoder and fill the Yjs instance with data in the decoder.

Parameters:
Name Type Description
decoder Decoder

The BinaryDecoder to read from.

transact(f, remotenullable)

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

remote Boolean <nullable>
false

Optional. Whether this transaction is initiated by a remote peer. This should not be set manually! Defaults to false.