Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TabSyncNetwork

Syncs updates to Collabs documents across different tabs for the same origin, using BroadcastChannel.

By default, this only forwards local operations to other tabs. Updates from other sources (e.g., a remote server via @collabs/ws-client) are not sent over the BroadcastChannel, since we expect that other tabs will get a copy from their own sources. You can override this with the allUpdates constructor option.

Likewise, our other providers do not forward or store operations from TabSyncNetwork. Instead, it is expected that each tab sets up its own providers to forward/store updates.

Hierarchy

Index

Constructors

  • new TabSyncNetwork(options?: { allUpdates?: boolean; bcName?: string }): TabSyncNetwork
  • Constructs a TabSyncNetwork.

    You typically only need one TabSyncNetwork per app, since it can subscribe multiple documents.

    Parameters

    • options: { allUpdates?: boolean; bcName?: string } = {}
      • Optional allUpdates?: boolean

        Set to true to forward all doc updates over the BroadcastChannel, not just local operations.

      • Optional bcName?: string

        The name of the BroadcastChannel to use. Default: "@collabs/tab-sync".

    Returns TabSyncNetwork

Properties

bcName: string

The name of this class's BroadcastChannel, set in the constructor.

Default: "@collabs/tab-sync".

isTabSyncNetwork: true = true

Methods

  • close(): void
  • Emits an event, which triggers all the registered event handlers.

    Event handlers are called in the order they are added. Errors in event handlers are captured and logged (with console.error), not propagated to the caller.

    Type Parameters

    • K extends "Error"

    Parameters

    Returns void

  • Registers an event handler that is triggered when the event happens.

    Type Parameters

    • K extends "Error"

    Parameters

    Returns (() => void)

    An "off" function that removes the event handler when called.

      • (): void
      • Registers an event handler that is triggered when the event happens.

        Returns void

        An "off" function that removes the event handler when called.

  • subscribe(doc: AbstractDoc | CRuntime, docID: string): void
  • Subscribes doc to updates for docID.

    doc will send and receive updates with other tabs that are subscribed to docID. It will also sync initial states with other tabs, to ensure that they start up-to-date.

    throws

    If doc is already subscribed to a docID.

    throws

    If another doc is subscribed to docID.

    Parameters

    • doc: AbstractDoc | CRuntime

      The document to subscribe.

    • docID: string

      An arbitrary string that identifies which updates to use.

    Returns void

  • unsubscribe(doc: AbstractDoc | CRuntime): void
  • Unsubscribes doc from its subscribed docID (if any).

    doc will no longer send or receive updates with other tabs.

    Parameters

    • doc: AbstractDoc | CRuntime

    Returns void

Generated using TypeDoc