watchChanges method

Stream<void> watchChanges({
  1. Duration? debounce = const Duration(milliseconds: 200),
  2. List<StoreDirectory> recursive = const [],
  3. List<ChangeType> events = const [ChangeType.ADD, ChangeType.MODIFY, ChangeType.REMOVE],
  4. List<RootParts> rootParts = RootParts.values,
  5. List<StoreParts> storeParts = StoreParts.values,
})

Watch for changes in the current cache

Useful to update UI only when required, for example, in a StreamBuilder. Whenever this has an event, it is likely the other statistics will have changed.

Recursively watch specific sub-stores (using StoreStats.watchChanges) by providing them as a list of StoreDirectorys to recursive. To watch all stores, use the storesAvailable/storesAvailableAsync getter as the argument. By default, no sub-stores are watched (empty list), meaning only top level changes (those to do with each store) will be caught.

Control which changes are caught through the events parameter, which takes a list of ChangeTypes. Catches all change types by default.

Enable debouncing to prevent unnecessary events for small changes in detail using debounce. Defaults to 200ms, or set to null to disable debouncing.

Debouncing example (dash roughly represents debounce):

input:  1-2-3---4---5-6-|
output: ------3---4-----6|

Implementation

Stream<void> watchChanges({
  Duration? debounce = const Duration(milliseconds: 200),
  List<StoreDirectory> recursive = const [],
  List<ChangeType> events = const [
    ChangeType.ADD,
    ChangeType.MODIFY,
    ChangeType.REMOVE,
  ],
  List<RootParts> rootParts = RootParts.values,
  List<StoreParts> storeParts = StoreParts.values,
}) {
  Stream<void> constructStream(Directory dir) => FileSystemEntity
          .isWatchSupported
      ? dir
          .watch(
            events: [
              events.contains(ChangeType.ADD) ? FileSystemEvent.create : null,
              events.contains(ChangeType.MODIFY)
                  ? FileSystemEvent.modify
                  : null,
              events.contains(ChangeType.MODIFY)
                  ? FileSystemEvent.move
                  : null,
              events.contains(ChangeType.REMOVE)
                  ? FileSystemEvent.delete
                  : null,
            ].whereType<int>().reduce((v, e) => v | e),
          )
          .map<void>((e) {})
      : DirectoryWatcher(dir.absolute.path)
          .events
          .where((evt) => events.contains(evt.type))
          .map<void>((e) {});

  final Stream<void> stream = constructStream(_access.real)
      .mergeAll(
    recursive.map(
      (e) => e.stats.watchChanges(
        debounce: debounce,
        events: events,
        storeParts: storeParts,
      ),
    ),
  )
      .mergeAll([
    if (rootParts.contains(RootParts.recovery))
      constructStream(_access.recovery),
    if (rootParts.contains(RootParts.stats)) constructStream(_access.stats),
    if (rootParts.contains(RootParts.stores)) constructStream(_access.stores),
  ]);

  return debounce == null ? stream : stream.debounce(debounce);
}