syncRun method

R? syncRun()

Synchronous fast-path over attachSync only.

Implementation

R? syncRun() {
  if (_branches.isEmpty) {
    throw StateError('Select requires at least one branch');
  }

  // Filtrer guards
  final active = <SelectBranch<R>>[];
  for (final (b, g) in _branches) {
    if (g?.call() != false) active.add(b);
  }
  if (active.isEmpty) return null;

  // Fairness
  if (!_ordered && active.length > 1) {
    final n = active.length;
    final offset = DateTime.now().microsecondsSinceEpoch % n;
    final rotated = <SelectBranch<R>>[
      ...active.skip(offset),
      ...active.take(offset)
    ];
    active
      ..clear()
      ..addAll(rotated);
  }

  R? out;
  var done = false;

  void resolve(int i, Object? tag, FutureOr<R> v) {
    if (done) return;
    if (v is Future) return;
    done = true;
    out = v;
  }

  for (var i = 0; i < active.length; i++) {
    if (active[i].attachSync(resolve: resolve, index: i) && done) {
      return out;
    }
  }

  return null;
}