apply method

void apply()

Applies and clears all pending commands. Must not be called while a query is iterating.

Implementation

void apply() {
  assert(
    !_world.isQueryActive,
    'Commands.apply() called while a query is iterating.',
  );
  if (_ops.isEmpty) return;
  _world.beginFlush();
  // Counted before the command runs, and dropped in the `finally`: a
  // command that throws is discarded with the ones before it, so a failed
  // flush cannot replay work the world has already taken.
  var consumed = 0;
  try {
    // Include commands added during this flush.
    while (consumed < _ops.length) {
      final index = consumed++;
      final entity = _entities[index];
      switch (_ops[index]) {
        case _opInsert:
          _world.insertNowByType(_types[index], entity, _payloads[index]);
        case _opRemove:
          _world.removeNowByType(_types[index], entity);
        case _opDespawn:
          // A second despawn queued for the same entity finds it already
          // gone; skipping keeps deferred despawn idempotent (see [despawn]).
          if (_world.isAlive(entity)) _world.despawnNow(entity);
        case _opBundle:
          (_payloads[index] as SceneDashBundle).insertInto(_world, entity);
      }
    }
  } finally {
    _ops.removeRange(0, consumed);
    _entities.removeRange(0, consumed);
    _payloads.removeRange(0, consumed);
    _types.removeRange(0, consumed);
    _world.endFlush();
  }
}