Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Unified Diff: runtime/bin/directory_impl.dart

Issue 8965036: Add asynchronous versions of the methods on Directory. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/directory.dart ('k') | tests/standalone/src/DirectoryTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/directory_impl.dart
diff --git a/runtime/bin/directory_impl.dart b/runtime/bin/directory_impl.dart
index 19c2a0a0f13066214256dea50d2ac687a4426018..952316a63466377ba0e578cf397f3a15d9261949 100644
--- a/runtime/bin/directory_impl.dart
+++ b/runtime/bin/directory_impl.dart
@@ -36,32 +36,150 @@ class _DirectoryListingIsolate extends Isolate {
}
-class _DirectoryCreateTempIsolate extends Isolate {
+class _DirectoryOperation {
+ abstract void execute(ReceivePort port);
- _DirectoryCreateTempIsolate() : super.heavy();
+ SendPort set replyPort(SendPort port) {
+ _replyPort = port;
+ }
+
+ SendPort _replyPort;
+}
+
+
+class _DirExitOperation extends _DirectoryOperation {
+ void execute(ReceivePort port) {
+ port.close();
+ }
+}
+
+
+class _DirExistsOperation extends _DirectoryOperation {
+ _DirExistsOperation(String this._path);
+
+ void execute(ReceivePort port) {
+ _replyPort.send(_Directory._exists(_path), port.toSendPort());
+ }
+
+ String _path;
+}
+
+
+class _DirCreateOperation extends _DirectoryOperation {
+ _DirCreateOperation(String this._path);
+
+ void execute(ReceivePort port) {
+ _replyPort.send(_Directory._create(_path), port.toSendPort());
+ }
+
+ String _path;
+}
+
+
+class _DirCreateTempOperation extends _DirectoryOperation {
+ _DirCreateTempOperation(String this._path);
+
+ void execute(ReceivePort port) {
+ var status = new _OSStatus();
+ var result = _Directory._createTemp(_path, status);
+ if (result == null) {
+ _replyPort.send(status, port.toSendPort());
+ } else {
+ _replyPort.send(result, port.toSendPort());
+ }
+ }
+
+ String _path;
+}
+
+
+class _DirDeleteOperation extends _DirectoryOperation {
+ _DirDeleteOperation(String this._path);
+
+ void execute(ReceivePort port) {
+ _replyPort.send(_Directory._delete(_path), port.toSendPort());
+ }
+
+ String _path;
+}
+
+
+class _DirectoryOperationIsolate extends Isolate {
+ _DirectoryOperationIsolate() : super.heavy();
+
+ void handleOperation(_DirectoryOperation message, SendPort ignored) {
+ message.execute(port);
+ port.receive(handleOperation);
+ }
void main() {
- port.receive((path, replyTo) {
- // Call function to get file name
- var status = new _OSStatus();
- var result = _Directory._createTemp(path, status);
- if (result == null) {
- replyTo.send(status);
- } else {
- replyTo.send(result);
- }
- port.close();
- });
+ port.receive(handleOperation);
}
}
+class _DirectoryOperationScheduler {
+ _DirectoryOperationScheduler() : _queue = new Queue();
+
+ void schedule(SendPort port) {
+ assert(_isolate != null);
+ if (_queue.isEmpty()) {
+ port.send(new _DirExitOperation());
+ _isolate = null;
+ } else {
+ port.send(_queue.removeFirst());
+ }
+ }
+
+ void scheduleWrap(void callback(result, ignored)) {
+ return (result, replyTo) {
+ callback(result, replyTo);
+ schedule(replyTo);
+ };
+ }
+
+ void enqueue(_DirectoryOperation operation, void callback(result, ignored)) {
+ ReceivePort replyPort = new ReceivePort.singleShot();
+ replyPort.receive(scheduleWrap(callback));
+ operation.replyPort = replyPort.toSendPort();
+ _queue.addLast(operation);
+ if (_isolate == null) {
+ _isolate = new _FileOperationIsolate();
+ _isolate.spawn().then((port) {
+ schedule(port);
+ });
+ }
+ }
+
+ Queue<_DirectoryOperation> _queue;
+ _DirectoryOperationIsolate _isolate;
+}
+
+
class _Directory implements Directory {
- _Directory(String this._path);
+ _Directory(String this._path)
+ : _scheduler = new _DirectoryOperationScheduler();
static String _createTemp(String template,
_OSStatus status) native "Directory_CreateTemp";
+ static int _exists(String path) native "Directory_Exists";
+ static bool _create(String path) native "Directory_Create";
+ static bool _delete(String path) native "Directory_Delete";
+
+ void exists() {
+ var handler = (_existsHandler != null) ? _existsHandler : (result) => null;
+ var operation = new _DirExistsOperation(_path);
+ _scheduler.enqueue(operation, (result, ignored) {
+ if (result < 0) {
+ if (_errorHandler != null) {
+ _errorHandler("Diretory exists test failed: $_path");
+ }
+ } else {
+ handler(result == 1);
+ }
+ });
+ }
bool existsSync() {
int exists = _exists(_path);
@@ -71,6 +189,18 @@ class _Directory implements Directory {
return (exists == 1);
}
+ void create() {
+ var handler = (_createHandler != null) ? _createHandler : () => null;
+ var operation = new _DirCreateOperation(_path);
+ _scheduler.enqueue(operation, (result, ignored) {
+ if (result) {
+ handler();
+ } else if (_errorHandler != null) {
+ _errorHandler("Directory creation failed: $_path");
+ }
+ });
+ }
+
void createSync() {
if (!_create(_path)) {
throw new DirectoryException("Directory creation failed: $_path");
@@ -78,20 +208,17 @@ class _Directory implements Directory {
}
void createTemp() {
- new _DirectoryCreateTempIsolate().spawn().then((port) {
- port.call(_path).receive((result, ignored) {
- if (result is !_OSStatus) {
- _path = result;
- if (_createTempHandler !== null) {
- _createTempHandler();
- }
- } else {
- if (_errorHandler !== null) {
- _errorHandler("Could not create temporary directory [$_path]: " +
- "${result._errorMessage}");
- }
- }
- });
+ var handler =
+ (_createTempHandler != null) ? _createTempHandler : () => null;
+ var operation = new _DirCreateTempOperation(_path);
+ _scheduler.enqueue(operation, (result, ignored) {
+ if (result is !_OSStatus) {
+ _path = result;
+ handler();
+ } else if (_errorHandler !== null) {
+ _errorHandler("Could not create temporary directory [$_path]: " +
+ "${result._errorMessage}");
+ }
});
}
@@ -108,6 +235,18 @@ class _Directory implements Directory {
}
}
+ void delete() {
+ var handler = (_deleteHandler != null) ? _deleteHandler : () => null;
+ var operation = new _DirDeleteOperation(_path);
+ _scheduler.enqueue(operation, (result, ignored) {
+ if (result) {
+ handler();
+ } else if (_errorHandler != null) {
+ _errorHandler("Directory deletion failed: $_path");
+ }
+ });
+ }
+
void deleteSync() {
if (!_delete(_path)) {
throw new DirectoryException("Directory deletion failed: $_path");
@@ -190,10 +329,22 @@ class _Directory implements Directory {
_doneHandler = doneHandler;
}
+ void set existsHandler(void existsHandler(bool exists)) {
+ _existsHandler = existsHandler;
+ }
+
+ void set createHandler(void createHandler()) {
+ _createHandler = createHandler;
+ }
+
void set createTempHandler(void createTempHandler()) {
_createTempHandler = createTempHandler;
}
+ void set deleteHandler(void deleteHandler()) {
+ _deleteHandler = deleteHandler;
+ }
+
void set errorHandler(void errorHandler(String error)) {
_errorHandler = errorHandler;
}
@@ -206,15 +357,15 @@ class _Directory implements Directory {
String get path() { return _path; }
- int _exists(String path) native "Directory_Exists";
- bool _create(String path) native "Directory_Create";
- bool _delete(String path) native "Directory_Delete";
-
var _dirHandler;
var _fileHandler;
var _doneHandler;
+ var _existsHandler;
+ var _createHandler;
var _createTempHandler;
+ var _deleteHandler;
var _errorHandler;
String _path;
+ _DirectoryOperationScheduler _scheduler;
}
« no previous file with comments | « runtime/bin/directory.dart ('k') | tests/standalone/src/DirectoryTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698