/
indexed_db_dart2js.dart
1291 lines (1091 loc) · 38.4 KB
/
indexed_db_dart2js.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* A client-side key-value store with support for indexes.
*
* Many browsers support IndexedDB—a web standard for
* an indexed database.
* By storing data on the client in an IndexedDB,
* a web app gets some advantages, such as faster performance and persistence.
* To find out which browsers support IndexedDB,
* refer to [Can I Use?](http://caniuse.com/#feat=indexeddb)
*
* In IndexedDB, each record is identified by a unique index or key,
* making data retrieval speedy.
* You can store structured data,
* such as images, arrays, and maps using IndexedDB.
* The standard does not specify size limits for individual data items
* or for the database itself, but browsers may impose storage limits.
*
* ## Using indexed_db
*
* The classes in this library provide an interface
* to the browser's IndexedDB, if it has one.
* To use this library in your code:
*
* import 'dart:indexed_db';
*
* A web app can determine if the browser supports
* IndexedDB with [IdbFactory.supported]:
*
* if (IdbFactory.supported)
* // Use indexeddb.
* else
* // Find an alternative.
*
* Access to the browser's IndexedDB is provided by the app's top-level
* [Window] object, which your code can refer to with `window.indexedDB`.
* So, for example,
* here's how to use window.indexedDB to open a database:
*
* Future open() {
* return window.indexedDB.open('myIndexedDB',
* version: 1,
* onUpgradeNeeded: _initializeDatabase)
* .then(_loadFromDB);
* }
* void _initializeDatabase(VersionChangeEvent e) {
* ...
* }
* Future _loadFromDB(Database db) {
* ...
* }
*
*
* All data in an IndexedDB is stored within an [ObjectStore].
* To manipulate the database use [Transaction]s.
*
* ## Other resources
*
* Other options for client-side data storage include:
*
* * [Window.localStorage]—a
* basic mechanism that stores data as a [Map],
* and where both the keys and the values are strings.
*
* MDN provides [API
* documentation](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API).
*
* {@category Web}
*/
library dart.dom.indexed_db;
import 'dart:async';
import 'dart:html';
import 'dart:html_common';
import 'dart:_native_typed_data';
import 'dart:typed_data';
import 'dart:_js_helper' show Creates, Returns, JSName, Native;
import 'dart:_foreign_helper' show JS;
import 'dart:_interceptors' show Interceptor, JSExtendableArray;
import 'dart:_js_helper' show convertDartClosureToJS;
// 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.
// DO NOT EDIT - unless you are editing documentation as per:
// https://code.google.com/p/dart/wiki/ContributingHTMLDocumentation
// Auto-generated dart:svg library.
class _KeyRangeFactoryProvider {
static KeyRange createKeyRange_only(/*Key*/ value) =>
_only(_class(), _translateKey(value));
static KeyRange createKeyRange_lowerBound(/*Key*/ bound,
[bool open = false]) =>
_lowerBound(_class(), _translateKey(bound), open);
static KeyRange createKeyRange_upperBound(/*Key*/ bound,
[bool open = false]) =>
_upperBound(_class(), _translateKey(bound), open);
static KeyRange createKeyRange_bound(/*Key*/ lower, /*Key*/ upper,
[bool lowerOpen = false, bool upperOpen = false]) =>
_bound(_class(), _translateKey(lower), _translateKey(upper), lowerOpen,
upperOpen);
static var _cachedClass;
static _class() {
if (_cachedClass != null) return _cachedClass;
return _cachedClass = _uncachedClass();
}
static _uncachedClass() =>
JS('var', '''window.webkitIDBKeyRange || window.mozIDBKeyRange ||
window.msIDBKeyRange || window.IDBKeyRange''');
static _translateKey(idbkey) => idbkey; // TODO: fixme.
static KeyRange _only(cls, value) => JS('KeyRange', '#.only(#)', cls, value);
static KeyRange _lowerBound(cls, bound, open) =>
JS('KeyRange', '#.lowerBound(#, #)', cls, bound, open);
static KeyRange _upperBound(cls, bound, open) =>
JS('KeyRange', '#.upperBound(#, #)', cls, bound, open);
static KeyRange _bound(cls, lower, upper, lowerOpen, upperOpen) => JS(
'KeyRange',
'#.bound(#, #, #, #)',
cls,
lower,
upper,
lowerOpen,
upperOpen);
}
// Conversions for IDBKey.
//
// Per http://www.w3.org/TR/IndexedDB/#key-construct
//
// "A value is said to be a valid key if it is one of the following types: Array
// JavaScript objects [ECMA-262], DOMString [WEBIDL], Date [ECMA-262] or float
// [WEBIDL]. However Arrays are only valid keys if every item in the array is
// defined and is a valid key (i.e. sparse arrays can not be valid keys) and if
// the Array doesn't directly or indirectly contain itself. Any non-numeric
// properties are ignored, and thus does not affect whether the Array is a valid
// key. Additionally, if the value is of type float, it is only a valid key if
// it is not NaN, and if the value is of type Date it is only a valid key if its
// [[PrimitiveValue]] internal property, as defined by [ECMA-262], is not NaN."
// What is required is to ensure that an Lists in the key are actually
// JavaScript arrays, and any Dates are JavaScript Dates.
/**
* Converts a native IDBKey into a Dart object.
*
* May return the original input. May mutate the original input (but will be
* idempotent if mutation occurs). It is assumed that this conversion happens
* on native IDBKeys on all paths that return IDBKeys from native DOM calls.
*
* If necessary, JavaScript Dates are converted into Dart Dates.
*/
_convertNativeToDart_IDBKey(nativeKey) {
containsDate(object) {
if (isJavaScriptDate(object)) return true;
if (object is List) {
for (int i = 0; i < object.length; i++) {
if (containsDate(object[i])) return true;
}
}
return false; // number, string.
}
if (containsDate(nativeKey)) {
throw new UnimplementedError('Key containing DateTime');
}
// TODO: Cache conversion somewhere?
return nativeKey;
}
/**
* Converts a Dart object into a valid IDBKey.
*
* May return the original input. Does not mutate input.
*
* If necessary, [dartKey] may be copied to ensure all lists are converted into
* JavaScript Arrays and Dart Dates into JavaScript Dates.
*/
_convertDartToNative_IDBKey(dartKey) {
// TODO: Implement.
return dartKey;
}
/// May modify original. If so, action is idempotent.
_convertNativeToDart_IDBAny(object) {
return convertNativeToDart_AcceptStructuredClone(object, mustCopy: false);
}
// TODO(sra): Add DateTime.
const String _idbKey = 'JSExtendableArray|=Object|num|String';
const _annotation_Creates_IDBKey = const Creates(_idbKey);
const _annotation_Returns_IDBKey = const Returns(_idbKey);
// Copyright (c) 2013, 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.
@Unstable()
@Native("IDBCursor")
class Cursor extends Interceptor {
Future delete() {
try {
return _completeRequest(_delete());
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future update(value) {
try {
return _completeRequest(_update(value));
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
@JSName('continue')
void next([Object? key]) {
if (key == null) {
JS('void', '#.continue()', this);
} else {
JS('void', '#.continue(#)', this, key);
}
}
// To suppress missing implicit constructor warnings.
factory Cursor._() {
throw new UnsupportedError("Not supported");
}
String? get direction native;
@_annotation_Creates_IDBKey
@_annotation_Returns_IDBKey
Object? get key native;
@_annotation_Creates_IDBKey
@_annotation_Returns_IDBKey
Object? get primaryKey native;
@Creates('Null')
@Returns('ObjectStore|Index|Null')
Object? get source native;
void advance(int count) native;
void continuePrimaryKey(Object key, Object primaryKey) native;
@JSName('delete')
Request _delete() native;
Request _update(/*any*/ value) {
var value_1 = convertDartToNative_SerializedScriptValue(value);
return _update_1(value_1);
}
@JSName('update')
Request _update_1(value) native;
}
// 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.
@Unstable()
@Native("IDBCursorWithValue")
class CursorWithValue extends Cursor {
// To suppress missing implicit constructor warnings.
factory CursorWithValue._() {
throw new UnsupportedError("Not supported");
}
dynamic get value => _convertNativeToDart_IDBAny(this._get_value);
@JSName('value')
@annotation_Creates_SerializedScriptValue
@annotation_Returns_SerializedScriptValue
dynamic get _get_value native;
}
// Copyright (c) 2013, 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.
/**
* An indexed database object for storing client-side data
* in web apps.
*/
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.FIREFOX, '15')
@SupportedBrowser(SupportedBrowser.IE, '10')
@Unstable()
@Native("IDBDatabase")
class Database extends EventTarget {
ObjectStore createObjectStore(String name, {keyPath, bool? autoIncrement}) {
var options = {};
if (keyPath != null) {
options['keyPath'] = keyPath;
}
if (autoIncrement != null) {
options['autoIncrement'] = autoIncrement;
}
return _createObjectStore(name, options);
}
Transaction transaction(storeName_OR_storeNames, String mode) {
if (mode != 'readonly' && mode != 'readwrite') {
throw new ArgumentError(mode);
}
// TODO(sra): Ensure storeName_OR_storeNames is a string or List<String>,
// and copy to JavaScript array if necessary.
// Try and create a transaction with a string mode. Browsers that expect a
// numeric mode tend to convert the string into a number. This fails
// silently, resulting in zero ('readonly').
return _transaction(storeName_OR_storeNames, mode);
}
Transaction transactionStore(String storeName, String mode) {
if (mode != 'readonly' && mode != 'readwrite') {
throw new ArgumentError(mode);
}
// Try and create a transaction with a string mode. Browsers that expect a
// numeric mode tend to convert the string into a number. This fails
// silently, resulting in zero ('readonly').
return _transaction(storeName, mode);
}
Transaction transactionList(List<String> storeNames, String mode) {
if (mode != 'readonly' && mode != 'readwrite') {
throw new ArgumentError(mode);
}
List storeNames_1 = convertDartToNative_StringArray(storeNames);
return _transaction(storeNames_1, mode);
}
Transaction transactionStores(DomStringList storeNames, String mode) {
if (mode != 'readonly' && mode != 'readwrite') {
throw new ArgumentError(mode);
}
return _transaction(storeNames, mode);
}
@JSName('transaction')
Transaction _transaction(stores, mode) native;
// To suppress missing implicit constructor warnings.
factory Database._() {
throw new UnsupportedError("Not supported");
}
/**
* Static factory designed to expose `abort` events to event
* handlers that are not necessarily instances of [Database].
*
* See [EventStreamProvider] for usage information.
*/
static const EventStreamProvider<Event> abortEvent =
const EventStreamProvider<Event>('abort');
/**
* Static factory designed to expose `close` events to event
* handlers that are not necessarily instances of [Database].
*
* See [EventStreamProvider] for usage information.
*/
static const EventStreamProvider<Event> closeEvent =
const EventStreamProvider<Event>('close');
/**
* Static factory designed to expose `error` events to event
* handlers that are not necessarily instances of [Database].
*
* See [EventStreamProvider] for usage information.
*/
static const EventStreamProvider<Event> errorEvent =
const EventStreamProvider<Event>('error');
/**
* Static factory designed to expose `versionchange` events to event
* handlers that are not necessarily instances of [Database].
*
* See [EventStreamProvider] for usage information.
*/
static const EventStreamProvider<VersionChangeEvent> versionChangeEvent =
const EventStreamProvider<VersionChangeEvent>('versionchange');
String? get name native;
@Returns('DomStringList')
@Creates('DomStringList')
List<String>? get objectStoreNames native;
@Creates('int|String|Null')
@Returns('int|String|Null')
int? get version native;
void close() native;
ObjectStore _createObjectStore(String name, [Map? options]) {
if (options != null) {
var options_1 = convertDartToNative_Dictionary(options);
return _createObjectStore_1(name, options_1);
}
return _createObjectStore_2(name);
}
@JSName('createObjectStore')
ObjectStore _createObjectStore_1(name, options) native;
@JSName('createObjectStore')
ObjectStore _createObjectStore_2(name) native;
void deleteObjectStore(String name) native;
/// Stream of `abort` events handled by this [Database].
Stream<Event> get onAbort => abortEvent.forTarget(this);
/// Stream of `close` events handled by this [Database].
Stream<Event> get onClose => closeEvent.forTarget(this);
/// Stream of `error` events handled by this [Database].
Stream<Event> get onError => errorEvent.forTarget(this);
/// Stream of `versionchange` events handled by this [Database].
Stream<VersionChangeEvent> get onVersionChange =>
versionChangeEvent.forTarget(this);
}
// 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.
// WARNING: Do not edit - generated code.
typedef void ObserverCallback(ObserverChanges changes);
// 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.
@SupportedBrowser(SupportedBrowser.CHROME)
@SupportedBrowser(SupportedBrowser.FIREFOX, '15')
@SupportedBrowser(SupportedBrowser.IE, '10')
@Unstable()
@Native("IDBFactory")
class IdbFactory extends Interceptor {
/**
* Checks to see if Indexed DB is supported on the current platform.
*/
static bool get supported {
return JS(
'bool',
'!!(window.indexedDB || '
'window.webkitIndexedDB || '
'window.mozIndexedDB)');
}
Future<Database> open(String name,
{int? version,
void onUpgradeNeeded(VersionChangeEvent event)?,
void onBlocked(Event event)?}) {
if ((version == null) != (onUpgradeNeeded == null)) {
return new Future.error(new ArgumentError(
'version and onUpgradeNeeded must be specified together'));
}
try {
OpenDBRequest request;
if (version != null) {
request = _open(name, version);
} else {
request = _open(name);
}
if (onUpgradeNeeded != null) {
request.onUpgradeNeeded.listen(onUpgradeNeeded);
}
if (onBlocked != null) {
request.onBlocked.listen(onBlocked);
}
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future<IdbFactory> deleteDatabase(String name, {void onBlocked(Event e)?}) {
try {
var request = _deleteDatabase(name);
if (onBlocked != null) {
request.onBlocked.listen(onBlocked);
}
var completer = new Completer<IdbFactory>.sync();
request.onSuccess.listen((e) {
completer.complete(this);
});
request.onError.listen(completer.completeError);
return completer.future;
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
/**
* Checks to see if getDatabaseNames is supported by the current platform.
* TODO(terry): Should be able to always return false?
*/
bool get supportsDatabaseNames {
return supported &&
JS('bool', '!!(#.getDatabaseNames || #.webkitGetDatabaseNames)', this,
this);
}
// To suppress missing implicit constructor warnings.
factory IdbFactory._() {
throw new UnsupportedError("Not supported");
}
int cmp(Object first, Object second) native;
@JSName('deleteDatabase')
OpenDBRequest _deleteDatabase(String name) native;
@JSName('open')
@Returns('Request')
@Creates('Request')
@Creates('Database')
OpenDBRequest _open(String name, [int? version]) native;
}
/**
* Ties a request to a completer, so the completer is completed when it succeeds
* and errors out when the request errors.
*/
Future<T> _completeRequest<T>(Request request) {
var completer = new Completer<T>.sync();
// TODO: make sure that completer.complete is synchronous as transactions
// may be committed if the result is not processed immediately.
request.onSuccess.listen((e) {
T result = request.result;
completer.complete(result);
});
request.onError.listen(completer.completeError);
return completer.future;
}
// Copyright (c) 2013, 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.
@Unstable()
@Native("IDBIndex")
class Index extends Interceptor {
Future<int> count([key_OR_range]) {
try {
var request = _count(key_OR_range);
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future get(key) {
try {
var request = _get(key);
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future getKey(key) {
try {
var request = _getKey(key);
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
/**
* Creates a stream of cursors over the records in this object store.
*
* See also:
*
* * [ObjectStore.openCursor]
*/
Stream<CursorWithValue> 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;
}
var request;
if (direction == null) {
// FIXME: Passing in "next" should be unnecessary.
request = _openCursor(key_OR_range, "next");
} else {
request = _openCursor(key_OR_range, direction);
}
return ObjectStore._cursorStreamFromResult(request, autoAdvance);
}
/**
* Creates a stream of cursors over the records in this object store.
*
* See also:
*
* * [ObjectStore.openCursor]
*/
Stream<Cursor> openKeyCursor(
{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;
}
var request;
if (direction == null) {
// FIXME: Passing in "next" should be unnecessary.
request = _openKeyCursor(key_OR_range, "next");
} else {
request = _openKeyCursor(key_OR_range, direction);
}
return ObjectStore._cursorStreamFromResult(request, autoAdvance);
}
// To suppress missing implicit constructor warnings.
factory Index._() {
throw new UnsupportedError("Not supported");
}
@annotation_Creates_SerializedScriptValue
Object? get keyPath native;
bool? get multiEntry native;
String? get name native;
set name(String? value) native;
ObjectStore? get objectStore native;
bool? get unique native;
@JSName('count')
Request _count(Object? key) native;
@JSName('get')
@Returns('Request')
@Creates('Request')
@annotation_Creates_SerializedScriptValue
Request _get(Object key) native;
Request getAll(Object? query, [int? count]) native;
Request getAllKeys(Object? query, [int? count]) native;
@JSName('getKey')
@Returns('Request')
@Creates('Request')
@annotation_Creates_SerializedScriptValue
@Creates('ObjectStore')
Request _getKey(Object key) native;
@JSName('openCursor')
@Returns('Request')
@Creates('Request')
@Creates('Cursor')
Request _openCursor(Object? range, [String? direction]) native;
@JSName('openKeyCursor')
@Returns('Request')
@Creates('Request')
@Creates('Cursor')
Request _openKeyCursor(Object? range, [String? direction]) native;
}
// 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.
@Unstable()
@Native("IDBKeyRange")
class KeyRange extends Interceptor {
factory KeyRange.only(/*Key*/ value) =>
_KeyRangeFactoryProvider.createKeyRange_only(value);
factory KeyRange.lowerBound(/*Key*/ bound, [bool open = false]) =>
_KeyRangeFactoryProvider.createKeyRange_lowerBound(bound, open);
factory KeyRange.upperBound(/*Key*/ bound, [bool open = false]) =>
_KeyRangeFactoryProvider.createKeyRange_upperBound(bound, open);
factory KeyRange.bound(/*Key*/ lower, /*Key*/ upper,
[bool lowerOpen = false, bool upperOpen = false]) =>
_KeyRangeFactoryProvider.createKeyRange_bound(
lower, upper, lowerOpen, upperOpen);
// To suppress missing implicit constructor warnings.
factory KeyRange._() {
throw new UnsupportedError("Not supported");
}
@annotation_Creates_SerializedScriptValue
Object? get lower native;
bool? get lowerOpen native;
@annotation_Creates_SerializedScriptValue
Object? get upper native;
bool? get upperOpen native;
@JSName('bound')
static KeyRange bound_(Object lower, Object upper,
[bool? lowerOpen, bool? upperOpen]) native;
bool includes(Object key) native;
@JSName('lowerBound')
static KeyRange lowerBound_(Object bound, [bool? open]) native;
@JSName('only')
static KeyRange only_(Object value) native;
@JSName('upperBound')
static KeyRange upperBound_(Object bound, [bool? open]) native;
}
// 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.
@Unstable()
@Native("IDBObjectStore")
class ObjectStore extends Interceptor {
Future add(value, [key]) {
try {
var request;
if (key != null) {
request = _add(value, key);
} else {
request = _add(value);
}
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future clear() {
try {
return _completeRequest(_clear());
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future delete(key_OR_keyRange) {
try {
return _completeRequest(_delete(key_OR_keyRange));
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future<int> count([key_OR_range]) {
try {
var request = _count(key_OR_range);
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future put(value, [key]) {
try {
var request;
if (key != null) {
request = _put(value, key);
} else {
request = _put(value);
}
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
Future getObject(key) {
try {
var request = _get(key);
return _completeRequest(request);
} catch (e, stacktrace) {
return new Future.error(e, stacktrace);
}
}
/**
* 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.**
*
* var cursors = objectStore.openCursor().listen(
* (cursor) {
* // ...some processing with the cursor
* cursor.next(); // advance onto the next cursor.
* },
* onDone: () {
* // called when there are no more cursors.
* print('all done!');
* });
*
* Asynchronous operations which are not related to the current transaction
* will cause the transaction to automatically be committed-- all processing
* must be done synchronously unless they are additional async requests to
* the current transaction.
*/
Stream<CursorWithValue> 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 = _openCursor(key_OR_range);
} else {
request = _openCursor(key_OR_range, direction);
}
return _cursorStreamFromResult(request, autoAdvance);
}
Index createIndex(String name, keyPath, {bool? unique, bool? multiEntry}) {
var options = {};
if (unique != null) {
options['unique'] = unique;
}
if (multiEntry != null) {
options['multiEntry'] = multiEntry;
}
return _createIndex(name, keyPath, options);
}
// To suppress missing implicit constructor warnings.
factory ObjectStore._() {
throw new UnsupportedError("Not supported");
}
bool? get autoIncrement native;
@Returns('DomStringList')
@Creates('DomStringList')
List<String>? get indexNames native;
@annotation_Creates_SerializedScriptValue
Object? get keyPath native;
String? get name native;
set name(String? value) native;
Transaction? get transaction native;
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _add(/*any*/ value, [/*any*/ key]) {
if (key != null) {
var value_1 = convertDartToNative_SerializedScriptValue(value);
var key_2 = convertDartToNative_SerializedScriptValue(key);
return _add_1(value_1, key_2);
}
var value_1 = convertDartToNative_SerializedScriptValue(value);
return _add_2(value_1);
}
@JSName('add')
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _add_1(value, key) native;
@JSName('add')
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _add_2(value) native;
@JSName('clear')
Request _clear() native;
@JSName('count')
Request _count(Object? key) native;
Index _createIndex(String name, Object keyPath, [Map? options]) {
if (options != null) {
var options_1 = convertDartToNative_Dictionary(options);
return _createIndex_1(name, keyPath, options_1);
}
return _createIndex_2(name, keyPath);
}
@JSName('createIndex')
Index _createIndex_1(name, keyPath, options) native;
@JSName('createIndex')
Index _createIndex_2(name, keyPath) native;
@JSName('delete')
Request _delete(Object key) native;
void deleteIndex(String name) native;
@JSName('get')
@Returns('Request')
@Creates('Request')
@annotation_Creates_SerializedScriptValue
Request _get(Object key) native;
Request getAll(Object? query, [int? count]) native;
Request getAllKeys(Object? query, [int? count]) native;
Request getKey(Object key) native;
Index index(String name) native;
@JSName('openCursor')
@Returns('Request')
@Creates('Request')
@Creates('Cursor')
Request _openCursor(Object? range, [String? direction]) native;
Request openKeyCursor(Object? range, [String? direction]) native;
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _put(/*any*/ value, [/*any*/ key]) {
if (key != null) {
var value_1 = convertDartToNative_SerializedScriptValue(value);
var key_2 = convertDartToNative_SerializedScriptValue(key);
return _put_1(value_1, key_2);
}
var value_1 = convertDartToNative_SerializedScriptValue(value);
return _put_2(value_1);
}
@JSName('put')
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _put_1(value, key) native;
@JSName('put')
@Returns('Request')
@Creates('Request')
@_annotation_Creates_IDBKey
Request _put_2(value) native;
/**
* Helper for iterating over cursors in a request.
*/
static Stream<T> _cursorStreamFromResult<T extends Cursor>(
Request request, bool? autoAdvance) {
// TODO: need to guarantee that the controller provides the values
// immediately as waiting until the next tick will cause the transaction to
// close.
var controller = new StreamController<T>(sync: true);
//TODO: Report stacktrace once issue 4061 is resolved.
request.onError.listen(controller.addError);
request.onSuccess.listen((e) {
T? cursor = request.result as dynamic;
if (cursor == null) {
controller.close();
} else {