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; | 5 library watcher.directory_watcher; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:crypto/crypto.dart'; | 10 import 'package:crypto/crypto.dart'; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 111 |
112 _ready = new Completer(); | 112 _ready = new Completer(); |
113 } | 113 } |
114 | 114 |
115 /// Scans the contents of the directory once to see which files have been | 115 /// Scans the contents of the directory once to see which files have been |
116 /// added, removed, and modified. | 116 /// added, removed, and modified. |
117 void _poll() { | 117 void _poll() { |
118 _filesToProcess.clear(); | 118 _filesToProcess.clear(); |
119 _polledFiles.clear(); | 119 _polledFiles.clear(); |
120 | 120 |
121 endListing() { | |
122 assert(_state != _WatchState.UNSUBSCRIBED); | |
123 _listSubscription = null; | |
124 | |
125 // Null tells the queue consumer that we're done listing. | |
126 _filesToProcess.add(null); | |
127 } | |
128 | |
121 var stream = new Directory(directory).list(recursive: true); | 129 var stream = new Directory(directory).list(recursive: true); |
122 _listSubscription = stream.listen((entity) { | 130 _listSubscription = stream.listen((entity) { |
123 assert(_state != _WatchState.UNSUBSCRIBED); | 131 assert(_state != _WatchState.UNSUBSCRIBED); |
124 | 132 |
125 if (entity is! File) return; | 133 if (entity is! File) return; |
126 _filesToProcess.add(entity.path); | 134 _filesToProcess.add(entity.path); |
127 }, onDone: () { | 135 }, onError: (error) { |
128 assert(_state != _WatchState.UNSUBSCRIBED); | 136 // If an error occurred during the listing, it usually means the |
nweiz
2013/08/14 19:43:11
Check if the error is the error we expect from the
Bob Nystrom
2013/08/16 21:40:19
Done. Instead of printing it, I pipe the error to
| |
129 _listSubscription = null; | 137 // directory doesn't exist. We end the listing normally, which has the |
130 | 138 // desired effect of marking all files that were in the directory as |
131 // Null tells the queue consumer that we're done listing. | 139 // being removed. |
132 _filesToProcess.add(null); | 140 endListing(); |
133 }); | 141 }, onDone: endListing); |
134 } | 142 } |
135 | 143 |
136 /// Processes [file] to determine if it has been modified since the last | 144 /// Processes [file] to determine if it has been modified since the last |
137 /// time it was scanned. | 145 /// time it was scanned. |
138 Future _processFile(String file) { | 146 Future _processFile(String file) { |
139 assert(_state != _WatchState.UNSUBSCRIBED); | 147 assert(_state != _WatchState.UNSUBSCRIBED); |
140 | 148 |
141 // `null` is the sentinel which means the directory listing is complete. | 149 // `null` is the sentinel which means the directory listing is complete. |
142 if (file == null) return _completePoll(); | 150 if (file == null) return _completePoll(); |
143 | 151 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 | 266 |
259 class _FileStatus { | 267 class _FileStatus { |
260 /// The last time the file was modified. | 268 /// The last time the file was modified. |
261 DateTime modified; | 269 DateTime modified; |
262 | 270 |
263 /// The SHA-1 hash of the contents of the file. | 271 /// The SHA-1 hash of the contents of the file. |
264 List<int> hash; | 272 List<int> hash; |
265 | 273 |
266 _FileStatus(this.modified, this.hash); | 274 _FileStatus(this.modified, this.hash); |
267 } | 275 } |
OLD | NEW |