bindingOwnersOf static method

Map<Binding, String> bindingOwnersOf(
  1. List<GetPage> branch
)

Maps every binding instance carried by the leaf of branch (a tree branch as produced by matchRoute) to the name of the page that declared it.

Route-tree flattening merges each page's bindings into all of its descendants (_flattenChildren), so a page's binding list is its parent's merged list followed by its own declarations. This walks that prefix structure to attribute each binding to the branch page that declared it, letting a route report dependencies registered by an inherited (ancestor) binding against the ancestor's route instead of its own — a deep link to a nested page must not take ownership of its ancestors' controllers.

Bindings that do not follow the prefix structure (e.g. added by onBindingsStart later, or a list not produced by the flattening) are simply absent from the result and treated as declared by the page running them.

Implementation

static Map<Binding, String> bindingOwnersOf(List<GetPage> branch) {
  final owners = LinkedHashMap<Binding, String>.identity();
  if (branch.isEmpty) return owners;

  final root = branch.first;
  for (final binding in root.bindings) {
    owners.putIfAbsent(binding, () => root.name);
  }
  var prefixLength = root.bindings.length;

  for (final page in branch.skip(1)) {
    final bindings = page.bindings;
    if (bindings.length < prefixLength) break;
    for (var i = prefixLength; i < bindings.length; i++) {
      owners.putIfAbsent(bindings[i], () => page.name);
    }
    prefixLength = bindings.length;
  }
  return owners;
}