| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, 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 part of $LIBRARYNAME; |
| 6 |
| 7 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 8 |
| 9 @DomName('IDBObjectStore.put') |
| 10 Future<Object> put(value, [key]) { |
| 11 try { |
| 12 var request; |
| 13 if (key != null) { |
| 14 request = $dom_put(value, key); |
| 15 } else { |
| 16 request = $dom_put(value); |
| 17 } |
| 18 return _completeRequest(request, new Completer<Object>()); |
| 19 } catch (e) { |
| 20 return new Future.immediateError(e); |
| 21 } |
| 22 } |
| 23 |
| 24 @DomName('IDBObjectStore.getObject') |
| 25 Future<Object> getObject(key) { |
| 26 try { |
| 27 var request = $dom_getObject(key); |
| 28 |
| 29 return _completeRequest(request, new Completer<Object>()); |
| 30 } catch (e) { |
| 31 return new Future.immediateError(e); |
| 32 } |
| 33 } |
| 34 |
| 35 /** |
| 36 * Creates a stream of cursors over the records in this object store. |
| 37 * |
| 38 * The stream must be manually advanced by calling [Cursor.next] after |
| 39 * each item or by specifying autoAdvance to be true. |
| 40 * |
| 41 * Asynchronous operations which are not related to the current transaction |
| 42 * will cause the transaction to automatically be committed- all processing |
| 43 * must be done synchronously unless they are additional async requests to |
| 44 * the current transaction. |
| 45 */ |
| 46 @DomName('IDBObjectStore.openCursor') |
| 47 Stream<Cursor> openCursor({key, KeyRange range, String direction, |
| 48 bool autoAdvance}) { |
| 49 var key_OR_range = null; |
| 50 if (key != null) { |
| 51 if (range != null) { |
| 52 throw new ArgumentError('Cannot specify both key and range.'); |
| 53 } |
| 54 key_OR_range = key; |
| 55 } else { |
| 56 key_OR_range = range; |
| 57 } |
| 58 |
| 59 // TODO: try/catch this and return a stream with an immediate error. |
| 60 var request; |
| 61 if (direction == null) { |
| 62 request = $dom_openCursor(key_OR_range); |
| 63 } else { |
| 64 request = $dom_openCursor(key_OR_range, direction); |
| 65 } |
| 66 return _cursorStreamFromResult(request, autoAdvance); |
| 67 } |
| 68 |
| 69 $!MEMBERS |
| 70 |
| 71 /** |
| 72 * Helper for iterating over cursors in a request. |
| 73 */ |
| 74 static Stream<Cursor> _cursorStreamFromResult(Request request, |
| 75 bool autoAdvance) { |
| 76 var controller = new StreamController<Cursor>(); |
| 77 |
| 78 request.onError.listen((e) { |
| 79 controller.signalError(e); |
| 80 }); |
| 81 |
| 82 request.onSuccess.listen((e) { |
| 83 Cursor cursor = request.result; |
| 84 if (cursor == null) { |
| 85 controller.close(); |
| 86 } else { |
| 87 controller.add(cursor); |
| 88 if (autoAdvance == true) { |
| 89 cursor.next(); |
| 90 } |
| 91 } |
| 92 }); |
| 93 return controller.stream; |
| 94 } |
| 95 } |
| OLD | NEW |