Skip to content

Commit

Permalink
Fix registerCustomWatcher to allow nulls (dart-lang/watcher#107)
Browse files Browse the repository at this point in the history
The idea behind the closures passed to `registerCustomWatcher` is that
they should return `null` if the particular implementation does not
support the provided path (in which case we should fallback to other
implementations).

Modified a test to check this.
  • Loading branch information
michalt authored Jan 7, 2021
1 parent 5e3fce1 commit 07b47f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions pkgs/watcher/lib/src/custom_watcher_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class _CustomWatcherFactory {
/// will be used instead of the default.
void registerCustomWatcher(
String id,
DirectoryWatcher Function(String path, {Duration? pollingDelay})?
DirectoryWatcher? Function(String path, {Duration? pollingDelay})?
createDirectoryWatcher,
FileWatcher Function(String path, {Duration? pollingDelay})?
FileWatcher? Function(String path, {Duration? pollingDelay})?
createFileWatcher,
) {
if (_customWatcherFactories.containsKey(id)) {
Expand Down
30 changes: 22 additions & 8 deletions pkgs/watcher/test/custom_watcher_factory_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import 'package:watcher/watcher.dart';

void main() {
late _MemFs memFs;
final defaultFactoryId = 'MemFs';
final memFsFactoryId = 'MemFs';
final noOpFactoryId = 'NoOp';

setUpAll(() {
memFs = _MemFs();
var watcherFactory = _MemFsWatcherFactory(memFs);
var memFsWatcherFactory = _MemFsWatcherFactory(memFs);
var noOpWatcherFactory = _NoOpWatcherFactory();
registerCustomWatcher(
defaultFactoryId,
watcherFactory.createDirectoryWatcher,
watcherFactory.createFileWatcher);
noOpFactoryId,
noOpWatcherFactory.createDirectoryWatcher,
noOpWatcherFactory.createFileWatcher);
registerCustomWatcher(
memFsFactoryId,
memFsWatcherFactory.createDirectoryWatcher,
memFsWatcherFactory.createFileWatcher);
});

test('notifies for files', () async {
Expand Down Expand Up @@ -44,7 +50,7 @@ void main() {

test('registering twice throws', () async {
expect(
() => registerCustomWatcher(defaultFactoryId,
() => registerCustomWatcher(memFsFactoryId,
(_, {pollingDelay}) => throw 0, (_, {pollingDelay}) => throw 0),
throwsA(isA<ArgumentError>()));
});
Expand Down Expand Up @@ -116,10 +122,18 @@ class _MemFsWatcherFactory {
final _MemFs _memFs;
_MemFsWatcherFactory(this._memFs);

DirectoryWatcher createDirectoryWatcher(String path,
DirectoryWatcher? createDirectoryWatcher(String path,
{Duration? pollingDelay}) =>
_MemFsWatcher(path, _memFs.watchStream(path));

FileWatcher createFileWatcher(String path, {Duration? pollingDelay}) =>
FileWatcher? createFileWatcher(String path, {Duration? pollingDelay}) =>
_MemFsWatcher(path, _memFs.watchStream(path));
}

class _NoOpWatcherFactory {
DirectoryWatcher? createDirectoryWatcher(String path,
{Duration? pollingDelay}) =>
null;

FileWatcher? createFileWatcher(String path, {Duration? pollingDelay}) => null;
}

0 comments on commit 07b47f3

Please sign in to comment.