| 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 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); |
| 19 } catch (e, stacktrace) { |
| 20 return new Future.immediateError(e, stacktrace); |
| 21 } |
| 22 } |
| 23 |
| 24 @DomName('IDBObjectStore.getObject') |
| 25 Future getObject(key) { |
| 26 try { |
| 27 var request = $dom_getObject(key); |
| 28 |
| 29 return _completeRequest(request); |
| 30 } catch (e, stacktrace) { |
| 31 return new Future.immediateError(e, stacktrace); |
| 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 * var cursors = objectStore.openCursor().listen( |
| 42 * (cursor) { |
| 43 * // ...some processing with the cursor |
| 44 * cursor.next(); // advance onto the next cursor. |
| 45 * }, |
| 46 * onDone: () { |
| 47 * // called when there are no more cursors. |
| 48 * print('all done!'); |
| 49 * }); |
| 50 * |
| 51 * Asynchronous operations which are not related to the current transaction |
| 52 * will cause the transaction to automatically be committed-- all processing |
| 53 * must be done synchronously unless they are additional async requests to |
| 54 * the current transaction. |
| 55 */ |
| 56 @DomName('IDBObjectStore.openCursor') |
| 57 Stream<Cursor> openCursor({key, KeyRange range, String direction, |
| 58 bool autoAdvance}) { |
| 59 var key_OR_range = null; |
| 60 if (key != null) { |
| 61 if (range != null) { |
| 62 throw new ArgumentError('Cannot specify both key and range.'); |
| 63 } |
| 64 key_OR_range = key; |
| 65 } else { |
| 66 key_OR_range = range; |
| 67 } |
| 68 |
| 69 // TODO: try/catch this and return a stream with an immediate error. |
| 70 var request; |
| 71 if (direction == null) { |
| 72 request = $dom_openCursor(key_OR_range); |
| 73 } else { |
| 74 request = $dom_openCursor(key_OR_range, direction); |
| 75 } |
| 76 return _cursorStreamFromResult(request, autoAdvance); |
| 77 } |
| 78 |
| 79 $!MEMBERS |
| 80 |
| 81 /** |
| 82 * Helper for iterating over cursors in a request. |
| 83 */ |
| 84 static Stream<Cursor> _cursorStreamFromResult(Request request, |
| 85 bool autoAdvance) { |
| 86 // TODO: need to guarantee that the controller provides the values |
| 87 // immediately as waiting until the next tick will cause the transaction to |
| 88 // close. |
| 89 var controller = new StreamController<Cursor>(); |
| 90 |
| 91 request.onError.listen((e) { |
| 92 //TODO: Report stacktrace once issue 4061 is resolved. |
| 93 controller.signalError(e); |
| 94 }); |
| 95 |
| 96 request.onSuccess.listen((e) { |
| 97 Cursor cursor = request.result; |
| 98 if (cursor == null) { |
| 99 controller.close(); |
| 100 } else { |
| 101 controller.add(cursor); |
| 102 if (autoAdvance == true) { |
| 103 cursor.next(); |
| 104 } |
| 105 } |
| 106 }); |
| 107 return controller.stream; |
| 108 } |
| 109 } |
| OLD | NEW |