watchChanges method

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

Watch for changes in the current store

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.

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<ChangeType> events = const [
    ChangeType.ADD,
    ChangeType.MODIFY,
    ChangeType.REMOVE
  ],
  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([
    if (storeParts.contains(StoreParts.metadata))
      constructStream(_access.metadata),
    if (storeParts.contains(StoreParts.stats)) constructStream(_access.stats),
    if (storeParts.contains(StoreParts.tiles)) constructStream(_access.tiles),
  ]);

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