Leaf
    Preparing search index...

    An entity.

    Entities are containers for collections of Components. All data in Leaf is stored in components that have been attached to Entitys.

    For example, assuming you have defined a Name component as demonstrated in the defComponent example, you could add it to a new entity like so:

    const ent = new Entity();

    const name = ent.getOrInit(Name)
    name.set("first", "John")
    name.set("last", "Smith")
    Index

    Constructors

    • Create a new Entity.

      By default the entity will have a new random ID and be initialize with no components.

      Parameters

      • Optionalid: IntoEntityId

        Specifying the ID will create an entity with a specific ID instead of a random ID. The document will sill be empty, by default, so if you are loading a specific entity you may need to load the entity with a StorageManager, for instance.

      Returns Entity

    Accessors

    • get doc(): EntityDoc

      Get the internal LoroDoc.

      This can be used to do anything that Loro allows, such as exporting snapshots, doing time travel, etc.

      Note: The Entity's components are stored in this doc. If you modify the components directly through the LoroDoc you may confuse the normal entity API when it tries to read or write components.

      In the Loro doc the Entity stores one special container using the value in entityComponentsKey as its ID. It is important that no component use this key as its ID.

      This container will be a map that contains an entry for each component ID on the entity. The value will always be true. For every key in that map, the component is considered to be on the entity.

      Every other root container in the Loro document will be a component, and its ID will be the component ID.

      Returns EntityDoc

    Methods

    • Parameters

      • Optionaloptions: null | { message?: string; origin?: string; timestamp?: number }

      Returns void

    • Manually garbage collect this entity. Shouldn't usually be necessary to call this but can be used to free memory more quickly instead of waiting for the periodic collector to come clean it up.

      Returns void

    • Get the component of the given type on the entity, initializing it with its default value if it does not already exist on the entity.

      The handler function will be immediately called with the retrieved component and the return value of that handler will be returned from by this function.

      This allows us to automatically reclaim the component accessor memory without waiting for the garbage collector.

      Type Parameters

      Parameters

      Returns R

    • Register a callback that will be run when the entity is committed.

      Parameters

      • listener: () => void

      Returns () => void

      A function that may be called to unregister the callback.