Index: sdk/lib/io/directory_impl.dart |
diff --git a/sdk/lib/io/directory_impl.dart b/sdk/lib/io/directory_impl.dart |
index 59911ed87f7808a79f8aa5db39673e41b0963c9b..5963d8390af08d1886e1df67d758de47d3d4afef 100644 |
--- a/sdk/lib/io/directory_impl.dart |
+++ b/sdk/lib/io/directory_impl.dart |
@@ -226,65 +226,20 @@ class _Directory implements Directory { |
return new Directory(newPath); |
} |
- DirectoryLister list({bool recursive: false}) { |
- return new _DirectoryLister(_path, recursive); |
- } |
- |
- List listSync({bool recursive: false}) { |
- if (_path is! String || recursive is! bool) { |
- throw new ArgumentError(); |
- } |
- return _list(_path, recursive); |
- } |
- |
- String get path => _path; |
- |
- String toString() => "Directory: '$path'"; |
- |
- bool _isErrorResponse(response) { |
- return response is List && response[0] != _SUCCESS_RESPONSE; |
- } |
- |
- _exceptionOrErrorFromResponse(response, String message) { |
- assert(_isErrorResponse(response)); |
- switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
- case _ILLEGAL_ARGUMENT_RESPONSE: |
- return new ArgumentError(); |
- case _OSERROR_RESPONSE: |
- var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
- response[_OSERROR_RESPONSE_ERROR_CODE]); |
- return new DirectoryIOException(message, _path, err); |
- default: |
- return new Exception("Unknown error"); |
- } |
- } |
- |
- void _ensureDirectoryService() { |
- if (_directoryService == null) { |
- _directoryService = _newServicePort(); |
- } |
- } |
- |
- final String _path; |
- SendPort _directoryService; |
-} |
- |
-class _DirectoryLister implements DirectoryLister { |
- _DirectoryLister(String path, bool recursive) { |
- const int LIST_DIRECTORY = 0; |
- const int LIST_FILE = 1; |
+ Stream<FileSystemEntity> list({bool recursive: false}) { |
+ const int LIST_FILE = 0; |
+ const int LIST_DIRECTORY = 1; |
const int LIST_ERROR = 2; |
const int LIST_DONE = 3; |
- final int RESPONSE_TYPE = 0; |
- final int RESPONSE_PATH = 1; |
- final int RESPONSE_COMPLETE = 1; |
- final int RESPONSE_ERROR = 2; |
+ const int RESPONSE_TYPE = 0; |
+ const int RESPONSE_PATH = 1; |
+ const int RESPONSE_COMPLETE = 1; |
+ const int RESPONSE_ERROR = 2; |
- List request = new List.fixedLength(3); |
- request[0] = _Directory.LIST_REQUEST; |
- request[1] = path; |
- request[2] = recursive; |
+ var controller = new StreamController<FileSystemEntity>(); |
+ |
+ List request = [ _Directory.LIST_REQUEST, path, recursive ]; |
ReceivePort responsePort = new ReceivePort(); |
// Use a separate directory service port for each listing as |
// listing operations on the same directory can run in parallel. |
@@ -292,21 +247,21 @@ class _DirectoryLister implements DirectoryLister { |
responsePort.receive((message, replyTo) { |
if (message is !List || message[RESPONSE_TYPE] is !int) { |
responsePort.close(); |
- _reportError(new DirectoryIOException("Internal error")); |
+ controller.signalError(new DirectoryIOException("Internal error")); |
return; |
} |
switch (message[RESPONSE_TYPE]) { |
- case LIST_DIRECTORY: |
- if (_onDir != null) _onDir(message[RESPONSE_PATH]); |
- break; |
case LIST_FILE: |
- if (_onFile != null) _onFile(message[RESPONSE_PATH]); |
+ controller.add(new File(message[RESPONSE_PATH])); |
+ break; |
+ case LIST_DIRECTORY: |
+ controller.add(new Directory(message[RESPONSE_PATH])); |
break; |
case LIST_ERROR: |
var errorType = |
message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
- _reportError(new ArgumentError()); |
+ controller.signalError(new ArgumentError()); |
} else if (errorType == _OSERROR_RESPONSE) { |
var responseError = message[RESPONSE_ERROR]; |
var err = new OSError( |
@@ -314,47 +269,59 @@ class _DirectoryLister implements DirectoryLister { |
responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
var errorPath = message[RESPONSE_PATH]; |
if (errorPath == null) errorPath = path; |
- _reportError(new DirectoryIOException("Directory listing failed", |
- errorPath, |
- err)); |
+ controller.signalError( |
+ new DirectoryIOException("Directory listing failed", |
+ errorPath, |
+ err)); |
} else { |
- _reportError(new DirectoryIOException("Internal error")); |
+ controller.signalError(new DirectoryIOException("Internal error")); |
} |
break; |
case LIST_DONE: |
responsePort.close(); |
- if (_onDone != null) _onDone(message[RESPONSE_COMPLETE]); |
+ controller.close(); |
break; |
} |
}); |
- } |
- void set onDir(void onDir(String dir)) { |
- _onDir = onDir; |
+ return controller.stream; |
} |
- void set onFile(void onFile(String file)) { |
- _onFile = onFile; |
+ List listSync({bool recursive: false}) { |
+ if (_path is! String || recursive is! bool) { |
+ throw new ArgumentError(); |
+ } |
+ return _list(_path, recursive); |
} |
- void set onDone(void onDone(bool completed)) { |
- _onDone = onDone; |
+ String get path => _path; |
+ |
+ String toString() => "Directory: '$path'"; |
+ |
+ bool _isErrorResponse(response) { |
+ return response is List && response[0] != _SUCCESS_RESPONSE; |
} |
- void set onError(void onError(e)) { |
- _onError = onError; |
+ _exceptionOrErrorFromResponse(response, String message) { |
+ assert(_isErrorResponse(response)); |
+ switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
+ case _ILLEGAL_ARGUMENT_RESPONSE: |
+ return new ArgumentError(); |
+ case _OSERROR_RESPONSE: |
+ var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
+ response[_OSERROR_RESPONSE_ERROR_CODE]); |
+ return new DirectoryIOException(message, _path, err); |
+ default: |
+ return new Exception("Unknown error"); |
+ } |
} |
- void _reportError(e) { |
- if (_onError != null) { |
- _onError(e); |
- } else { |
- throw e; |
+ void _ensureDirectoryService() { |
+ if (_directoryService == null) { |
+ _directoryService = _newServicePort(); |
} |
} |
- Function _onDir; |
- Function _onFile; |
- Function _onDone; |
- Function _onError; |
+ final String _path; |
+ SendPort _directoryService; |
} |