Chromium Code Reviews| Index: sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart |
| diff --git a/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart b/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart |
| index 8a37de0d22beaf046a35958f46c5c7ac40c4be17..302547720195ce294553815c3971e7bb73e2d2ca 100644 |
| --- a/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart |
| +++ b/sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart |
| @@ -164,7 +164,7 @@ class Cursor native "*IDBCursor" { |
| @JSName('continue') |
| @DomName('IDBCursor.continue') |
| @DocsEditable |
| - void continueFunction([Object key]) native; |
| + void next([Object key]) native; |
| @DomName('IDBCursor.delete') |
| @DocsEditable |
| @@ -343,27 +343,79 @@ class IdbFactory native "*IDBFactory" { |
| 'window.mozIndexedDB)'); |
| } |
| + @DomName('IDBFactory.open') |
| + Future<Database> open(String name, |
| + {int version, void onUpgradeNeeded(VersionChangeEvent)}) { |
|
nweiz
2013/02/05 02:03:19
It's probably worthwhile to add some code that thr
blois
2013/02/05 03:02:42
True- my initial reading was that it was only for
|
| + try { |
| + var request; |
| + if (version != null) { |
| + request = $dom_open(name, version); |
| + } else { |
| + request = $dom_open(name); |
| + } |
| + |
| + if (onUpgradeNeeded != null) { |
| + request.onUpgradeNeeded.listen(onUpgradeNeeded); |
| + } |
| + return _completeRequest(request, new Completer<Database>()); |
| + } catch (e) { |
| + return new Future.immediateError(e); |
|
nweiz
2013/02/05 02:03:19
Catch and report the stack trace.
blois
2013/02/05 03:02:42
Done.
|
| + } |
| + } |
| + |
| + @DomName('IDBFactory.open') |
| + Future<IdbFactory> deleteDatabase(String name, |
| + {void onBlocked(Event)}) { |
| + try { |
| + var request = $dom_deleteDatabase(name); |
| + |
| + if (onBlocked != null) { |
| + request.onBlocked.listen(onBlocked); |
| + } |
| + _completeRequest(request, new Completer<Database>()); |
| + } catch (e) { |
| + return new Future.immediateError(e); |
| + } |
| + } |
| + |
| @DomName('IDBFactory.cmp') |
| @DocsEditable |
| int cmp(Object first, Object second) native; |
| + @JSName('deleteDatabase') |
| @DomName('IDBFactory.deleteDatabase') |
| @DocsEditable |
| - VersionChangeRequest deleteDatabase(String name) native; |
| + VersionChangeRequest $dom_deleteDatabase(String name) native; |
| + @JSName('open') |
| @DomName('IDBFactory.open') |
| @DocsEditable |
| @Returns('Request') |
| @Creates('Request') |
| @Creates('Database') |
| - OpenDBRequest open(String name, [int version]) native; |
| + OpenDBRequest $dom_open(String name, [int version]) native; |
| @DomName('IDBFactory.webkitGetDatabaseNames') |
| @DocsEditable |
| Request webkitGetDatabaseNames() native; |
| } |
| + |
| + |
| +/** |
| + * Ties a request to a completer, so the completer is completed when it succeeds |
| + * and errors out when the request errors. |
| + */ |
| +Future _completeRequest(Request request, Completer completer) { |
|
nweiz
2013/02/05 02:03:19
Why does this take an external completer? It seems
blois
2013/02/05 03:02:42
I made it take one so it would be typed properly.
nweiz
2013/02/05 03:05:39
Trying to get Futures typed correctly is a losing
|
| + request.onSuccess.listen((e) { |
| + completer.complete(request.result); |
| + }); |
| + request.onError.listen((e) { |
| + completer.completeError(e); |
| + }); |
| + return completer.future; |
| +} |
| // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| @@ -494,10 +546,70 @@ class KeyRange native "*IDBKeyRange" { |
| // BSD-style license that can be found in the LICENSE file. |
| -@DocsEditable |
| @DomName('IDBObjectStore') |
| class ObjectStore native "*IDBObjectStore" { |
| + @DomName('IDBObjectStore.put') |
| + Future<Object> put(value, [key]) { |
|
nweiz
2013/02/05 02:03:19
Why "Future<Object>" rather than just "Future"?
blois
2013/02/05 03:02:42
Removed, but it was a preference to be explicit as
|
| + try { |
| + var request; |
| + if (key != null) { |
| + request = $dom_put(value, key); |
| + } else { |
| + request = $dom_put(value); |
| + } |
| + return _completeRequest(request, new Completer<Object>()); |
| + } catch (e) { |
| + return new Future.immediateError(e); |
| + } |
| + } |
| + |
| + @DomName('IDBObjectStore.getObject') |
| + Future<Object> getObject(key) { |
| + try { |
| + var request = $dom_getObject(key); |
| + |
| + return _completeRequest(request, new Completer<Object>()); |
| + } catch (e) { |
| + return new Future.immediateError(e); |
| + } |
| + } |
| + |
| + /** |
| + * Creates a stream of cursors over the records in this object store. |
| + * |
| + * The stream must be manually advanced by calling [cursor.next] after |
| + * each item or by specifying autoAdvance to be true. |
| + * |
| + * Asynchronous operations which are not related to the current transaction |
| + * will cause the transaction to automatically be committed- all processing |
|
nweiz
2013/02/05 02:03:19
Nit: "committed- all" -> "comitted--all"
|
| + * must be done synchronously unless they are additional async requests to |
| + * the current transaction. |
|
nweiz
2013/02/05 02:03:19
I would make this warning more urgent and scary, m
blois
2013/02/05 03:02:42
Done.
|
| + */ |
| + @DomName('IDBObjectStore.openCursor') |
| + Stream<Cursor> openCursor({key, KeyRange range, String direction, |
| + bool autoAdvance}) { |
| + var key_OR_range = null; |
| + if (key != null) { |
| + if (range != null) { |
| + throw new ArgumentError('Cannot specify both key and range.'); |
| + } |
| + key_OR_range = key; |
| + } else { |
| + key_OR_range = range; |
| + } |
| + |
| + // TODO: try/catch this and return a stream with an immediate error. |
| + var request; |
| + if (direction == null) { |
| + request = $dom_openCursor(key_OR_range); |
| + } else { |
| + request = $dom_openCursor(key_OR_range, direction); |
| + } |
| + return _cursorStreamFromResult(request, autoAdvance); |
| + } |
| + |
| + |
| @DomName('IDBObjectStore.autoIncrement') |
| @DocsEditable |
| final bool autoIncrement; |
| @@ -600,27 +712,28 @@ class ObjectStore native "*IDBObjectStore" { |
| @Returns('Request') |
| @Creates('Request') |
| @annotation_Creates_SerializedScriptValue |
| - Request getObject(key) native; |
| + Request $dom_getObject(key) native; |
| @DomName('IDBObjectStore.index') |
| @DocsEditable |
| Index index(String name) native; |
| + @JSName('openCursor') |
| @DomName('IDBObjectStore.openCursor') |
| @DocsEditable |
| @Returns('Request') |
| @Creates('Request') |
| @Creates('Cursor') |
| - Request openCursor([key_OR_range, String direction]) native; |
| + Request $dom_openCursor([key_OR_range, String direction]) native; |
| - Request put(/*any*/ value, [/*any*/ key]) { |
| + Request $dom_put(/*any*/ value, [/*any*/ key]) { |
| if (?key) { |
| var value_1 = convertDartToNative_SerializedScriptValue(value); |
| var key_2 = convertDartToNative_SerializedScriptValue(key); |
| - return _put_1(value_1, key_2); |
| + return _$dom_put_1(value_1, key_2); |
| } |
| var value_3 = convertDartToNative_SerializedScriptValue(value); |
| - return _put_2(value_3); |
| + return _$dom_put_2(value_3); |
| } |
| @JSName('put') |
| @DomName('IDBObjectStore.put') |
| @@ -628,14 +741,40 @@ class ObjectStore native "*IDBObjectStore" { |
| @Returns('Request') |
| @Creates('Request') |
| @_annotation_Creates_IDBKey |
| - Request _put_1(value, key) native; |
| + Request _$dom_put_1(value, key) native; |
| @JSName('put') |
| @DomName('IDBObjectStore.put') |
| @DocsEditable |
| @Returns('Request') |
| @Creates('Request') |
| @_annotation_Creates_IDBKey |
| - Request _put_2(value) native; |
| + Request _$dom_put_2(value) native; |
| + |
| + |
| + /** |
| + * Helper for iterating over cursors in a request. |
| + */ |
| + static Stream<Cursor> _cursorStreamFromResult(Request request, |
| + bool autoAdvance) { |
| + var controller = new StreamController<Cursor>(); |
|
nweiz
2013/02/05 02:03:19
Add a TODO to use an explicitly synchronous contro
blois
2013/02/05 03:02:42
Done.
|
| + |
| + request.onError.listen((e) { |
| + controller.signalError(e); |
|
nweiz
2013/02/05 02:03:19
Add a TODO here to include a stack trace once issu
blois
2013/02/05 03:02:42
Done.
|
| + }); |
| + |
| + request.onSuccess.listen((e) { |
| + Cursor cursor = request.result; |
| + if (cursor == null) { |
| + controller.close(); |
| + } else { |
| + controller.add(cursor); |
| + if (autoAdvance == true) { |
| + cursor.next(); |
| + } |
| + } |
| + }); |
| + return controller.stream; |
| + } |
| } |
| // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |