OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * A client-side key-value store with support for indexes. |
| 3 * |
| 4 * Many browsers support IndexedDB—a web standard for |
| 5 * an indexed database. |
| 6 * By storing data on the client in an IndexedDB, |
| 7 * a web app gets some advantages, such as faster performance and persistence. |
| 8 * To find out which browsers support IndexedDB, |
| 9 * refer to [Can I Use?](http://caniuse.com/#feat=indexeddb) |
| 10 * |
| 11 * In IndexedDB, each record is identified by a unique index or key, |
| 12 * making data retrieval speedy. |
| 13 * You can store structured data, |
| 14 * such as images, arrays, and maps using IndexedDB. |
| 15 * The standard does not specify size limits for individual data items |
| 16 * or for the database itself, but browsers may impose storage limits. |
| 17 * |
| 18 * ## Using indexed_db |
| 19 * |
| 20 * The classes in this library provide an interface |
| 21 * to the browser's IndexedDB, if it has one. |
| 22 * To use this library in your code: |
| 23 * |
| 24 * import 'dart:indexed_db'; |
| 25 * |
| 26 * A web app can determine if the browser supports |
| 27 * IndexedDB with [IdbFactory.supported]: |
| 28 * |
| 29 * if (IdbFactory.supported) |
| 30 * // Use indexeddb. |
| 31 * else |
| 32 * // Find an alternative. |
| 33 * |
| 34 * Access to the browser's IndexedDB is provided by the app's top-level |
| 35 * [Window] object, which your code can refer to with `window.indexedDB`. |
| 36 * So, for example, |
| 37 * here's how to use window.indexedDB to open a database: |
| 38 * |
| 39 * Future open() { |
| 40 * return window.indexedDB.open('myIndexedDB', |
| 41 * version: 1, |
| 42 * onUpgradeNeeded: _initializeDatabase) |
| 43 * .then(_loadFromDB); |
| 44 * } |
| 45 * void _initializeDatabase(VersionChangeEvent e) { |
| 46 * ... |
| 47 * } |
| 48 * Future _loadFromDB(Database db) { |
| 49 * ... |
| 50 * } |
| 51 * |
| 52 * |
| 53 * All data in an IndexedDB is stored within an [ObjectStore]. |
| 54 * To manipulate the database use [Transaction]s. |
| 55 * |
| 56 * ## Other resources |
| 57 * |
| 58 * Other options for client-side data storage include: |
| 59 * |
| 60 * * [Window.localStorage]—a |
| 61 * basic mechanism that stores data as a [Map], |
| 62 * and where both the keys and the values are strings. |
| 63 * |
| 64 * * [dart:web_sql]—a database that can be queried with SQL. |
| 65 * |
| 66 * For a tutorial about using the indexed_db library with Dart, |
| 67 * check out |
| 68 * [Use IndexedDB](http://www.dartlang.org/docs/tutorials/indexeddb/). |
| 69 * |
| 70 * [IndexedDB reference](http://docs.webplatform.org/wiki/apis/indexeddb) |
| 71 * provides wiki-style docs about indexedDB |
| 72 */ |
| 73 library dart.dom.indexed_db; |
| 74 |
| 75 import 'dart:async'; |
| 76 import 'dart:html'; |
| 77 import 'dart:html_common'; |
| 78 import 'dart:_native_typed_data'; |
| 79 import 'dart:typed_data'; |
| 80 import 'dart:_js_helper' show Creates, Returns, JSName, Native; |
| 81 import 'dart:_foreign_helper' show JS; |
| 82 import 'dart:_interceptors' show Interceptor, JSExtendableArray; |
| 83 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 84 // for details. All rights reserved. Use of this source code is governed by a |
| 85 // BSD-style license that can be found in the LICENSE file. |
| 86 |
| 87 // DO NOT EDIT - unless you are editing documentation as per: |
| 88 // https://code.google.com/p/dart/wiki/ContributingHTMLDocumentation |
| 89 // Auto-generated dart:svg library. |
| 90 |
| 91 |
| 92 |
| 93 |
| 94 |
| 95 class _KeyRangeFactoryProvider { |
| 96 |
| 97 static KeyRange createKeyRange_only(/*Key*/ value) => |
| 98 _only(_class(), _translateKey(value)); |
| 99 |
| 100 static KeyRange createKeyRange_lowerBound( |
| 101 /*Key*/ bound, [bool open = false]) => |
| 102 _lowerBound(_class(), _translateKey(bound), open); |
| 103 |
| 104 static KeyRange createKeyRange_upperBound( |
| 105 /*Key*/ bound, [bool open = false]) => |
| 106 _upperBound(_class(), _translateKey(bound), open); |
| 107 |
| 108 static KeyRange createKeyRange_bound(/*Key*/ lower, /*Key*/ upper, |
| 109 [bool lowerOpen = false, bool upperOpen = false]) => |
| 110 _bound(_class(), _translateKey(lower), _translateKey(upper), |
| 111 lowerOpen, upperOpen); |
| 112 |
| 113 static var _cachedClass; |
| 114 |
| 115 static _class() { |
| 116 if (_cachedClass != null) return _cachedClass; |
| 117 return _cachedClass = _uncachedClass(); |
| 118 } |
| 119 |
| 120 static _uncachedClass() => |
| 121 JS('var', |
| 122 '''window.webkitIDBKeyRange || window.mozIDBKeyRange || |
| 123 window.msIDBKeyRange || window.IDBKeyRange'''); |
| 124 |
| 125 static _translateKey(idbkey) => idbkey; // TODO: fixme. |
| 126 |
| 127 static KeyRange _only(cls, value) => |
| 128 JS('KeyRange', '#.only(#)', cls, value); |
| 129 |
| 130 static KeyRange _lowerBound(cls, bound, open) => |
| 131 JS('KeyRange', '#.lowerBound(#, #)', cls, bound, open); |
| 132 |
| 133 static KeyRange _upperBound(cls, bound, open) => |
| 134 JS('KeyRange', '#.upperBound(#, #)', cls, bound, open); |
| 135 |
| 136 static KeyRange _bound(cls, lower, upper, lowerOpen, upperOpen) => |
| 137 JS('KeyRange', '#.bound(#, #, #, #)', |
| 138 cls, lower, upper, lowerOpen, upperOpen); |
| 139 } |
| 140 |
| 141 // Conversions for IDBKey. |
| 142 // |
| 143 // Per http://www.w3.org/TR/IndexedDB/#key-construct |
| 144 // |
| 145 // "A value is said to be a valid key if it is one of the following types: Array |
| 146 // JavaScript objects [ECMA-262], DOMString [WEBIDL], Date [ECMA-262] or float |
| 147 // [WEBIDL]. However Arrays are only valid keys if every item in the array is |
| 148 // defined and is a valid key (i.e. sparse arrays can not be valid keys) and if |
| 149 // the Array doesn't directly or indirectly contain itself. Any non-numeric |
| 150 // properties are ignored, and thus does not affect whether the Array is a valid |
| 151 // key. Additionally, if the value is of type float, it is only a valid key if |
| 152 // it is not NaN, and if the value is of type Date it is only a valid key if its |
| 153 // [[PrimitiveValue]] internal property, as defined by [ECMA-262], is not NaN." |
| 154 |
| 155 // What is required is to ensure that an Lists in the key are actually |
| 156 // JavaScript arrays, and any Dates are JavaScript Dates. |
| 157 |
| 158 |
| 159 /** |
| 160 * Converts a native IDBKey into a Dart object. |
| 161 * |
| 162 * May return the original input. May mutate the original input (but will be |
| 163 * idempotent if mutation occurs). It is assumed that this conversion happens |
| 164 * on native IDBKeys on all paths that return IDBKeys from native DOM calls. |
| 165 * |
| 166 * If necessary, JavaScript Dates are converted into Dart Dates. |
| 167 */ |
| 168 _convertNativeToDart_IDBKey(nativeKey) { |
| 169 containsDate(object) { |
| 170 if (isJavaScriptDate(object)) return true; |
| 171 if (object is List) { |
| 172 for (int i = 0; i < object.length; i++) { |
| 173 if (containsDate(object[i])) return true; |
| 174 } |
| 175 } |
| 176 return false; // number, string. |
| 177 } |
| 178 if (containsDate(nativeKey)) { |
| 179 throw new UnimplementedError('Key containing DateTime'); |
| 180 } |
| 181 // TODO: Cache conversion somewhere? |
| 182 return nativeKey; |
| 183 } |
| 184 |
| 185 /** |
| 186 * Converts a Dart object into a valid IDBKey. |
| 187 * |
| 188 * May return the original input. Does not mutate input. |
| 189 * |
| 190 * If necessary, [dartKey] may be copied to ensure all lists are converted into |
| 191 * JavaScript Arrays and Dart Dates into JavaScript Dates. |
| 192 */ |
| 193 _convertDartToNative_IDBKey(dartKey) { |
| 194 // TODO: Implement. |
| 195 return dartKey; |
| 196 } |
| 197 |
| 198 |
| 199 |
| 200 /// May modify original. If so, action is idempotent. |
| 201 _convertNativeToDart_IDBAny(object) { |
| 202 return convertNativeToDart_AcceptStructuredClone(object, mustCopy: false); |
| 203 } |
| 204 |
| 205 // TODO(sra): Add DateTime. |
| 206 const String _idbKey = 'JSExtendableArray|=Object|num|String'; |
| 207 const _annotation_Creates_IDBKey = const Creates(_idbKey); |
| 208 const _annotation_Returns_IDBKey = const Returns(_idbKey); |
| 209 |
| 210 // FIXME: Can we make this private? |
| 211 final indexed_dbBlinkMap = { |
| 212 |
| 213 }; |
| 214 |
| 215 // FIXME: Can we make this private? |
| 216 final indexed_dbBlinkFunctionMap = { |
| 217 |
| 218 }; |
OLD | NEW |