| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library watcher.directory_watcher.mac_os; | 5 library watcher.directory_watcher.mac_os; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import '../constructable_file_system_event.dart'; | 10 import '../constructable_file_system_event.dart'; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 /// | 66 /// |
| 67 /// These are gathered together so that they may all be canceled when the | 67 /// These are gathered together so that they may all be canceled when the |
| 68 /// watcher is closed. This does not include [_watchSubscription]. | 68 /// watcher is closed. This does not include [_watchSubscription]. |
| 69 final _subscriptions = new Set<StreamSubscription>(); | 69 final _subscriptions = new Set<StreamSubscription>(); |
| 70 | 70 |
| 71 _MacOSDirectoryWatcher(String directory) | 71 _MacOSDirectoryWatcher(String directory) |
| 72 : directory = directory, | 72 : directory = directory, |
| 73 _files = new PathSet(directory) { | 73 _files = new PathSet(directory) { |
| 74 _startWatch(); | 74 _startWatch(); |
| 75 | 75 |
| 76 _listen(new Directory(directory).list(recursive: true), | 76 _listen(Chain.track(new Directory(directory).list(recursive: true)), |
| 77 (entity) { | 77 (entity) { |
| 78 if (entity is! Directory) _files.add(entity.path); | 78 if (entity is! Directory) _files.add(entity.path); |
| 79 }, | 79 }, |
| 80 onError: _emitError, | 80 onError: _emitError, |
| 81 onDone: _readyCompleter.complete, | 81 onDone: _readyCompleter.complete, |
| 82 cancelOnError: true); | 82 cancelOnError: true); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void close() { | 85 void close() { |
| 86 for (var subscription in _subscriptions) { | 86 for (var subscription in _subscriptions) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 102 _eventsBasedOnFileSystem(path) : [canonicalEvent]; | 102 _eventsBasedOnFileSystem(path) : [canonicalEvent]; |
| 103 | 103 |
| 104 for (var event in events) { | 104 for (var event in events) { |
| 105 if (event is FileSystemCreateEvent) { | 105 if (event is FileSystemCreateEvent) { |
| 106 if (!event.isDirectory) { | 106 if (!event.isDirectory) { |
| 107 _emitEvent(ChangeType.ADD, path); | 107 _emitEvent(ChangeType.ADD, path); |
| 108 _files.add(path); | 108 _files.add(path); |
| 109 continue; | 109 continue; |
| 110 } | 110 } |
| 111 | 111 |
| 112 _listen(new Directory(path).list(recursive: true), (entity) { | 112 _listen(Chain.track(new Directory(path).list(recursive: true)), |
| 113 (entity) { |
| 113 if (entity is Directory) return; | 114 if (entity is Directory) return; |
| 114 _emitEvent(ChangeType.ADD, entity.path); | 115 _emitEvent(ChangeType.ADD, entity.path); |
| 115 _files.add(entity.path); | 116 _files.add(entity.path); |
| 116 }, onError: _emitError, cancelOnError: true); | 117 }, onError: _emitError, cancelOnError: true); |
| 117 } else if (event is FileSystemModifyEvent) { | 118 } else if (event is FileSystemModifyEvent) { |
| 118 assert(!event.isDirectory); | 119 assert(!event.isDirectory); |
| 119 _emitEvent(ChangeType.MODIFY, path); | 120 _emitEvent(ChangeType.MODIFY, path); |
| 120 } else { | 121 } else { |
| 121 assert(event is FileSystemDeleteEvent); | 122 assert(event is FileSystemDeleteEvent); |
| 122 for (var removedPath in _files.remove(path)) { | 123 for (var removedPath in _files.remove(path)) { |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 for (var file in _files.toSet()) { | 308 for (var file in _files.toSet()) { |
| 308 _emitEvent(ChangeType.REMOVE, file); | 309 _emitEvent(ChangeType.REMOVE, file); |
| 309 } | 310 } |
| 310 _files.clear(); | 311 _files.clear(); |
| 311 close(); | 312 close(); |
| 312 } | 313 } |
| 313 | 314 |
| 314 /// Start or restart the underlying [Directory.watch] stream. | 315 /// Start or restart the underlying [Directory.watch] stream. |
| 315 void _startWatch() { | 316 void _startWatch() { |
| 316 // Batch the FSEvent changes together so that we can dedup events. | 317 // Batch the FSEvent changes together so that we can dedup events. |
| 317 var innerStream = new Directory(directory).watch(recursive: true).transform( | 318 var innerStream = |
| 318 new BatchedStreamTransformer<FileSystemEvent>()); | 319 Chain.track(new Directory(directory).watch(recursive: true)) |
| 320 .transform(new BatchedStreamTransformer<FileSystemEvent>()); |
| 319 _watchSubscription = innerStream.listen(_onBatch, | 321 _watchSubscription = innerStream.listen(_onBatch, |
| 320 onError: _eventsController.addError, | 322 onError: _eventsController.addError, |
| 321 onDone: _onDone); | 323 onDone: _onDone); |
| 322 } | 324 } |
| 323 | 325 |
| 324 /// Emit an event with the given [type] and [path]. | 326 /// Emit an event with the given [type] and [path]. |
| 325 void _emitEvent(ChangeType type, String path) { | 327 void _emitEvent(ChangeType type, String path) { |
| 326 if (!isReady) return; | 328 if (!isReady) return; |
| 327 | 329 |
| 328 // Don't emit ADD events for files that we already know about. Such an event | 330 // Don't emit ADD events for files that we already know about. Such an event |
| (...skipping 15 matching lines...) Expand all Loading... |
| 344 void _listen(Stream stream, void onData(event), {Function onError, | 346 void _listen(Stream stream, void onData(event), {Function onError, |
| 345 void onDone(), bool cancelOnError}) { | 347 void onDone(), bool cancelOnError}) { |
| 346 var subscription; | 348 var subscription; |
| 347 subscription = stream.listen(onData, onError: onError, onDone: () { | 349 subscription = stream.listen(onData, onError: onError, onDone: () { |
| 348 _subscriptions.remove(subscription); | 350 _subscriptions.remove(subscription); |
| 349 if (onDone != null) onDone(); | 351 if (onDone != null) onDone(); |
| 350 }, cancelOnError: cancelOnError); | 352 }, cancelOnError: cancelOnError); |
| 351 _subscriptions.add(subscription); | 353 _subscriptions.add(subscription); |
| 352 } | 354 } |
| 353 } | 355 } |
| OLD | NEW |