| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of $LIBRARYNAME; | 5 part of $LIBRARYNAME; |
| 6 | 6 |
| 7 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { | 7 $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC { |
| 8 /** | 8 /** |
| 9 * Checks to see if Indexed DB is supported on the current platform. | 9 * Checks to see if Indexed DB is supported on the current platform. |
| 10 */ | 10 */ |
| 11 static bool get supported { | 11 static bool get supported { |
| 12 $if DARTIUM | 12 $if DARTIUM |
| 13 return true; | 13 return true; |
| 14 $else | 14 $else |
| 15 return JS('bool', | 15 return JS('bool', |
| 16 '!!(window.indexedDB || ' | 16 '!!(window.indexedDB || ' |
| 17 'window.webkitIndexedDB || ' | 17 'window.webkitIndexedDB || ' |
| 18 'window.mozIndexedDB)'); | 18 'window.mozIndexedDB)'); |
| 19 $endif | 19 $endif |
| 20 } | 20 } |
| 21 | 21 |
| 22 @DomName('IDBFactory.open') |
| 23 Future<Database> open(String name, |
| 24 {int version, void onUpgradeNeeded(VersionChangeEvent), |
| 25 void onBlocked(Event)}) { |
| 26 if ((version == null) != (onUpgradeNeeded == null)) { |
| 27 return new Future.immediateError(new ArgumentError( |
| 28 'version specified but not onUpgradeNeeded')); |
| 29 } |
| 30 try { |
| 31 var request; |
| 32 if (version != null) { |
| 33 request = $dom_open(name, version); |
| 34 } else { |
| 35 request = $dom_open(name); |
| 36 } |
| 37 |
| 38 if (onUpgradeNeeded != null) { |
| 39 request.onUpgradeNeeded.listen(onUpgradeNeeded); |
| 40 } |
| 41 if (onBlocked != null) { |
| 42 request.onBlocked.listen(onBlocked); |
| 43 } |
| 44 return _completeRequest(request); |
| 45 } catch (e, stacktrace) { |
| 46 return new Future.immediateError(e, stacktrace); |
| 47 } |
| 48 } |
| 49 |
| 50 @DomName('IDBFactory.open') |
| 51 Future<IdbFactory> deleteDatabase(String name, |
| 52 {void onBlocked(Event)}) { |
| 53 try { |
| 54 var request = $dom_deleteDatabase(name); |
| 55 |
| 56 if (onBlocked != null) { |
| 57 request.onBlocked.listen(onBlocked); |
| 58 } |
| 59 return _completeRequest(request); |
| 60 } catch (e, stacktrace) { |
| 61 return new Future.immediateError(e, stacktrace); |
| 62 } |
| 63 } |
| 64 |
| 22 $!MEMBERS | 65 $!MEMBERS |
| 23 } | 66 } |
| 67 |
| 68 |
| 69 /** |
| 70 * Ties a request to a completer, so the completer is completed when it succeeds |
| 71 * and errors out when the request errors. |
| 72 */ |
| 73 Future _completeRequest(Request request) { |
| 74 var completer = new Completer(); |
| 75 // TODO: make sure that completer.complete is synchronous as transactions |
| 76 // may be committed if the result is not processed immediately. |
| 77 request.onSuccess.listen((e) { |
| 78 completer.complete(request.result); |
| 79 }); |
| 80 request.onError.listen((e) { |
| 81 completer.completeError(e); |
| 82 }); |
| 83 return completer.future; |
| 84 } |
| OLD | NEW |