traverseBFS method

bool traverseBFS(
  1. void stepCallback(
    1. AffogatoVFSEntity
    ), {
  2. String? startDirId,
})

Implementation

bool traverseBFS(
  void Function(AffogatoVFSEntity) stepCallback, {
  String? startDirId,
}) {
  final AffogatoVFSEntity? entity;
  if (startDirId != null) {
    entity = accessEntity(startDirId, isDir: true);
    if (entity == null) {
      return false;
    }
  } else {
    entity = api.workspace.workspaceConfigs.vfs.root;
  }
  final List<AffogatoVFSEntity> fileQueue = entity.files;
  final List<AffogatoVFSEntity> dirQueue = entity.subdirs;
  void iterateOverFiles() {
    for (final file in fileQueue) {
      stepCallback(file);
    }
    fileQueue.clear();
  }

  void iterateOverDirs() {
    final List<AffogatoVFSEntity> newDirQueue = [];
    for (final dir in dirQueue) {
      fileQueue.addAll(dir.files);
      newDirQueue.addAll(dir.subdirs);
      stepCallback(dir);
    }
    dirQueue
      ..clear()
      ..addAll(newDirQueue);
  }

  // Iterate until there are no more subdirectories
  while (dirQueue.isNotEmpty) {
    // Clear the queue of files from the parent directory/directories
    if (fileQueue.isNotEmpty) {
      iterateOverFiles();
    }
    // Expand the subdirectories, adding to each queue the items for the next iteration
    iterateOverDirs();
  }
  // Call this one last time to clear the files expanded from the last subdir
  iterateOverFiles();
  return true;
}