applyFiltersAndChecks function

List<PageLoaderElement> applyFiltersAndChecks(
  1. List<PageLoaderElement> elements,
  2. List<Filter> filters,
  3. List<Checker> checkers
)

Functionality shared in multiple PageLoader subclasses. Applies a list of filters and checks to a set of elements.

Returns filtered elements and throws exceptions if any checks fail.

Implementation

/// Applies a list of filters and checks to a set of elements.
///
/// Returns filtered elements and throws exceptions if any checks fail.
List<PageLoaderElement> applyFiltersAndChecks(List<PageLoaderElement> elements,
    List<Filter> filters, List<Checker> checkers) {
  // This doesn't work functionally due to asyncs.
  final filteredElements = <PageLoaderElement>[];
  for (final e in elements) {
    var keep = true;
    for (final filter in filters) {
      keep = keep && filter.keep(e);
    }
    if (keep) {
      filteredElements.add(e);
    }
  }

  for (final element in filteredElements) {
    for (final check in checkers) {
      if (!check.check(element)) {
        throw PageLoaderException(
            'Failed check: ${check.toString()}. '
            'Found "${element.name.toLowerCase()}" instead.',
            element);
      }
    }
  }

  return filteredElements;
}