Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

localstorage.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define('localStorageWrapper', ['module', 'exports', '../utils/isLocalStorageValid', '../utils/serializer', '../utils/promise', '../utils/executeCallback', '../utils/normalizeKey', '../utils/getCallback'], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(module, exports, require('../utils/isLocalStorageValid'), require('../utils/serializer'), require('../utils/promise'), require('../utils/executeCallback'), require('../utils/normalizeKey'), require('../utils/getCallback'));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod, mod.exports, global.isLocalStorageValid, global.serializer, global.promise, global.executeCallback, global.normalizeKey, global.getCallback);
  11. global.localStorageWrapper = mod.exports;
  12. }
  13. })(this, function (module, exports, _isLocalStorageValid, _serializer, _promise, _executeCallback, _normalizeKey, _getCallback) {
  14. 'use strict';
  15. Object.defineProperty(exports, "__esModule", {
  16. value: true
  17. });
  18. var _isLocalStorageValid2 = _interopRequireDefault(_isLocalStorageValid);
  19. var _serializer2 = _interopRequireDefault(_serializer);
  20. var _promise2 = _interopRequireDefault(_promise);
  21. var _executeCallback2 = _interopRequireDefault(_executeCallback);
  22. var _normalizeKey2 = _interopRequireDefault(_normalizeKey);
  23. var _getCallback2 = _interopRequireDefault(_getCallback);
  24. function _interopRequireDefault(obj) {
  25. return obj && obj.__esModule ? obj : {
  26. default: obj
  27. };
  28. }
  29. // If IndexedDB isn't available, we'll fall back to localStorage.
  30. // Note that this will have considerable performance and storage
  31. // side-effects (all data will be serialized on save and only data that
  32. // can be converted to a string via `JSON.stringify()` will be saved).
  33. function _getKeyPrefix(options, defaultConfig) {
  34. var keyPrefix = options.name + '/';
  35. if (options.storeName !== defaultConfig.storeName) {
  36. keyPrefix += options.storeName + '/';
  37. }
  38. return keyPrefix;
  39. }
  40. // Check if localStorage throws when saving an item
  41. function checkIfLocalStorageThrows() {
  42. var localStorageTestKey = '_localforage_support_test';
  43. try {
  44. localStorage.setItem(localStorageTestKey, true);
  45. localStorage.removeItem(localStorageTestKey);
  46. return false;
  47. } catch (e) {
  48. return true;
  49. }
  50. }
  51. // Check if localStorage is usable and allows to save an item
  52. // This method checks if localStorage is usable in Safari Private Browsing
  53. // mode, or in any other case where the available quota for localStorage
  54. // is 0 and there wasn't any saved items yet.
  55. function _isLocalStorageUsable() {
  56. return !checkIfLocalStorageThrows() || localStorage.length > 0;
  57. }
  58. // Config the localStorage backend, using options set in the config.
  59. function _initStorage(options) {
  60. var self = this;
  61. var dbInfo = {};
  62. if (options) {
  63. for (var i in options) {
  64. dbInfo[i] = options[i];
  65. }
  66. }
  67. dbInfo.keyPrefix = _getKeyPrefix(options, self._defaultConfig);
  68. if (!_isLocalStorageUsable()) {
  69. return _promise2.default.reject();
  70. }
  71. self._dbInfo = dbInfo;
  72. dbInfo.serializer = _serializer2.default;
  73. return _promise2.default.resolve();
  74. }
  75. // Remove all keys from the datastore, effectively destroying all data in
  76. // the app's key/value store!
  77. function clear(callback) {
  78. var self = this;
  79. var promise = self.ready().then(function () {
  80. var keyPrefix = self._dbInfo.keyPrefix;
  81. for (var i = localStorage.length - 1; i >= 0; i--) {
  82. var key = localStorage.key(i);
  83. if (key.indexOf(keyPrefix) === 0) {
  84. localStorage.removeItem(key);
  85. }
  86. }
  87. });
  88. (0, _executeCallback2.default)(promise, callback);
  89. return promise;
  90. }
  91. // Retrieve an item from the store. Unlike the original async_storage
  92. // library in Gaia, we don't modify return values at all. If a key's value
  93. // is `undefined`, we pass that value to the callback function.
  94. function getItem(key, callback) {
  95. var self = this;
  96. key = (0, _normalizeKey2.default)(key);
  97. var promise = self.ready().then(function () {
  98. var dbInfo = self._dbInfo;
  99. var result = localStorage.getItem(dbInfo.keyPrefix + key);
  100. // If a result was found, parse it from the serialized
  101. // string into a JS object. If result isn't truthy, the key
  102. // is likely undefined and we'll pass it straight to the
  103. // callback.
  104. if (result) {
  105. result = dbInfo.serializer.deserialize(result);
  106. }
  107. return result;
  108. });
  109. (0, _executeCallback2.default)(promise, callback);
  110. return promise;
  111. }
  112. // Iterate over all items in the store.
  113. function iterate(iterator, callback) {
  114. var self = this;
  115. var promise = self.ready().then(function () {
  116. var dbInfo = self._dbInfo;
  117. var keyPrefix = dbInfo.keyPrefix;
  118. var keyPrefixLength = keyPrefix.length;
  119. var length = localStorage.length;
  120. // We use a dedicated iterator instead of the `i` variable below
  121. // so other keys we fetch in localStorage aren't counted in
  122. // the `iterationNumber` argument passed to the `iterate()`
  123. // callback.
  124. //
  125. // See: github.com/mozilla/localForage/pull/435#discussion_r38061530
  126. var iterationNumber = 1;
  127. for (var i = 0; i < length; i++) {
  128. var key = localStorage.key(i);
  129. if (key.indexOf(keyPrefix) !== 0) {
  130. continue;
  131. }
  132. var value = localStorage.getItem(key);
  133. // If a result was found, parse it from the serialized
  134. // string into a JS object. If result isn't truthy, the
  135. // key is likely undefined and we'll pass it straight
  136. // to the iterator.
  137. if (value) {
  138. value = dbInfo.serializer.deserialize(value);
  139. }
  140. value = iterator(value, key.substring(keyPrefixLength), iterationNumber++);
  141. if (value !== void 0) {
  142. return value;
  143. }
  144. }
  145. });
  146. (0, _executeCallback2.default)(promise, callback);
  147. return promise;
  148. }
  149. // Same as localStorage's key() method, except takes a callback.
  150. function key(n, callback) {
  151. var self = this;
  152. var promise = self.ready().then(function () {
  153. var dbInfo = self._dbInfo;
  154. var result;
  155. try {
  156. result = localStorage.key(n);
  157. } catch (error) {
  158. result = null;
  159. }
  160. // Remove the prefix from the key, if a key is found.
  161. if (result) {
  162. result = result.substring(dbInfo.keyPrefix.length);
  163. }
  164. return result;
  165. });
  166. (0, _executeCallback2.default)(promise, callback);
  167. return promise;
  168. }
  169. function keys(callback) {
  170. var self = this;
  171. var promise = self.ready().then(function () {
  172. var dbInfo = self._dbInfo;
  173. var length = localStorage.length;
  174. var keys = [];
  175. for (var i = 0; i < length; i++) {
  176. var itemKey = localStorage.key(i);
  177. if (itemKey.indexOf(dbInfo.keyPrefix) === 0) {
  178. keys.push(itemKey.substring(dbInfo.keyPrefix.length));
  179. }
  180. }
  181. return keys;
  182. });
  183. (0, _executeCallback2.default)(promise, callback);
  184. return promise;
  185. }
  186. // Supply the number of keys in the datastore to the callback function.
  187. function length(callback) {
  188. var self = this;
  189. var promise = self.keys().then(function (keys) {
  190. return keys.length;
  191. });
  192. (0, _executeCallback2.default)(promise, callback);
  193. return promise;
  194. }
  195. // Remove an item from the store, nice and simple.
  196. function removeItem(key, callback) {
  197. var self = this;
  198. key = (0, _normalizeKey2.default)(key);
  199. var promise = self.ready().then(function () {
  200. var dbInfo = self._dbInfo;
  201. localStorage.removeItem(dbInfo.keyPrefix + key);
  202. });
  203. (0, _executeCallback2.default)(promise, callback);
  204. return promise;
  205. }
  206. // Set a key's value and run an optional callback once the value is set.
  207. // Unlike Gaia's implementation, the callback function is passed the value,
  208. // in case you want to operate on that value only after you're sure it
  209. // saved, or something like that.
  210. function setItem(key, value, callback) {
  211. var self = this;
  212. key = (0, _normalizeKey2.default)(key);
  213. var promise = self.ready().then(function () {
  214. // Convert undefined values to null.
  215. // https://github.com/mozilla/localForage/pull/42
  216. if (value === undefined) {
  217. value = null;
  218. }
  219. // Save the original value to pass to the callback.
  220. var originalValue = value;
  221. return new _promise2.default(function (resolve, reject) {
  222. var dbInfo = self._dbInfo;
  223. dbInfo.serializer.serialize(value, function (value, error) {
  224. if (error) {
  225. reject(error);
  226. } else {
  227. try {
  228. localStorage.setItem(dbInfo.keyPrefix + key, value);
  229. resolve(originalValue);
  230. } catch (e) {
  231. // localStorage capacity exceeded.
  232. // TODO: Make this a specific error/event.
  233. if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
  234. reject(e);
  235. }
  236. reject(e);
  237. }
  238. }
  239. });
  240. });
  241. });
  242. (0, _executeCallback2.default)(promise, callback);
  243. return promise;
  244. }
  245. function dropInstance(options, callback) {
  246. callback = _getCallback2.default.apply(this, arguments);
  247. options = typeof options !== 'function' && options || {};
  248. if (!options.name) {
  249. var currentConfig = this.config();
  250. options.name = options.name || currentConfig.name;
  251. options.storeName = options.storeName || currentConfig.storeName;
  252. }
  253. var self = this;
  254. var promise;
  255. if (!options.name) {
  256. promise = _promise2.default.reject('Invalid arguments');
  257. } else {
  258. promise = new _promise2.default(function (resolve) {
  259. if (!options.storeName) {
  260. resolve(options.name + '/');
  261. } else {
  262. resolve(_getKeyPrefix(options, self._defaultConfig));
  263. }
  264. }).then(function (keyPrefix) {
  265. for (var i = localStorage.length - 1; i >= 0; i--) {
  266. var key = localStorage.key(i);
  267. if (key.indexOf(keyPrefix) === 0) {
  268. localStorage.removeItem(key);
  269. }
  270. }
  271. });
  272. }
  273. (0, _executeCallback2.default)(promise, callback);
  274. return promise;
  275. }
  276. var localStorageWrapper = {
  277. _driver: 'localStorageWrapper',
  278. _initStorage: _initStorage,
  279. _support: (0, _isLocalStorageValid2.default)(),
  280. iterate: iterate,
  281. getItem: getItem,
  282. setItem: setItem,
  283. removeItem: removeItem,
  284. clear: clear,
  285. length: length,
  286. key: key,
  287. keys: keys,
  288. dropInstance: dropInstance
  289. };
  290. exports.default = localStorageWrapper;
  291. module.exports = exports['default'];
  292. });