umd.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*!
  2. MIT License
  3. Copyright (c) 2018 Arturas Molcanovas <a.molcanovas@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. (function (global, factory) {
  21. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  22. typeof define === 'function' && define.amd ? define('localforage-driver-memory', ['exports'], factory) :
  23. factory(global.LocalforageDriverMemory = {});
  24. }(typeof self !== 'undefined' ? self : this, function (exports) { 'use strict';
  25. var _driver = 'localforage-driver-memory';
  26. /*! *****************************************************************************
  27. Copyright (c) Microsoft Corporation. All rights reserved.
  28. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  29. this file except in compliance with the License. You may obtain a copy of the
  30. License at http://www.apache.org/licenses/LICENSE-2.0
  31. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  32. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  33. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  34. MERCHANTABLITY OR NON-INFRINGEMENT.
  35. See the Apache Version 2.0 License for specific language governing permissions
  36. and limitations under the License.
  37. ***************************************************************************** */
  38. function __values(o) {
  39. var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
  40. if (m) return m.call(o);
  41. return {
  42. next: function () {
  43. if (o && i >= o.length) o = void 0;
  44. return { value: o && o[i++], done: !o };
  45. }
  46. };
  47. }
  48. /*!
  49. MIT License
  50. Copyright (c) 2018 Arturas Molcanovas <a.molcanovas@gmail.com>
  51. Permission is hereby granted, free of charge, to any person obtaining a copy
  52. of this software and associated documentation files (the "Software"), to deal
  53. in the Software without restriction, including without limitation the rights
  54. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  55. copies of the Software, and to permit persons to whom the Software is
  56. furnished to do so, subject to the following conditions:
  57. The above copyright notice and this permission notice shall be included in all
  58. copies or substantial portions of the Software.
  59. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  60. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  61. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  62. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  63. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  64. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  65. SOFTWARE.
  66. */
  67. /**
  68. * Abstracts constructing a Blob object, so it also works in older
  69. * browsers that don't support the native Blob constructor. (i.e.
  70. * old QtWebKit versions, at least).
  71. * Abstracts constructing a Blob object, so it also works in older
  72. * browsers that don't support the native Blob constructor. (i.e.
  73. * old QtWebKit versions, at least).
  74. *
  75. * @param parts
  76. * @param properties
  77. */
  78. function createBlob(parts, properties) {
  79. /* global BlobBuilder,MSBlobBuilder,MozBlobBuilder,WebKitBlobBuilder */
  80. parts = parts || [];
  81. properties = properties || {};
  82. try {
  83. return new Blob(parts, properties);
  84. }
  85. catch (e) {
  86. if (e.name !== 'TypeError') {
  87. throw e;
  88. }
  89. //tslint:disable-next-line:variable-name
  90. var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder
  91. : typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder
  92. : typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder
  93. : WebKitBlobBuilder;
  94. var builder = new Builder();
  95. for (var i = 0; i < parts.length; i += 1) {
  96. builder.append(parts[i]);
  97. }
  98. return builder.getBlob(properties.type);
  99. }
  100. }
  101. var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/;
  102. var SERIALIZED_MARKER_LENGTH = "__lfsc__:" /* SERIALIZED_MARKER */.length;
  103. var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + "arbf" /* TYPE_ARRAYBUFFER */.length;
  104. //tslint:disable:no-magic-numbers no-bitwise prefer-switch no-unbound-method
  105. var toString = Object.prototype.toString;
  106. function stringToBuffer(serializedString) {
  107. // Fill the string into a ArrayBuffer.
  108. var bufferLength = serializedString.length * 0.75;
  109. var len = serializedString.length;
  110. if (serializedString[serializedString.length - 1] === '=') {
  111. bufferLength--;
  112. if (serializedString[serializedString.length - 2] === '=') {
  113. bufferLength--;
  114. }
  115. }
  116. var buffer = new ArrayBuffer(bufferLength);
  117. var bytes = new Uint8Array(buffer);
  118. for (var i = 0, p = 0; i < len; i += 4) {
  119. var encoded1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */.indexOf(serializedString[i]);
  120. var encoded2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */.indexOf(serializedString[i + 1]);
  121. var encoded3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */.indexOf(serializedString[i + 2]);
  122. var encoded4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */.indexOf(serializedString[i + 3]);
  123. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  124. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  125. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  126. }
  127. return buffer;
  128. }
  129. /**
  130. * Converts a buffer to a string to store, serialized, in the backend
  131. * storage library.
  132. */
  133. function bufferToString(buffer) {
  134. // base64-arraybuffer
  135. var bytes = new Uint8Array(buffer);
  136. var base64String = '';
  137. for (var i = 0; i < bytes.length; i += 3) {
  138. /*jslint bitwise: true */
  139. base64String += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */[bytes[i] >> 2];
  140. base64String += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  141. base64String +=
  142. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  143. base64String += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" /* BASE_CHARS */[bytes[i + 2] & 63];
  144. }
  145. if (bytes.length % 3 === 2) {
  146. base64String = base64String.substring(0, base64String.length - 1) + '=';
  147. }
  148. else if (bytes.length % 3 === 1) {
  149. base64String = base64String.substring(0, base64String.length - 2) + '==';
  150. }
  151. return base64String;
  152. }
  153. /**
  154. * Serialize a value, afterwards executing a callback (which usually
  155. * instructs the `setItem()` callback/promise to be executed). This is how
  156. * we store binary data with localStorage.
  157. * @param value
  158. * @param callback
  159. */
  160. function serialize(value, callback) {
  161. var valueType = '';
  162. if (value) {
  163. valueType = toString.call(value);
  164. }
  165. // Cannot use `value instanceof ArrayBuffer` or such here, as these
  166. // checks fail when running the tests using casper.js...
  167. if (value && (valueType === '[object ArrayBuffer]' ||
  168. (value.buffer && toString.call(value.buffer) === '[object ArrayBuffer]'))) {
  169. // Convert binary arrays to a string and prefix the string with
  170. // a special marker.
  171. var buffer = void 0;
  172. var marker = "__lfsc__:" /* SERIALIZED_MARKER */;
  173. if (value instanceof ArrayBuffer) {
  174. buffer = value;
  175. marker += "arbf" /* TYPE_ARRAYBUFFER */;
  176. }
  177. else {
  178. buffer = value.buffer;
  179. if (valueType === '[object Int8Array]') {
  180. marker += "si08" /* TYPE_INT8ARRAY */;
  181. }
  182. else if (valueType === '[object Uint8Array]') {
  183. marker += "ui08" /* TYPE_UINT8ARRAY */;
  184. }
  185. else if (valueType === '[object Uint8ClampedArray]') {
  186. marker += "uic8" /* TYPE_UINT8CLAMPEDARRAY */;
  187. }
  188. else if (valueType === '[object Int16Array]') {
  189. marker += "si16" /* TYPE_INT16ARRAY */;
  190. }
  191. else if (valueType === '[object Uint16Array]') {
  192. marker += "ur16" /* TYPE_UINT16ARRAY */;
  193. }
  194. else if (valueType === '[object Int32Array]') {
  195. marker += "si32" /* TYPE_INT32ARRAY */;
  196. }
  197. else if (valueType === '[object Uint32Array]') {
  198. marker += "ui32" /* TYPE_UINT32ARRAY */;
  199. }
  200. else if (valueType === '[object Float32Array]') {
  201. marker += "fl32" /* TYPE_FLOAT32ARRAY */;
  202. }
  203. else if (valueType === '[object Float64Array]') {
  204. marker += "fl64" /* TYPE_FLOAT64ARRAY */;
  205. }
  206. else {
  207. callback(new Error('Failed to get type for BinaryArray'));
  208. }
  209. }
  210. callback(marker + bufferToString(buffer));
  211. }
  212. else if (valueType === '[object Blob]') {
  213. // Convert the blob to a binaryArray and then to a string.
  214. var fileReader = new FileReader();
  215. fileReader.onload = function () {
  216. // Backwards-compatible prefix for the blob type.
  217. //tslint:disable-next-line:restrict-plus-operands
  218. var str = "~~local_forage_type~" /* BLOB_TYPE_PREFIX */ + value.type + "~" + bufferToString(this.result);
  219. callback("__lfsc__:" /* SERIALIZED_MARKER */ + "blob" /* TYPE_BLOB */ + str);
  220. };
  221. fileReader.readAsArrayBuffer(value);
  222. }
  223. else {
  224. try {
  225. callback(JSON.stringify(value));
  226. }
  227. catch (e) {
  228. console.error('Couldn\'t convert value into a JSON string: ', value);
  229. callback(null, e);
  230. }
  231. }
  232. }
  233. /**
  234. * Deserialize data we've inserted into a value column/field. We place
  235. * special markers into our strings to mark them as encoded; this isn't
  236. * as nice as a meta field, but it's the only sane thing we can do whilst
  237. * keeping localStorage support intact.
  238. *
  239. * Oftentimes this will just deserialize JSON content, but if we have a
  240. * special marker (SERIALIZED_MARKER, defined above), we will extract
  241. * some kind of arraybuffer/binary data/typed array out of the string.
  242. * @param value
  243. */
  244. function deserialize(value) {
  245. // If we haven't marked this string as being specially serialized (i.e.
  246. // something other than serialized JSON), we can just return it and be
  247. // done with it.
  248. if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== "__lfsc__:" /* SERIALIZED_MARKER */) {
  249. return JSON.parse(value);
  250. }
  251. // The following code deals with deserializing some kind of Blob or
  252. // TypedArray. First we separate out the type of data we're dealing
  253. // with from the data itself.
  254. var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
  255. var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH);
  256. var blobType;
  257. // Backwards-compatible blob type serialization strategy.
  258. // DBs created with older versions of localForage will simply not have the blob type.
  259. if (type === "blob" /* TYPE_BLOB */ && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) {
  260. var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX);
  261. blobType = matcher[1];
  262. serializedString = serializedString.substring(matcher[0].length);
  263. }
  264. var buffer = stringToBuffer(serializedString);
  265. // Return the right type based on the code/type set during
  266. // serialization.
  267. switch (type) {
  268. case "arbf" /* TYPE_ARRAYBUFFER */:
  269. return buffer;
  270. case "blob" /* TYPE_BLOB */:
  271. return createBlob([buffer], { type: blobType });
  272. case "si08" /* TYPE_INT8ARRAY */:
  273. return new Int8Array(buffer);
  274. case "ui08" /* TYPE_UINT8ARRAY */:
  275. return new Uint8Array(buffer);
  276. case "uic8" /* TYPE_UINT8CLAMPEDARRAY */:
  277. return new Uint8ClampedArray(buffer);
  278. case "si16" /* TYPE_INT16ARRAY */:
  279. return new Int16Array(buffer);
  280. case "ur16" /* TYPE_UINT16ARRAY */:
  281. return new Uint16Array(buffer);
  282. case "si32" /* TYPE_INT32ARRAY */:
  283. return new Int32Array(buffer);
  284. case "ui32" /* TYPE_UINT32ARRAY */:
  285. return new Uint32Array(buffer);
  286. case "fl32" /* TYPE_FLOAT32ARRAY */:
  287. return new Float32Array(buffer);
  288. case "fl64" /* TYPE_FLOAT64ARRAY */:
  289. return new Float64Array(buffer);
  290. default:
  291. throw new Error('Unkown type: ' + type);
  292. }
  293. }
  294. function clone(obj) {
  295. var e_1, _a;
  296. if (obj === null || typeof (obj) !== 'object' || 'isActiveClone' in obj) {
  297. return obj;
  298. }
  299. var temp = obj instanceof Date ? new Date(obj) : (obj.constructor());
  300. try {
  301. for (var _b = __values(Object.keys(obj)), _c = _b.next(); !_c.done; _c = _b.next()) {
  302. var key = _c.value;
  303. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  304. obj['isActiveClone'] = null;
  305. temp[key] = clone(obj[key]);
  306. delete obj['isActiveClone'];
  307. }
  308. }
  309. }
  310. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  311. finally {
  312. try {
  313. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  314. }
  315. finally { if (e_1) throw e_1.error; }
  316. }
  317. return temp;
  318. }
  319. function getKeyPrefix(options, defaultConfig) {
  320. return (options.name || defaultConfig.name) + "/" + (options.storeName || defaultConfig.storeName) + "/";
  321. }
  322. function executeCallback(promise, callback) {
  323. if (callback) {
  324. promise.then(function (result) {
  325. callback(null, result);
  326. }, function (error) {
  327. callback(error);
  328. });
  329. }
  330. }
  331. function getCallback() {
  332. var _args = [];
  333. for (var _i = 0; _i < arguments.length; _i++) {
  334. _args[_i] = arguments[_i];
  335. }
  336. if (arguments.length && typeof arguments[arguments.length - 1] === 'function') {
  337. return arguments[arguments.length - 1];
  338. }
  339. }
  340. //tslint:disable-next-line:no-ignored-initial-value
  341. function dropInstanceCommon(options, callback) {
  342. var _this = this;
  343. callback = getCallback.apply(this, arguments);
  344. options = (typeof options !== 'function' && options) || {};
  345. if (!options.name) {
  346. var currentConfig = this.config();
  347. options.name = options.name || currentConfig.name;
  348. options.storeName = options.storeName || currentConfig.storeName;
  349. }
  350. var promise;
  351. if (!options.name) {
  352. promise = Promise.reject('Invalid arguments');
  353. }
  354. else {
  355. promise = new Promise(function (resolve) {
  356. if (!options.storeName) {
  357. resolve(options.name + "/");
  358. }
  359. else {
  360. resolve(getKeyPrefix(options, _this._defaultConfig));
  361. }
  362. });
  363. }
  364. return { promise: promise, callback: callback };
  365. }
  366. function normaliseKey(key) {
  367. // Cast the key to a string, as that's all we can set as a key.
  368. if (typeof key !== 'string') {
  369. console.warn(key + " used as a key, but it is not a string.");
  370. key = String(key);
  371. }
  372. return key;
  373. }
  374. var serialiser = {
  375. bufferToString: bufferToString,
  376. deserialize: deserialize,
  377. serialize: serialize,
  378. stringToBuffer: stringToBuffer
  379. };
  380. var stores = {};
  381. /** @internal */
  382. var Store = /** @class */ (function () {
  383. function Store(kp) {
  384. this.kp = kp;
  385. this.data = {};
  386. }
  387. Store.resolve = function (kp) {
  388. if (!stores[kp]) {
  389. stores[kp] = new Store(kp);
  390. }
  391. return stores[kp];
  392. };
  393. Store.prototype.clear = function () {
  394. this.data = {};
  395. };
  396. Store.prototype.drop = function () {
  397. this.clear();
  398. delete stores[this.kp];
  399. };
  400. Store.prototype.get = function (key) {
  401. return this.data[key];
  402. };
  403. Store.prototype.key = function (idx) {
  404. return this.keys()[idx];
  405. };
  406. Store.prototype.keys = function () {
  407. return Object.keys(this.data);
  408. };
  409. Store.prototype.rm = function (k) {
  410. delete this.data[k];
  411. };
  412. Store.prototype.set = function (k, v) {
  413. this.data[k] = v;
  414. };
  415. return Store;
  416. }());
  417. function _initStorage(options) {
  418. var opts = options ? clone(options) : {};
  419. var kp = getKeyPrefix(opts, this._defaultConfig);
  420. var store = Store.resolve(kp);
  421. this._dbInfo = opts;
  422. this._dbInfo.serializer = serialiser;
  423. this._dbInfo.keyPrefix = kp;
  424. this._dbInfo.mStore = store;
  425. return Promise.resolve();
  426. }
  427. function clear(callback) {
  428. var _this = this;
  429. var promise = this.ready().then(function () {
  430. _this._dbInfo.mStore.clear();
  431. });
  432. executeCallback(promise, callback);
  433. return promise;
  434. }
  435. function dropInstance(_options, _cb) {
  436. var _a = dropInstanceCommon.apply(this, arguments), promise = _a.promise, callback = _a.callback;
  437. var outPromise = promise.then(function (keyPrefix) {
  438. Store.resolve(keyPrefix).drop();
  439. });
  440. executeCallback(outPromise, callback);
  441. return promise;
  442. }
  443. function getItem(key$, callback) {
  444. var _this = this;
  445. key$ = normaliseKey(key$);
  446. var promise = this.ready().then(function () {
  447. var result = _this._dbInfo.mStore.get(key$);
  448. // Deserialise if the result is not null or undefined
  449. return result == null ? null : _this._dbInfo.serializer.deserialize(result); //tslint:disable-line:triple-equals
  450. });
  451. executeCallback(promise, callback);
  452. return promise;
  453. }
  454. function iterate(iterator, callback) {
  455. var _this = this;
  456. var promise = this.ready().then(function () {
  457. var store = _this._dbInfo.mStore;
  458. var keys = store.keys();
  459. for (var i = 0; i < keys.length; i++) {
  460. var value = store.get(keys[i]);
  461. // If a result was found, parse it from the serialized
  462. // string into a JS object. If result isn't truthy, the
  463. // key is likely undefined and we'll pass it straight
  464. // to the iterator.
  465. if (value) {
  466. value = _this._dbInfo.serializer.deserialize(value);
  467. }
  468. value = iterator(value, keys[i], i + 1);
  469. if (value !== undefined) {
  470. return value;
  471. }
  472. }
  473. });
  474. executeCallback(promise, callback);
  475. return promise;
  476. }
  477. function key(idx, callback) {
  478. var _this = this;
  479. var promise = this.ready().then(function () {
  480. var result;
  481. try {
  482. result = _this._dbInfo.mStore.key(idx);
  483. if (result === undefined) {
  484. result = null;
  485. }
  486. }
  487. catch (_a) {
  488. result = null;
  489. }
  490. return result;
  491. });
  492. executeCallback(promise, callback);
  493. return promise;
  494. }
  495. function keys(callback) {
  496. var _this = this;
  497. var promise = this.ready().then(function () {
  498. return _this._dbInfo.mStore.keys();
  499. });
  500. executeCallback(promise, callback);
  501. return promise;
  502. }
  503. function length(callback) {
  504. var promise = this.keys().then(function (keys$) { return keys$.length; });
  505. executeCallback(promise, callback);
  506. return promise;
  507. }
  508. function removeItem(key$, callback) {
  509. var _this = this;
  510. key$ = normaliseKey(key$);
  511. var promise = this.ready().then(function () {
  512. _this._dbInfo.mStore.rm(key$);
  513. });
  514. executeCallback(promise, callback);
  515. return promise;
  516. }
  517. function setItem(key$, value, callback) {
  518. var _this = this;
  519. key$ = normaliseKey(key$);
  520. var promise = this.ready().then(function () {
  521. if (value === undefined) {
  522. value = null;
  523. }
  524. // Save the original value to pass to the callback.
  525. var originalValue = value;
  526. return new Promise(function (resolve, reject) {
  527. _this._dbInfo.serializer.serialize(value, function (value$, error) {
  528. if (error) {
  529. reject(error);
  530. }
  531. else {
  532. try {
  533. _this._dbInfo.mStore.set(key$, value$);
  534. resolve(originalValue);
  535. }
  536. catch (e) {
  537. reject(e);
  538. }
  539. }
  540. });
  541. });
  542. });
  543. executeCallback(promise, callback);
  544. return promise;
  545. }
  546. var _support = true;
  547. exports._support = _support;
  548. exports._driver = _driver;
  549. exports._initStorage = _initStorage;
  550. exports.clear = clear;
  551. exports.dropInstance = dropInstance;
  552. exports.getItem = getItem;
  553. exports.iterate = iterate;
  554. exports.key = key;
  555. exports.keys = keys;
  556. exports.length = length;
  557. exports.removeItem = removeItem;
  558. exports.setItem = setItem;
  559. Object.defineProperty(exports, '__esModule', { value: true });
  560. }));
  561. //# sourceMappingURL=umd.js.map