toBase method

Context toBase()

Takes a portion of the context tree from the current node (leaf) to the base.

Implementation

Context toBase() {
  final stack = <Context>[];
  Context context = this;
  while (context.parent != null) {
    stack.add(context);
    if (context.isBase) break;
    context = context.parent!;
  }

  Context? getParent(List<Context> stack) {
    final context = stack.firstOrNull;
    if (context == null) return null;
    return Context(
      current: context.current,
      isBase: stack.length == 1,
      key: context.key,
      parent: getParent(stack.skip(1).toList()),
    );
  }

  return getParent(stack)!;
}