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

localforage.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define('localforage', ['module', 'exports', './drivers/indexeddb', './drivers/websql', './drivers/localstorage', './utils/serializer', './utils/promise', './utils/executeCallback', './utils/executeTwoCallbacks', './utils/includes', './utils/isArray'], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(module, exports, require('./drivers/indexeddb'), require('./drivers/websql'), require('./drivers/localstorage'), require('./utils/serializer'), require('./utils/promise'), require('./utils/executeCallback'), require('./utils/executeTwoCallbacks'), require('./utils/includes'), require('./utils/isArray'));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod, mod.exports, global.indexeddb, global.websql, global.localstorage, global.serializer, global.promise, global.executeCallback, global.executeTwoCallbacks, global.includes, global.isArray);
  11. global.localforage = mod.exports;
  12. }
  13. })(this, function (module, exports, _indexeddb, _websql, _localstorage, _serializer, _promise, _executeCallback, _executeTwoCallbacks, _includes, _isArray) {
  14. 'use strict';
  15. Object.defineProperty(exports, "__esModule", {
  16. value: true
  17. });
  18. var _indexeddb2 = _interopRequireDefault(_indexeddb);
  19. var _websql2 = _interopRequireDefault(_websql);
  20. var _localstorage2 = _interopRequireDefault(_localstorage);
  21. var _serializer2 = _interopRequireDefault(_serializer);
  22. var _promise2 = _interopRequireDefault(_promise);
  23. var _executeCallback2 = _interopRequireDefault(_executeCallback);
  24. var _executeTwoCallbacks2 = _interopRequireDefault(_executeTwoCallbacks);
  25. var _includes2 = _interopRequireDefault(_includes);
  26. var _isArray2 = _interopRequireDefault(_isArray);
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {
  29. default: obj
  30. };
  31. }
  32. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  33. return typeof obj;
  34. } : function (obj) {
  35. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  36. };
  37. function _classCallCheck(instance, Constructor) {
  38. if (!(instance instanceof Constructor)) {
  39. throw new TypeError("Cannot call a class as a function");
  40. }
  41. }
  42. // Drivers are stored here when `defineDriver()` is called.
  43. // They are shared across all instances of localForage.
  44. var DefinedDrivers = {};
  45. var DriverSupport = {};
  46. var DefaultDrivers = {
  47. INDEXEDDB: _indexeddb2.default,
  48. WEBSQL: _websql2.default,
  49. LOCALSTORAGE: _localstorage2.default
  50. };
  51. var DefaultDriverOrder = [DefaultDrivers.INDEXEDDB._driver, DefaultDrivers.WEBSQL._driver, DefaultDrivers.LOCALSTORAGE._driver];
  52. var OptionalDriverMethods = ['dropInstance'];
  53. var LibraryMethods = ['clear', 'getItem', 'iterate', 'key', 'keys', 'length', 'removeItem', 'setItem'].concat(OptionalDriverMethods);
  54. var DefaultConfig = {
  55. description: '',
  56. driver: DefaultDriverOrder.slice(),
  57. name: 'localforage',
  58. // Default DB size is _JUST UNDER_ 5MB, as it's the highest size
  59. // we can use without a prompt.
  60. size: 4980736,
  61. storeName: 'keyvaluepairs',
  62. version: 1.0
  63. };
  64. function callWhenReady(localForageInstance, libraryMethod) {
  65. localForageInstance[libraryMethod] = function () {
  66. var _args = arguments;
  67. return localForageInstance.ready().then(function () {
  68. return localForageInstance[libraryMethod].apply(localForageInstance, _args);
  69. });
  70. };
  71. }
  72. function extend() {
  73. for (var i = 1; i < arguments.length; i++) {
  74. var arg = arguments[i];
  75. if (arg) {
  76. for (var key in arg) {
  77. if (arg.hasOwnProperty(key)) {
  78. if ((0, _isArray2.default)(arg[key])) {
  79. arguments[0][key] = arg[key].slice();
  80. } else {
  81. arguments[0][key] = arg[key];
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return arguments[0];
  88. }
  89. var LocalForage = function () {
  90. function LocalForage(options) {
  91. _classCallCheck(this, LocalForage);
  92. for (var driverTypeKey in DefaultDrivers) {
  93. if (DefaultDrivers.hasOwnProperty(driverTypeKey)) {
  94. var driver = DefaultDrivers[driverTypeKey];
  95. var driverName = driver._driver;
  96. this[driverTypeKey] = driverName;
  97. if (!DefinedDrivers[driverName]) {
  98. // we don't need to wait for the promise,
  99. // since the default drivers can be defined
  100. // in a blocking manner
  101. this.defineDriver(driver);
  102. }
  103. }
  104. }
  105. this._defaultConfig = extend({}, DefaultConfig);
  106. this._config = extend({}, this._defaultConfig, options);
  107. this._driverSet = null;
  108. this._initDriver = null;
  109. this._ready = false;
  110. this._dbInfo = null;
  111. this._wrapLibraryMethodsWithReady();
  112. this.setDriver(this._config.driver).catch(function () {});
  113. }
  114. // Set any config values for localForage; can be called anytime before
  115. // the first API call (e.g. `getItem`, `setItem`).
  116. // We loop through options so we don't overwrite existing config
  117. // values.
  118. LocalForage.prototype.config = function config(options) {
  119. // If the options argument is an object, we use it to set values.
  120. // Otherwise, we return either a specified config value or all
  121. // config values.
  122. if ((typeof options === 'undefined' ? 'undefined' : _typeof(options)) === 'object') {
  123. // If localforage is ready and fully initialized, we can't set
  124. // any new configuration values. Instead, we return an error.
  125. if (this._ready) {
  126. return new Error("Can't call config() after localforage " + 'has been used.');
  127. }
  128. for (var i in options) {
  129. if (i === 'storeName') {
  130. options[i] = options[i].replace(/\W/g, '_');
  131. }
  132. if (i === 'version' && typeof options[i] !== 'number') {
  133. return new Error('Database version must be a number.');
  134. }
  135. this._config[i] = options[i];
  136. }
  137. // after all config options are set and
  138. // the driver option is used, try setting it
  139. if ('driver' in options && options.driver) {
  140. return this.setDriver(this._config.driver);
  141. }
  142. return true;
  143. } else if (typeof options === 'string') {
  144. return this._config[options];
  145. } else {
  146. return this._config;
  147. }
  148. };
  149. LocalForage.prototype.defineDriver = function defineDriver(driverObject, callback, errorCallback) {
  150. var promise = new _promise2.default(function (resolve, reject) {
  151. try {
  152. var driverName = driverObject._driver;
  153. var complianceError = new Error('Custom driver not compliant; see ' + 'https://mozilla.github.io/localForage/#definedriver');
  154. // A driver name should be defined and not overlap with the
  155. // library-defined, default drivers.
  156. if (!driverObject._driver) {
  157. reject(complianceError);
  158. return;
  159. }
  160. var driverMethods = LibraryMethods.concat('_initStorage');
  161. for (var i = 0, len = driverMethods.length; i < len; i++) {
  162. var driverMethodName = driverMethods[i];
  163. // when the property is there,
  164. // it should be a method even when optional
  165. var isRequired = !(0, _includes2.default)(OptionalDriverMethods, driverMethodName);
  166. if ((isRequired || driverObject[driverMethodName]) && typeof driverObject[driverMethodName] !== 'function') {
  167. reject(complianceError);
  168. return;
  169. }
  170. }
  171. var configureMissingMethods = function configureMissingMethods() {
  172. var methodNotImplementedFactory = function methodNotImplementedFactory(methodName) {
  173. return function () {
  174. var error = new Error('Method ' + methodName + ' is not implemented by the current driver');
  175. var promise = _promise2.default.reject(error);
  176. (0, _executeCallback2.default)(promise, arguments[arguments.length - 1]);
  177. return promise;
  178. };
  179. };
  180. for (var _i = 0, _len = OptionalDriverMethods.length; _i < _len; _i++) {
  181. var optionalDriverMethod = OptionalDriverMethods[_i];
  182. if (!driverObject[optionalDriverMethod]) {
  183. driverObject[optionalDriverMethod] = methodNotImplementedFactory(optionalDriverMethod);
  184. }
  185. }
  186. };
  187. configureMissingMethods();
  188. var setDriverSupport = function setDriverSupport(support) {
  189. if (DefinedDrivers[driverName]) {
  190. console.info('Redefining LocalForage driver: ' + driverName);
  191. }
  192. DefinedDrivers[driverName] = driverObject;
  193. DriverSupport[driverName] = support;
  194. // don't use a then, so that we can define
  195. // drivers that have simple _support methods
  196. // in a blocking manner
  197. resolve();
  198. };
  199. if ('_support' in driverObject) {
  200. if (driverObject._support && typeof driverObject._support === 'function') {
  201. driverObject._support().then(setDriverSupport, reject);
  202. } else {
  203. setDriverSupport(!!driverObject._support);
  204. }
  205. } else {
  206. setDriverSupport(true);
  207. }
  208. } catch (e) {
  209. reject(e);
  210. }
  211. });
  212. (0, _executeTwoCallbacks2.default)(promise, callback, errorCallback);
  213. return promise;
  214. };
  215. LocalForage.prototype.driver = function driver() {
  216. return this._driver || null;
  217. };
  218. LocalForage.prototype.getDriver = function getDriver(driverName, callback, errorCallback) {
  219. var getDriverPromise = DefinedDrivers[driverName] ? _promise2.default.resolve(DefinedDrivers[driverName]) : _promise2.default.reject(new Error('Driver not found.'));
  220. (0, _executeTwoCallbacks2.default)(getDriverPromise, callback, errorCallback);
  221. return getDriverPromise;
  222. };
  223. LocalForage.prototype.getSerializer = function getSerializer(callback) {
  224. var serializerPromise = _promise2.default.resolve(_serializer2.default);
  225. (0, _executeTwoCallbacks2.default)(serializerPromise, callback);
  226. return serializerPromise;
  227. };
  228. LocalForage.prototype.ready = function ready(callback) {
  229. var self = this;
  230. var promise = self._driverSet.then(function () {
  231. if (self._ready === null) {
  232. self._ready = self._initDriver();
  233. }
  234. return self._ready;
  235. });
  236. (0, _executeTwoCallbacks2.default)(promise, callback, callback);
  237. return promise;
  238. };
  239. LocalForage.prototype.setDriver = function setDriver(drivers, callback, errorCallback) {
  240. var self = this;
  241. if (!(0, _isArray2.default)(drivers)) {
  242. drivers = [drivers];
  243. }
  244. var supportedDrivers = this._getSupportedDrivers(drivers);
  245. function setDriverToConfig() {
  246. self._config.driver = self.driver();
  247. }
  248. function extendSelfWithDriver(driver) {
  249. self._extend(driver);
  250. setDriverToConfig();
  251. self._ready = self._initStorage(self._config);
  252. return self._ready;
  253. }
  254. function initDriver(supportedDrivers) {
  255. return function () {
  256. var currentDriverIndex = 0;
  257. function driverPromiseLoop() {
  258. while (currentDriverIndex < supportedDrivers.length) {
  259. var driverName = supportedDrivers[currentDriverIndex];
  260. currentDriverIndex++;
  261. self._dbInfo = null;
  262. self._ready = null;
  263. return self.getDriver(driverName).then(extendSelfWithDriver).catch(driverPromiseLoop);
  264. }
  265. setDriverToConfig();
  266. var error = new Error('No available storage method found.');
  267. self._driverSet = _promise2.default.reject(error);
  268. return self._driverSet;
  269. }
  270. return driverPromiseLoop();
  271. };
  272. }
  273. // There might be a driver initialization in progress
  274. // so wait for it to finish in order to avoid a possible
  275. // race condition to set _dbInfo
  276. var oldDriverSetDone = this._driverSet !== null ? this._driverSet.catch(function () {
  277. return _promise2.default.resolve();
  278. }) : _promise2.default.resolve();
  279. this._driverSet = oldDriverSetDone.then(function () {
  280. var driverName = supportedDrivers[0];
  281. self._dbInfo = null;
  282. self._ready = null;
  283. return self.getDriver(driverName).then(function (driver) {
  284. self._driver = driver._driver;
  285. setDriverToConfig();
  286. self._wrapLibraryMethodsWithReady();
  287. self._initDriver = initDriver(supportedDrivers);
  288. });
  289. }).catch(function () {
  290. setDriverToConfig();
  291. var error = new Error('No available storage method found.');
  292. self._driverSet = _promise2.default.reject(error);
  293. return self._driverSet;
  294. });
  295. (0, _executeTwoCallbacks2.default)(this._driverSet, callback, errorCallback);
  296. return this._driverSet;
  297. };
  298. LocalForage.prototype.supports = function supports(driverName) {
  299. return !!DriverSupport[driverName];
  300. };
  301. LocalForage.prototype._extend = function _extend(libraryMethodsAndProperties) {
  302. extend(this, libraryMethodsAndProperties);
  303. };
  304. LocalForage.prototype._getSupportedDrivers = function _getSupportedDrivers(drivers) {
  305. var supportedDrivers = [];
  306. for (var i = 0, len = drivers.length; i < len; i++) {
  307. var driverName = drivers[i];
  308. if (this.supports(driverName)) {
  309. supportedDrivers.push(driverName);
  310. }
  311. }
  312. return supportedDrivers;
  313. };
  314. LocalForage.prototype._wrapLibraryMethodsWithReady = function _wrapLibraryMethodsWithReady() {
  315. // Add a stub for each driver API method that delays the call to the
  316. // corresponding driver method until localForage is ready. These stubs
  317. // will be replaced by the driver methods as soon as the driver is
  318. // loaded, so there is no performance impact.
  319. for (var i = 0, len = LibraryMethods.length; i < len; i++) {
  320. callWhenReady(this, LibraryMethods[i]);
  321. }
  322. };
  323. LocalForage.prototype.createInstance = function createInstance(options) {
  324. return new LocalForage(options);
  325. };
  326. return LocalForage;
  327. }();
  328. exports.default = new LocalForage();
  329. module.exports = exports['default'];
  330. });