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

Side by Side Diff: sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart

Issue 12159005: Dartifying some IndexedDB APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 library dart.dom.indexed_db; 1 library dart.dom.indexed_db;
2 2
3 import 'dart:async'; 3 import 'dart:async';
4 import 'dart:html'; 4 import 'dart:html';
5 import 'dart:html_common'; 5 import 'dart:html_common';
6 import 'dart:_js_helper' show Creates, Returns, JSName, Null; 6 import 'dart:_js_helper' show Creates, Returns, JSName, Null;
7 import 'dart:_foreign_helper' show JS; 7 import 'dart:_foreign_helper' show JS;
8 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 8 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
9 // for details. All rights reserved. Use of this source code is governed by a 9 // for details. All rights reserved. Use of this source code is governed by a
10 // BSD-style license that can be found in the LICENSE file. 10 // BSD-style license that can be found in the LICENSE file.
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 @DocsEditable 157 @DocsEditable
158 final dynamic source; 158 final dynamic source;
159 159
160 @DomName('IDBCursor.advance') 160 @DomName('IDBCursor.advance')
161 @DocsEditable 161 @DocsEditable
162 void advance(int count) native; 162 void advance(int count) native;
163 163
164 @JSName('continue') 164 @JSName('continue')
165 @DomName('IDBCursor.continue') 165 @DomName('IDBCursor.continue')
166 @DocsEditable 166 @DocsEditable
167 void continueFunction([Object key]) native; 167 void next([Object key]) native;
168 168
169 @DomName('IDBCursor.delete') 169 @DomName('IDBCursor.delete')
170 @DocsEditable 170 @DocsEditable
171 Request delete() native; 171 Request delete() native;
172 172
173 Request update(/*any*/ value) { 173 Request update(/*any*/ value) {
174 var value_1 = convertDartToNative_SerializedScriptValue(value); 174 var value_1 = convertDartToNative_SerializedScriptValue(value);
175 return _update_1(value_1); 175 return _update_1(value_1);
176 } 176 }
177 @JSName('update') 177 @JSName('update')
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 /** 336 /**
337 * Checks to see if Indexed DB is supported on the current platform. 337 * Checks to see if Indexed DB is supported on the current platform.
338 */ 338 */
339 static bool get supported { 339 static bool get supported {
340 return JS('bool', 340 return JS('bool',
341 '!!(window.indexedDB || ' 341 '!!(window.indexedDB || '
342 'window.webkitIndexedDB || ' 342 'window.webkitIndexedDB || '
343 'window.mozIndexedDB)'); 343 'window.mozIndexedDB)');
344 } 344 }
345 345
346 @DomName('IDBFactory.open')
347 Future<Database> open(String name,
348 {int version, void onUpgradeNeeded(VersionChangeEvent),
349 void onBlocked(Event)}) {
350 if ((version == null) != (onUpgradeNeeded == null)) {
351 return new Future.immediateError(new ArgumentError(
352 'version specified but not onUpgradeNeeded'));
nweiz 2013/02/07 20:38:10 This is inaccurate if onUpgradeNeeded is specified
blois 2013/02/08 22:18:56 Done.
353 }
354 try {
355 var request;
356 if (version != null) {
357 request = $dom_open(name, version);
358 } else {
359 request = $dom_open(name);
360 }
361
362 if (onUpgradeNeeded != null) {
363 request.onUpgradeNeeded.listen(onUpgradeNeeded);
364 }
365 if (onBlocked != null) {
366 request.onBlocked.listen(onBlocked);
367 }
368 return _completeRequest(request);
369 } catch (e, stacktrace) {
370 return new Future.immediateError(e, stacktrace);
371 }
372 }
373
374 @DomName('IDBFactory.open')
375 Future<IdbFactory> deleteDatabase(String name,
376 {void onBlocked(Event)}) {
377 try {
378 var request = $dom_deleteDatabase(name);
379
380 if (onBlocked != null) {
381 request.onBlocked.listen(onBlocked);
382 }
383 return _completeRequest(request);
384 } catch (e, stacktrace) {
385 return new Future.immediateError(e, stacktrace);
386 }
387 }
388
346 389
347 @DomName('IDBFactory.cmp') 390 @DomName('IDBFactory.cmp')
348 @DocsEditable 391 @DocsEditable
349 int cmp(Object first, Object second) native; 392 int cmp(Object first, Object second) native;
350 393
394 @JSName('deleteDatabase')
351 @DomName('IDBFactory.deleteDatabase') 395 @DomName('IDBFactory.deleteDatabase')
352 @DocsEditable 396 @DocsEditable
353 OpenDBRequest deleteDatabase(String name) native; 397 OpenDBRequest $dom_deleteDatabase(String name) native;
354 398
399 @JSName('open')
355 @DomName('IDBFactory.open') 400 @DomName('IDBFactory.open')
356 @DocsEditable 401 @DocsEditable
357 @Returns('Request') 402 @Returns('Request')
358 @Creates('Request') 403 @Creates('Request')
359 @Creates('Database') 404 @Creates('Database')
360 OpenDBRequest open(String name, [int version]) native; 405 OpenDBRequest $dom_open(String name, [int version]) native;
361 406
362 @DomName('IDBFactory.webkitGetDatabaseNames') 407 @DomName('IDBFactory.webkitGetDatabaseNames')
363 @DocsEditable 408 @DocsEditable
364 Request webkitGetDatabaseNames() native; 409 Request webkitGetDatabaseNames() native;
365 410
366 } 411 }
412
413
414 /**
415 * Ties a request to a completer, so the completer is completed when it succeeds
416 * and errors out when the request errors.
417 */
418 Future _completeRequest(Request request) {
419 var completer = new Completer();
420 // TODO: make sure that completer.complete is synchronous as transactions
421 // may be committed if the result is not processed immediately.
422 request.onSuccess.listen((e) {
423 completer.complete(request.result);
424 });
425 request.onError.listen((e) {
426 completer.completeError(e);
427 });
428 return completer.future;
429 }
367 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 430 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
368 // for details. All rights reserved. Use of this source code is governed by a 431 // for details. All rights reserved. Use of this source code is governed by a
369 // BSD-style license that can be found in the LICENSE file. 432 // BSD-style license that can be found in the LICENSE file.
370 433
371 434
372 @DocsEditable 435 @DocsEditable
373 @DomName('IDBIndex') 436 @DomName('IDBIndex')
374 class Index native "*IDBIndex" { 437 class Index native "*IDBIndex" {
375 438
376 @DomName('IDBIndex.keyPath') 439 @DomName('IDBIndex.keyPath')
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 @DomName('IDBKeyRange.upperBound') 550 @DomName('IDBKeyRange.upperBound')
488 @DocsEditable 551 @DocsEditable
489 static KeyRange upperBound_(Object bound, [bool open]) native; 552 static KeyRange upperBound_(Object bound, [bool open]) native;
490 553
491 } 554 }
492 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 555 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
493 // for details. All rights reserved. Use of this source code is governed by a 556 // for details. All rights reserved. Use of this source code is governed by a
494 // BSD-style license that can be found in the LICENSE file. 557 // BSD-style license that can be found in the LICENSE file.
495 558
496 559
497 @DocsEditable
498 @DomName('IDBObjectStore') 560 @DomName('IDBObjectStore')
499 class ObjectStore native "*IDBObjectStore" { 561 class ObjectStore native "*IDBObjectStore" {
500 562
563 @DomName('IDBObjectStore.put')
564 Future put(value, [key]) {
565 try {
566 var request;
567 if (key != null) {
568 request = $dom_put(value, key);
569 } else {
570 request = $dom_put(value);
571 }
572 return _completeRequest(request);
573 } catch (e, stacktrace) {
574 return new Future.immediateError(e, stacktrace);
575 }
576 }
577
578 @DomName('IDBObjectStore.getObject')
579 Future getObject(key) {
580 try {
581 var request = $dom_getObject(key);
582
583 return _completeRequest(request);
584 } catch (e, stacktrace) {
585 return new Future.immediateError(e, stacktrace);
586 }
587 }
588
589 /**
590 * Creates a stream of cursors over the records in this object store.
591 *
592 * **The stream must be manually advanced by calling [Cursor.next] after
593 * each item or by specifying autoAdvance to be true.**
594 *
595 * var cursors = objectStore.openCursor().listen(
596 * (cursor) {
597 * // ...some processing with the cursor
598 * cursor.next(); // advance onto the next cursor.
599 * },
600 * onDone: () {
601 * // called when there are no more cursors.
602 * print('all done!');
603 * });
604 *
605 * Asynchronous operations which are not related to the current transaction
606 * will cause the transaction to automatically be committed-- all processing
607 * must be done synchronously unless they are additional async requests to
608 * the current transaction.
609 */
610 @DomName('IDBObjectStore.openCursor')
611 Stream<Cursor> openCursor({key, KeyRange range, String direction,
612 bool autoAdvance}) {
613 var key_OR_range = null;
614 if (key != null) {
615 if (range != null) {
616 throw new ArgumentError('Cannot specify both key and range.');
617 }
618 key_OR_range = key;
619 } else {
620 key_OR_range = range;
621 }
622
623 // TODO: try/catch this and return a stream with an immediate error.
624 var request;
625 if (direction == null) {
626 request = $dom_openCursor(key_OR_range);
627 } else {
628 request = $dom_openCursor(key_OR_range, direction);
629 }
630 return _cursorStreamFromResult(request, autoAdvance);
631 }
632
633
501 @DomName('IDBObjectStore.autoIncrement') 634 @DomName('IDBObjectStore.autoIncrement')
502 @DocsEditable 635 @DocsEditable
503 final bool autoIncrement; 636 final bool autoIncrement;
504 637
505 @DomName('IDBObjectStore.indexNames') 638 @DomName('IDBObjectStore.indexNames')
506 @DocsEditable 639 @DocsEditable
507 @Returns('DomStringList') 640 @Returns('DomStringList')
508 @Creates('DomStringList') 641 @Creates('DomStringList')
509 final List<String> indexNames; 642 final List<String> indexNames;
510 643
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 @DomName('IDBObjectStore.deleteIndex') 726 @DomName('IDBObjectStore.deleteIndex')
594 @DocsEditable 727 @DocsEditable
595 void deleteIndex(String name) native; 728 void deleteIndex(String name) native;
596 729
597 @JSName('get') 730 @JSName('get')
598 @DomName('IDBObjectStore.get') 731 @DomName('IDBObjectStore.get')
599 @DocsEditable 732 @DocsEditable
600 @Returns('Request') 733 @Returns('Request')
601 @Creates('Request') 734 @Creates('Request')
602 @annotation_Creates_SerializedScriptValue 735 @annotation_Creates_SerializedScriptValue
603 Request getObject(key) native; 736 Request $dom_getObject(key) native;
604 737
605 @DomName('IDBObjectStore.index') 738 @DomName('IDBObjectStore.index')
606 @DocsEditable 739 @DocsEditable
607 Index index(String name) native; 740 Index index(String name) native;
608 741
742 @JSName('openCursor')
609 @DomName('IDBObjectStore.openCursor') 743 @DomName('IDBObjectStore.openCursor')
610 @DocsEditable 744 @DocsEditable
611 @Returns('Request') 745 @Returns('Request')
612 @Creates('Request') 746 @Creates('Request')
613 @Creates('Cursor') 747 @Creates('Cursor')
614 Request openCursor([key_OR_range, String direction]) native; 748 Request $dom_openCursor([key_OR_range, String direction]) native;
615 749
616 Request put(/*any*/ value, [/*any*/ key]) { 750 Request $dom_put(/*any*/ value, [/*any*/ key]) {
617 if (?key) { 751 if (?key) {
618 var value_1 = convertDartToNative_SerializedScriptValue(value); 752 var value_1 = convertDartToNative_SerializedScriptValue(value);
619 var key_2 = convertDartToNative_SerializedScriptValue(key); 753 var key_2 = convertDartToNative_SerializedScriptValue(key);
620 return _put_1(value_1, key_2); 754 return _$dom_put_1(value_1, key_2);
621 } 755 }
622 var value_3 = convertDartToNative_SerializedScriptValue(value); 756 var value_3 = convertDartToNative_SerializedScriptValue(value);
623 return _put_2(value_3); 757 return _$dom_put_2(value_3);
624 } 758 }
625 @JSName('put') 759 @JSName('put')
626 @DomName('IDBObjectStore.put') 760 @DomName('IDBObjectStore.put')
627 @DocsEditable 761 @DocsEditable
628 @Returns('Request') 762 @Returns('Request')
629 @Creates('Request') 763 @Creates('Request')
630 @_annotation_Creates_IDBKey 764 @_annotation_Creates_IDBKey
631 Request _put_1(value, key) native; 765 Request _$dom_put_1(value, key) native;
632 @JSName('put') 766 @JSName('put')
633 @DomName('IDBObjectStore.put') 767 @DomName('IDBObjectStore.put')
634 @DocsEditable 768 @DocsEditable
635 @Returns('Request') 769 @Returns('Request')
636 @Creates('Request') 770 @Creates('Request')
637 @_annotation_Creates_IDBKey 771 @_annotation_Creates_IDBKey
638 Request _put_2(value) native; 772 Request _$dom_put_2(value) native;
773
774
775 /**
776 * Helper for iterating over cursors in a request.
777 */
778 static Stream<Cursor> _cursorStreamFromResult(Request request,
779 bool autoAdvance) {
780 // TODO: need to guarantee that the controller provides the values
781 // immediately as waiting until the next tick will cause the transaction to
782 // close.
783 var controller = new StreamController<Cursor>();
784
785 request.onError.listen((e) {
786 //TODO: Report stacktrace once issue 4061 is resolved.
787 controller.signalError(e);
788 });
789
790 request.onSuccess.listen((e) {
791 Cursor cursor = request.result;
792 if (cursor == null) {
793 controller.close();
794 } else {
795 controller.add(cursor);
796 if (autoAdvance == true) {
797 cursor.next();
798 }
799 }
800 });
801 return controller.stream;
802 }
639 } 803 }
640 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 804 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
641 // for details. All rights reserved. Use of this source code is governed by a 805 // for details. All rights reserved. Use of this source code is governed by a
642 // BSD-style license that can be found in the LICENSE file. 806 // BSD-style license that can be found in the LICENSE file.
643 807
644 808
645 @DocsEditable 809 @DocsEditable
646 @DomName('IDBOpenDBRequest') 810 @DomName('IDBOpenDBRequest')
647 class OpenDBRequest extends Request implements EventTarget native "*IDBOpenDBReq uest" { 811 class OpenDBRequest extends Request implements EventTarget native "*IDBOpenDBReq uest" {
648 812
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 class RequestEvents extends Events { 924 class RequestEvents extends Events {
761 @DocsEditable 925 @DocsEditable
762 RequestEvents(EventTarget _ptr) : super(_ptr); 926 RequestEvents(EventTarget _ptr) : super(_ptr);
763 927
764 @DocsEditable 928 @DocsEditable
765 EventListenerList get error => this['error']; 929 EventListenerList get error => this['error'];
766 930
767 @DocsEditable 931 @DocsEditable
768 EventListenerList get success => this['success']; 932 EventListenerList get success => this['success'];
769 } 933 }
770 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 934 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
771 // for details. All rights reserved. Use of this source code is governed by a 935 // for details. All rights reserved. Use of this source code is governed by a
772 // BSD-style license that can be found in the LICENSE file. 936 // BSD-style license that can be found in the LICENSE file.
773 937
774 938
775 @DocsEditable
776 @DomName('IDBTransaction') 939 @DomName('IDBTransaction')
777 class Transaction extends EventTarget native "*IDBTransaction" { 940 class Transaction extends EventTarget native "*IDBTransaction" {
778 941
942 /**
943 * Provides a Future which will be completed once the transaction has
944 * completed.
945 *
946 * The future will error if an error occurrs on the transaction or if the
947 * transaction is aborted.
948 */
949 Future<Database> get completed {
950 var completer = new Completer<Database>();
951
952 this.onComplete.first.then((_) {
953 completer.complete(db);
954 });
955
956 this.onError.first.then((e) {
957 completer.completeError(e);
958 });
959
960 this.onAbort.first.then((e) {
961 completer.completeError(e);
962 });
963
964 return completer.future;
965 }
966
967
779 @DomName('IDBTransaction.abortEvent') 968 @DomName('IDBTransaction.abortEvent')
780 @DocsEditable 969 @DocsEditable
781 static const EventStreamProvider<Event> abortEvent = const EventStreamProvider <Event>('abort'); 970 static const EventStreamProvider<Event> abortEvent = const EventStreamProvider <Event>('abort');
782 971
783 @DomName('IDBTransaction.completeEvent') 972 @DomName('IDBTransaction.completeEvent')
784 @DocsEditable 973 @DocsEditable
785 static const EventStreamProvider<Event> completeEvent = const EventStreamProvi der<Event>('complete'); 974 static const EventStreamProvider<Event> completeEvent = const EventStreamProvi der<Event>('complete');
786 975
787 @DomName('IDBTransaction.errorEvent') 976 @DomName('IDBTransaction.errorEvent')
788 @DocsEditable 977 @DocsEditable
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 @DocsEditable 1025 @DocsEditable
837 Stream<Event> get onAbort => abortEvent.forTarget(this); 1026 Stream<Event> get onAbort => abortEvent.forTarget(this);
838 1027
839 @DomName('IDBTransaction.oncomplete') 1028 @DomName('IDBTransaction.oncomplete')
840 @DocsEditable 1029 @DocsEditable
841 Stream<Event> get onComplete => completeEvent.forTarget(this); 1030 Stream<Event> get onComplete => completeEvent.forTarget(this);
842 1031
843 @DomName('IDBTransaction.onerror') 1032 @DomName('IDBTransaction.onerror')
844 @DocsEditable 1033 @DocsEditable
845 Stream<Event> get onError => errorEvent.forTarget(this); 1034 Stream<Event> get onError => errorEvent.forTarget(this);
1035
846 } 1036 }
847 1037
848 @DocsEditable 1038 @DocsEditable
849 @deprecated 1039 @deprecated
850 class TransactionEvents extends Events { 1040 class TransactionEvents extends Events {
851 @DocsEditable 1041 @DocsEditable
852 TransactionEvents(EventTarget _ptr) : super(_ptr); 1042 TransactionEvents(EventTarget _ptr) : super(_ptr);
853 1043
854 @DocsEditable 1044 @DocsEditable
855 EventListenerList get abort => this['abort']; 1045 EventListenerList get abort => this['abort'];
(...skipping 23 matching lines...) Expand all
879 } 1069 }
880 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1070 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
881 // for details. All rights reserved. Use of this source code is governed by a 1071 // for details. All rights reserved. Use of this source code is governed by a
882 // BSD-style license that can be found in the LICENSE file. 1072 // BSD-style license that can be found in the LICENSE file.
883 1073
884 1074
885 @DocsEditable 1075 @DocsEditable
886 @DomName('IDBAny') 1076 @DomName('IDBAny')
887 class _IDBAny native "*IDBAny" { 1077 class _IDBAny native "*IDBAny" {
888 } 1078 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/indexed_db/dartium/indexed_db_dartium.dart » ('j') | tests/html/indexeddb_1_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698