Home Reference Source
import Y from 'yjs/src/Y.js'
public class | source

Y

Extends:

NamedEventHandler → Y

A Yjs instance handles the state of shared data.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public
public

The AbstractConnector.that is used by this Yjs instance.

public

ds: *

public
public

os: *

public

The AbstractPersistence that is used by this Yjs instance.

public

The room name that this Yjs instance connects to.

public

share: {}

public

ss: *

public

userID: *

Method Summary

Public Methods
public

define(name: String, TypeConstructor: YType Constructor): YType

Define a shared data type.

public

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

public

disconnect(): *

Disconnect this Yjs Instance from the network.

public

get(name: String): *

Get a defined type.

public

reconnect(): *

If disconnected, tell the connector to reconnect to the room.

public

transact(f: Function, remote: Boolean)

Changes that happen inside of a transaction are bundled.

public

Inherited Summary

From class NamedEventHandler
public
public

emit(name: String, args: Array)

Emit a named event.

public

off(name: *, f: *)

Remove an event listener that was registered with either EventHandler#on or EventHandler#once.

public

on(name: String, f: Function)

Adds a named event listener.

public

once(name: String, f: Function)

Adds a named event listener.

public

when(name: *): *

Returns a Promise that is resolved when the event name is called.

Public Constructors

public constructor() source

Override:

NamedEventHandler#constructor

Public Members

public connected: boolean source

public connector: AbstractConnector source

The AbstractConnector.that is used by this Yjs instance.

public ds: * source

public gcEnabled: * source

public os: * source

public persistence: AbstractPersistence source

The AbstractPersistence that is used by this Yjs instance.

public room: String source

The room name that this Yjs instance connects to.

public share: {} source

public ss: * source

public userID: * source

Public Methods

public define(name: String, TypeConstructor: YType Constructor): YType source

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.share[name].

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

Params:

NameTypeAttributeDescription
name String
TypeConstructor YType Constructor

The constructor of the type definition

Return:

YType

The created type

Example:

  // Option 1
  const y = new Y(..)
  y.define('myArray', YArray)
  y.define('myMap', YMap)
  // .. when accessing the type use y.share[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(..)

public destroy() source

Disconnect from the room, and destroy all traces of this Yjs instance. Persisted data will remain until removed by the persistence adapter.

Override:

NamedEventHandler#destroy

public disconnect(): * source

Disconnect this Yjs Instance from the network. The connector will unsubscribe from the room and document updates are not shared anymore.

Return:

*

public get(name: String): * source

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]

Params:

NameTypeAttributeDescription
name String

The typename

Return:

*

public reconnect(): * source

If disconnected, tell the connector to reconnect to the room.

Return:

*

public transact(f: Function, remote: Boolean) source

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.

Params:

NameTypeAttributeDescription
f Function

The function that should be executed as a transaction

remote Boolean
  • nullable: true

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

public whenContentReady(): * source

Return:

*