Chromium Code Reviews| Index: pkg/analysis_server/lib/src/resource.dart |
| diff --git a/pkg/analysis_server/lib/src/resource.dart b/pkg/analysis_server/lib/src/resource.dart |
| index baf7068de4b6e5dd73f4efc69f1ed61516fa96ab..46b33ca5342ee3c0d2732638e1d3843a6bcbd0a6 100644 |
| --- a/pkg/analysis_server/lib/src/resource.dart |
| +++ b/pkg/analysis_server/lib/src/resource.dart |
| @@ -244,8 +244,17 @@ class _MemoryFolder extends _MemoryResource implements Folder { |
| @override |
| Stream<WatchEvent> get changes { |
| - // TODO(paulberry): implement. |
| - return new StreamController<WatchEvent>().stream; |
| + if (_provider._pathToWatcher.containsKey(_path)) { |
| + // Two clients watching the same path is not yet supported. |
| + // TODO(paulberry): add support for this if needed. |
| + throw new StateError('Path "$_path" is already being watched for changes'); |
| + } |
| + var streamController = new StreamController<WatchEvent>(); |
|
scheglov
2014/05/28 18:22:28
AFAIK we use type annotations.
Paul Berry
2014/05/28 20:16:22
Done.
|
| + _provider._pathToWatcher[_path] = streamController; |
| + streamController.done.then((_) { |
| + _provider._pathToWatcher.remove(_path); |
| + }); |
| + return streamController.stream; |
| } |
| } |
| @@ -258,6 +267,8 @@ class MemoryResourceProvider implements ResourceProvider { |
| final Map<String, _MemoryResource> _pathToResource = <String, _MemoryResource>{}; |
| final Map<String, String> _pathToContent = <String, String>{}; |
| final Map<String, int> _pathToTimestamp = <String, int>{}; |
| + final Map<String, StreamController<WatchEvent>> _pathToWatcher = |
| + <String, StreamController<WatchEvent>>{}; |
| int nextStamp = 0; |
| @override |
| @@ -306,8 +317,40 @@ class MemoryResourceProvider implements ResourceProvider { |
| _pathToResource[path] = file; |
| _pathToContent[path] = content; |
| _pathToTimestamp[path] = nextStamp++; |
| + _notifyWatchers(path, ChangeType.ADD); |
| return file; |
| } |
| + |
| + void _notifyWatchers(String path, changeType) { |
|
scheglov
2014/05/28 18:22:28
Type annotation for "changeType".
Paul Berry
2014/05/28 20:16:22
Done.
|
| + _pathToWatcher.forEach((watcherPath, streamController) { |
|
scheglov
2014/05/28 18:22:28
I'm not 100% sure, but AFAIK we should put type an
Paul Berry
2014/05/28 20:16:22
Done.
|
| + if (posix.isWithin(watcherPath, path)) { |
| + streamController.add(new WatchEvent(changeType, path)); |
| + } |
| + }); |
| + } |
| + |
| + void modifyFile(String path, String content) { |
| + _checkFileAtPath(path); |
| + _pathToContent[path] = content; |
| + _pathToTimestamp[path] = nextStamp++; |
| + _notifyWatchers(path, ChangeType.MODIFY); |
| + } |
| + |
| + void _checkFileAtPath(String path) { |
| + var resource = _pathToResource[path]; |
| + if (resource is! _MemoryFile) { |
| + throw new ArgumentError( |
| + 'File expected at "$path" but ${resource.runtimeType} found'); |
| + } |
| + } |
| + |
| + void deleteFile(String path) { |
| + _checkFileAtPath(path); |
| + _pathToResource.remove(path); |
| + _pathToContent.remove(path); |
| + _pathToTimestamp.remove(path); |
| + _notifyWatchers(path, ChangeType.REMOVE); |
| + } |
| } |