recursiveDelete method

void recursiveDelete(
  1. String path
)

Deletes the given path from the data store recursively. Documents under the given path could be stored in one of two places:

  1. In resolver paths under/equal to the given path. These documents can easily be deleted by deleting all value stores under the document path in the resolver.

  2. In a resolver path that is a parent path of the given path. Ex. When deleting path users__1, all user documents might be stored in resolver path "users", or if no custom persistence key has been specified anywhere along the path, then in the default store.

    Therefore to delete the remaining documents under the given path, each value store in the resolver above the given path is visited and has the given path evicted from its store.

Implementation

void recursiveDelete(String path) async {
  _assertHydrated();

  // 1. Delete the given path from the resolver, evicting all documents under that path that were stored in
  //    resolver paths at or under that path.
  if (_storeResolver.hasPath(path)) {
    _storeResolver.delete(path);
    isDirty = true;
  }

  // 2. Evict the given path from any parent stores above the given path.
  final stores = _storeResolver.extractParentPath(path);
  for (final entry in stores.entries) {
    final resolverPath = entry.key;
    final store = entry.value;

    if (store.hasPath(path)) {
      store.delete(path);
      isDirty = true;

      if (store.isEmpty) {
        _storeResolver.delete(resolverPath, recursive: false);
      }

      // Data under the given path can only exist in one parent path store at a time, so deletion can exit early
      // once a parent path is found.
      break;
    }
  }
}