Class LoroMap<T>

The handler of a map container.

Learn more at https://loro.dev/docs/tutorial/map

Type Parameters

  • T extends Record<string, unknown> = Record<string, unknown>

Constructors

  • Create a new detached LoroMap (not attached to any LoroDoc).

    The edits on a detached container will not be persisted. To attach the container to the document, please insert it into an attached container.

    Type Parameters

    • T extends Record<string, unknown> = Record<string, unknown>

    Returns LoroMap<T>

  • Returns LoroMap<T>

Properties

The container id of this handler.

size: number

Get the size of the map.

import { LoroDoc } from "loro-crdt";

const doc = new LoroDoc();
const map = doc.getMap("map");
map.set("foo", "bar");
console.log(map.size); // 1

Methods

  • Delete all key-value pairs in the map.

    Returns void

  • Parameters

    • key: string

    Returns void

  • Remove the key from the map.

    Parameters

    • key: string

    Returns void

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    map.delete("foo");
  • Get the entries of the map. If the value is a child container, the corresponding Container will be returned.

    Returns [string, Container | Value][]

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    map.set("baz", "bar");
    const entries = map.entries(); // [["foo", "bar"], ["baz", "bar"]]
  • Returns void

  • Get the value of the key. If the value is a child container, the corresponding Container will be returned.

    The object/value returned is a new js object/value each time because it need to cross the WASM boundary.

    Type Parameters

    • Key extends string | number | symbol

    Parameters

    Returns T[Key]

     import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    const bar = map.get("foo");
  • Get the attached container associated with this.

    Returns an attached Container that equals to this or created by this, otherwise undefined.

    Returns LoroMap<Record<string, unknown>>

  • Get the peer id of the last editor on the given entry

    Parameters

    • key: string

    Returns `${number}`

  • Get the value of the key. If the value is a child container, the corresponding Container will be returned.

    The object returned is a new js object each time because it need to cross

    Type Parameters

    Parameters

    • key: string
    • child: C

    Returns C

     import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    const bar = map.get("foo");
  • Get the shallow value of the map.

    Unlike toJSON() which recursively resolves all containers to their values, getShallowValue() returns container IDs as strings for any nested containers.

    Returns Record<string, Value>

    import { LoroDoc, LoroText } from "loro-crdt";

    const doc = new LoroDoc();
    doc.setPeerId("1");
    const map = doc.getMap("map");
    map.set("key", "value");
    const subText = map.setContainer("text", new LoroText());
    subText.insert(0, "Hello");

    // Get shallow value - nested containers are represented by their IDs
    console.log(map.getShallowValue());
    // Output: { key: "value", text: "cid:1@1:Text" }

    // Get full value with nested containers resolved by `toJSON()`
    console.log(map.toJSON());
    // Output: { key: "value", text: "Hello" }
  • Whether the container is attached to a document.

    If it's detached, the operations on the container will not be persisted.

    Returns boolean

  • Check if the container is deleted

    Returns boolean

  • Get the keys of the map.

    Returns any[]

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    map.set("baz", "bar");
    const keys = map.keys(); // ["foo", "baz"]
  • "Map"

    Returns "Map"

  • Get the parent container.

    • The parent container of the root tree is undefined.
    • The object returned is a new js object each time because it need to cross the WASM boundary.

    Returns Container

  • Set the key with the value.

    If the key already exists, its value will be updated. If the key doesn't exist, a new key-value pair will be created.

    Note: When calling map.set(key, value) on a LoroMap, if map.get(key) already returns value, the operation will be a no-op (no operation recorded) to avoid unnecessary updates.

    Type Parameters

    • Key extends string | number | symbol
    • V extends unknown

    Parameters

    Returns void

     import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    map.set("foo", "baz");
  • Set the key with a container.

    Type Parameters

    • C extends Container
    • Key extends string | number | symbol

    Parameters

    Returns Exclude<T[Key], null> extends C ? C & Exclude<T[Key], null> : C

     import { LoroDoc, LoroText, LoroList } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    const text = map.setContainer("text", new LoroText());
    const list = map.setContainer("list", new LoroList());
  • Get the keys and the values. If the type of value is a child container, it will be resolved recursively.

    Returns any

    import { LoroDoc, LoroText } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    const text = map.setContainer("text", new LoroText());
    text.insert(0, "Hello");
    console.log(map.toJSON()); // {"foo": "bar", "text": "Hello"}
  • Get the values of the map. If the value is a child container, the corresponding Container will be returned.

    Returns any[]

    import { LoroDoc } from "loro-crdt";

    const doc = new LoroDoc();
    const map = doc.getMap("map");
    map.set("foo", "bar");
    map.set("baz", "bar");
    const values = map.values(); // ["bar", "bar"]