Skip to content

Commit

Permalink
Improve PartenFilter performance by detecting subtrees
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Mar 3, 2024
1 parent aa812fb commit 60867fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 9 additions & 6 deletions lib/spot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,15 @@ WidgetSelector<Widget> spotOffstage({
List<WidgetSelector> parents = const [],
List<WidgetSelector> children = const [],
}) {
return _global
.spot<Widget>(
parents: parents,
children: children,
)
.overrideIncludeOffstage(true);
return WidgetSelector(
stages: [
PredicateFilter(
predicate: (e) => true,
description: 'any Offstage Widget',
),
],
includeOffstage: true,
);
}

/// Creates a [WidgetSelector] that finds [widget] by identity and returns all
Expand Down
16 changes: 11 additions & 5 deletions lib/src/spot/filters/parent_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,28 @@ class ParentFilter implements ElementFilter {
continue;
}

for (final WidgetTreeNode node in parentSnapshot.discovered) {
// remove elements when the parent is already in the list. This prevents searching all element of a subtree, resulting in always the same items
final rootNodes = parentSnapshot.discovered
.whereNot(
(element) => parentSnapshot.discovered.contains(element.parent),
)
.toList();

for (final WidgetTreeNode node in rootNodes) {
groups[node] ??= [];

final root = node.isOffstage ? spotOffstage() : spotAllWidgets();
final subtree = tree.scope(node);
final s1 = WidgetSnapshot(
selector: root.withParent(parentSnapshot.selector),
final snapshot = WidgetSnapshot(
selector: root.withParent(spotElement(node.element)),
discovered: [
// TODO is returning the root correct?
node,
...subtree.allNodes,
],
scope: subtree,
debugCandidates: candidates.map((it) => it.element).toList(),
);
groups[node]!.add(s1);
groups[node]!.add(snapshot);
}

discoveryByParent.add(groups);
Expand Down

0 comments on commit 60867fe

Please sign in to comment.