ancestors method

Iterable<State<T>> ancestors({
  1. State<T>? upTo,
})

Finds the ancestors of a state

If upTo is null, returns the set of all ancestors (parents) from this up to (and including) the top of tree (parent == null). If upTo is not null, return all ancestors up to but not including upTo.

Special case: the sole ancestor of root is root

Implementation

Iterable<State<T>> ancestors({State<T>? upTo}) sync* {
  if (parent == null) {
    yield this;
    return;
  }
  if (upTo != null && upTo == this) return;
  var probe = parent;
  while (probe != null && upTo != probe) {
    yield probe;
    probe = probe.parent;
    if (probe == null) break;
  }
}