mutate<T extends Component> method

Option<T> mutate<T extends Component>(
  1. WorldEntity entity,
  2. T update(
    1. T current
    )
)

Replaces the component of type T using update. Returns the new value, or None if entity has no component of type T.

The new value is written into the slot keyed by T even when update returns a subclass — get<T>() stays stable instead of leaving the old T alongside a parallel subclass entry.

Implementation

Option<T> mutate<T extends Component>(
  WorldEntity entity,
  T Function(T current) update,
) {
  return switch (_readComponent<T>(entity)) {
    Some(value: final current) => () {
        final next = update(current);
        registry.setDependency(
          Dependency(
            Sync.okValue(next),
            metadata: Some(
              DependencyMetadata(
                groupEntity: entity,
                preemptivetypeEntity: TypeEntity(T),
              ),
            ),
          ),
        );
        return Some(next);
      }(),
    None() => const None(),
  };
}