Class Storage<T>

The Storage class manages and synchronizes state within a Y.Doc.

Type Parameters

  • T extends SyncedStorageState

    Type parameter extending from SyncedStorageState.

Constructors

  • Initializes a new instance of the Storage class with a Y.Doc instance, a room identifier, and an initial state.

    Type Parameters

    • T extends SyncedStorageState

    Parameters

    • doc: Doc

      The Y.Doc instance for state synchronization.

    • roomId: string

      The identifier for the room, used in state syncing.

    • storageId: string

      The identifier for the storage, used in state syncing.

    • initialState: T

      The initial state of the storage.

    Returns Storage<T>

Properties

_state: YMap<T>
doc: Doc

The Y.Doc instance for state synchronization.

initialState: T

The initial state of the storage.

listeners: {
    [event: string]: Function[];
} = {}

Type declaration

  • [event: string]: Function[]
roomId: string

The identifier for the room, used in state syncing.

storageId: string

The identifier for the storage, used in state syncing.

Accessors

Methods

  • Deletes a given key or state from the synced storage.

    Parameters

    • given: Partial<T> | keyof T

      The key (as a keyof T) or state (as Partial) to delete.

    Returns void

    Example1

    By key: // state: { ban: false, id: "shape:xxxx" } // given: "ban" => state: { id: "shape:xxxx" }

    Example2

    By state: // state: { ban: false, id: "shape:xxxx" } // given: { ban: true } => state: { id: "shape:xxxx" }

  • Emits an event with the given arguments.

    Parameters

    • event: string

      The event name to emit.

    • Rest ...args: any[]

      Arguments to be passed to the event listeners.

    Returns void

  • Retrieves the current state.

    Returns T

    The current state as an instance of T.

  • Handles state changes and emits a stateChanged event.

    Parameters

    • event: YMapEvent<T>

      The YMapEvent object.

    Returns void

  • Initializes the state with the given initial state.

    Parameters

    • initialState: T

      The initial state to be set.

    Returns void

  • Removes a listener for an event.

    Parameters

    • event: string

      The event name.

    • listener: Function

      The listener function to remove.

    Returns void

  • Adds a listener for an event.

    Parameters

    • event: string

      The event name to listen for.

    • listener: Function

      The function to be called when the event is emitted.

    Returns (() => void)

    A function to remove the listener.

      • (): void
      • Returns void

  • Resets the state to the initial state.

    Returns void

  • Sets the state based on the provided new state.

    Parameters

    • newState: Partial<T>

      A partial state object representing the new state.

    Returns void

Generated using TypeDoc