OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library dart2js_incremental.watcher; | |
6 | |
7 import 'dart:io'; | |
8 | |
9 import 'dart:async'; | |
10 | |
11 class Watcher { | |
12 final Set<String> _watchedDirectories = new Set<String>(); | |
13 | |
14 final Map<String, Set<Uri>> _watchedFiles = new Map<String, Set<Uri>>(); | |
15 | |
16 final Set<Uri> _changes = new Set<Uri>(); | |
17 | |
18 bool _hasEarlyChanges = false; | |
19 | |
20 Completer<bool> _changesCompleter; | |
21 | |
22 Future<bool> hasChanges() { | |
23 if (_changesCompleter == null && _hasEarlyChanges) { | |
24 return new Future.value(true); | |
25 } | |
26 _changesCompleter = new Completer<bool>(); | |
27 return _changesCompleter.future; | |
28 } | |
29 | |
30 void _onFileSystemEvent(FileSystemEvent event) { | |
31 Set<Uri> uris = _watchedFiles[event.path]; | |
32 if (uris == null) return; | |
33 _changes.addAll(uris); | |
34 if (_changesCompleter == null) { | |
35 _hasEarlyChanges = true; | |
36 } else if (!_changesCompleter.isCompleted) { | |
37 _changesCompleter.complete(true); | |
38 } | |
39 } | |
40 | |
41 Map<Uri, Uri> readChanges() { | |
42 if (_changes.isEmpty) { | |
43 throw new StateError("No changes"); | |
44 } | |
45 Map<Uri, Uri> result = new Map<Uri, Uri>(); | |
46 for (Uri uri in _changes) { | |
47 result[uri] = uri; | |
48 } | |
49 _changes.clear(); | |
50 return result; | |
51 } | |
52 | |
53 void watchFile(Uri uri) { | |
54 String realpath = new File.fromUri(uri).resolveSymbolicLinksSync(); | |
55 _watchedFiles.putIfAbsent(realpath, () => new Set<Uri>()).add(uri); | |
56 Directory directory = new File(realpath).parent; | |
57 if (_watchedDirectories.add(directory.path)) { | |
58 print("Watching ${directory.path}"); | |
59 directory.watch().listen(_onFileSystemEvent); | |
60 } | |
61 } | |
62 } | |
OLD | NEW |