fesm5.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. import { clone, getKeyPrefix, serialiser, executeCallback, dropInstanceCommon, normaliseKey } from 'localforage-driver-commons';
  21. var _driver = 'localforage-driver-memory';
  22. var stores = {};
  23. /** @internal */
  24. var Store = /** @class */ (function () {
  25. function Store(kp) {
  26. this.kp = kp;
  27. this.data = {};
  28. }
  29. Store.resolve = function (kp) {
  30. if (!stores[kp]) {
  31. stores[kp] = new Store(kp);
  32. }
  33. return stores[kp];
  34. };
  35. Store.prototype.clear = function () {
  36. this.data = {};
  37. };
  38. Store.prototype.drop = function () {
  39. this.clear();
  40. delete stores[this.kp];
  41. };
  42. Store.prototype.get = function (key) {
  43. return this.data[key];
  44. };
  45. Store.prototype.key = function (idx) {
  46. return this.keys()[idx];
  47. };
  48. Store.prototype.keys = function () {
  49. return Object.keys(this.data);
  50. };
  51. Store.prototype.rm = function (k) {
  52. delete this.data[k];
  53. };
  54. Store.prototype.set = function (k, v) {
  55. this.data[k] = v;
  56. };
  57. return Store;
  58. }());
  59. function _initStorage(options) {
  60. var opts = options ? clone(options) : {};
  61. var kp = getKeyPrefix(opts, this._defaultConfig);
  62. var store = Store.resolve(kp);
  63. this._dbInfo = opts;
  64. this._dbInfo.serializer = serialiser;
  65. this._dbInfo.keyPrefix = kp;
  66. this._dbInfo.mStore = store;
  67. return Promise.resolve();
  68. }
  69. function clear(callback) {
  70. var _this = this;
  71. var promise = this.ready().then(function () {
  72. _this._dbInfo.mStore.clear();
  73. });
  74. executeCallback(promise, callback);
  75. return promise;
  76. }
  77. function dropInstance(_options, _cb) {
  78. var _a = dropInstanceCommon.apply(this, arguments), promise = _a.promise, callback = _a.callback;
  79. var outPromise = promise.then(function (keyPrefix) {
  80. Store.resolve(keyPrefix).drop();
  81. });
  82. executeCallback(outPromise, callback);
  83. return promise;
  84. }
  85. function getItem(key$, callback) {
  86. var _this = this;
  87. key$ = normaliseKey(key$);
  88. var promise = this.ready().then(function () {
  89. var result = _this._dbInfo.mStore.get(key$);
  90. // Deserialise if the result is not null or undefined
  91. return result == null ? null : _this._dbInfo.serializer.deserialize(result); //tslint:disable-line:triple-equals
  92. });
  93. executeCallback(promise, callback);
  94. return promise;
  95. }
  96. function iterate(iterator, callback) {
  97. var _this = this;
  98. var promise = this.ready().then(function () {
  99. var store = _this._dbInfo.mStore;
  100. var keys = store.keys();
  101. for (var i = 0; i < keys.length; i++) {
  102. var value = store.get(keys[i]);
  103. // If a result was found, parse it from the serialized
  104. // string into a JS object. If result isn't truthy, the
  105. // key is likely undefined and we'll pass it straight
  106. // to the iterator.
  107. if (value) {
  108. value = _this._dbInfo.serializer.deserialize(value);
  109. }
  110. value = iterator(value, keys[i], i + 1);
  111. if (value !== undefined) {
  112. return value;
  113. }
  114. }
  115. });
  116. executeCallback(promise, callback);
  117. return promise;
  118. }
  119. function key(idx, callback) {
  120. var _this = this;
  121. var promise = this.ready().then(function () {
  122. var result;
  123. try {
  124. result = _this._dbInfo.mStore.key(idx);
  125. if (result === undefined) {
  126. result = null;
  127. }
  128. }
  129. catch (_a) {
  130. result = null;
  131. }
  132. return result;
  133. });
  134. executeCallback(promise, callback);
  135. return promise;
  136. }
  137. function keys(callback) {
  138. var _this = this;
  139. var promise = this.ready().then(function () {
  140. return _this._dbInfo.mStore.keys();
  141. });
  142. executeCallback(promise, callback);
  143. return promise;
  144. }
  145. function length(callback) {
  146. var promise = this.keys().then(function (keys$) { return keys$.length; });
  147. executeCallback(promise, callback);
  148. return promise;
  149. }
  150. function removeItem(key$, callback) {
  151. var _this = this;
  152. key$ = normaliseKey(key$);
  153. var promise = this.ready().then(function () {
  154. _this._dbInfo.mStore.rm(key$);
  155. });
  156. executeCallback(promise, callback);
  157. return promise;
  158. }
  159. function setItem(key$, value, callback) {
  160. var _this = this;
  161. key$ = normaliseKey(key$);
  162. var promise = this.ready().then(function () {
  163. if (value === undefined) {
  164. value = null;
  165. }
  166. // Save the original value to pass to the callback.
  167. var originalValue = value;
  168. return new Promise(function (resolve, reject) {
  169. _this._dbInfo.serializer.serialize(value, function (value$, error) {
  170. if (error) {
  171. reject(error);
  172. }
  173. else {
  174. try {
  175. _this._dbInfo.mStore.set(key$, value$);
  176. resolve(originalValue);
  177. }
  178. catch (e) {
  179. reject(e);
  180. }
  181. }
  182. });
  183. });
  184. });
  185. executeCallback(promise, callback);
  186. return promise;
  187. }
  188. var _support = true;
  189. export { _support, _driver, _initStorage, clear, dropInstance, getItem, iterate, key, keys, length, removeItem, setItem };
  190. //# sourceMappingURL=fesm5.js.map