updateComponent<T extends Component> method

Result<Component, String> updateComponent<T extends Component>(
  1. Entity<Object> entity,
  2. T newComponent
)

Updates the component of type T for the given entity. Returns an Ok with the updated component or an Err if the component does not exist.

Implementation

Result<Component, String> updateComponent<T extends Component>(
  Entity entity,
  T newComponent,
) {
  final dependency = _di.getDependencyOrNull<T>(groupEntity: entity);
  if (dependency != null) {
    _di.setDependency(
      Dependency<T>(
        newComponent,
        metadata: dependency.metadata,
      ),
    );
    return Ok(newComponent);
  } else {
    return Err(
      'Component of type $T does not exist for entity $entity.',
    );
  }
}