Create a new Entity
.
By default the entity will have a new random ID and be initialize with no components.
Optional
id: EntityId | `leaf:${string}`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.
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 it's ID. It is important that no component use this key as
it's 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 it's ID will be the component ID.
Optional
options: { message?: string; origin?: string; timestamp?: number }Delete a component from the entity.
Get the component of the given type from the entity, or undefined
if the component is not on
the entity.
Get the component of the given type on the entity, initializing it with it's default value if it does not already exist on the entity.
Check whether the entity has a given component on it.
if (!entity.has(Name)) throw "Person must have name!";
Initialize a component with it's default value and add it to the entity, if the entity does not already have a component of that type.
An entity.
Entities are containers for collections of Components. All data in Leaf is stored in components that have been attached to
Entity
s.Example
For example, assuming you have defined a
Name
component as demonstrated in thedefComponent
example, you could add it to a new entity like so: