LocalDatastoreController.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  3. var _Storage = _interopRequireDefault(require("./Storage"));
  4. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  5. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  6. if (it) return (it = it.call(o)).next.bind(it);
  7. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  8. if (it) o = it;
  9. var i = 0;
  10. return function () {
  11. if (i >= o.length) return {
  12. done: true
  13. };
  14. return {
  15. done: false,
  16. value: o[i++]
  17. };
  18. };
  19. }
  20. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  21. }
  22. function _unsupportedIterableToArray(o, minLen) {
  23. if (!o) return;
  24. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  25. var n = Object.prototype.toString.call(o).slice(8, -1);
  26. if (n === "Object" && o.constructor) n = o.constructor.name;
  27. if (n === "Map" || n === "Set") return Array.from(o);
  28. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  29. }
  30. function _arrayLikeToArray(arr, len) {
  31. if (len == null || len > arr.length) len = arr.length;
  32. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  33. arr2[i] = arr[i];
  34. }
  35. return arr2;
  36. }
  37. function _regeneratorRuntime() {
  38. "use strict";
  39. _regeneratorRuntime = function () {
  40. return exports;
  41. };
  42. var exports = {},
  43. Op = Object.prototype,
  44. hasOwn = Op.hasOwnProperty,
  45. $Symbol = "function" == typeof Symbol ? Symbol : {},
  46. iteratorSymbol = $Symbol.iterator || "@@iterator",
  47. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  48. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  49. function define(obj, key, value) {
  50. return Object.defineProperty(obj, key, {
  51. value: value,
  52. enumerable: !0,
  53. configurable: !0,
  54. writable: !0
  55. }), obj[key];
  56. }
  57. try {
  58. define({}, "");
  59. } catch (err) {
  60. define = function (obj, key, value) {
  61. return obj[key] = value;
  62. };
  63. }
  64. function wrap(innerFn, outerFn, self, tryLocsList) {
  65. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  66. generator = Object.create(protoGenerator.prototype),
  67. context = new Context(tryLocsList || []);
  68. return generator._invoke = function (innerFn, self, context) {
  69. var state = "suspendedStart";
  70. return function (method, arg) {
  71. if ("executing" === state) throw new Error("Generator is already running");
  72. if ("completed" === state) {
  73. if ("throw" === method) throw arg;
  74. return doneResult();
  75. }
  76. for (context.method = method, context.arg = arg;;) {
  77. var delegate = context.delegate;
  78. if (delegate) {
  79. var delegateResult = maybeInvokeDelegate(delegate, context);
  80. if (delegateResult) {
  81. if (delegateResult === ContinueSentinel) continue;
  82. return delegateResult;
  83. }
  84. }
  85. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  86. if ("suspendedStart" === state) throw state = "completed", context.arg;
  87. context.dispatchException(context.arg);
  88. } else "return" === context.method && context.abrupt("return", context.arg);
  89. state = "executing";
  90. var record = tryCatch(innerFn, self, context);
  91. if ("normal" === record.type) {
  92. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  93. return {
  94. value: record.arg,
  95. done: context.done
  96. };
  97. }
  98. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  99. }
  100. };
  101. }(innerFn, self, context), generator;
  102. }
  103. function tryCatch(fn, obj, arg) {
  104. try {
  105. return {
  106. type: "normal",
  107. arg: fn.call(obj, arg)
  108. };
  109. } catch (err) {
  110. return {
  111. type: "throw",
  112. arg: err
  113. };
  114. }
  115. }
  116. exports.wrap = wrap;
  117. var ContinueSentinel = {};
  118. function Generator() {}
  119. function GeneratorFunction() {}
  120. function GeneratorFunctionPrototype() {}
  121. var IteratorPrototype = {};
  122. define(IteratorPrototype, iteratorSymbol, function () {
  123. return this;
  124. });
  125. var getProto = Object.getPrototypeOf,
  126. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  127. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  128. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  129. function defineIteratorMethods(prototype) {
  130. ["next", "throw", "return"].forEach(function (method) {
  131. define(prototype, method, function (arg) {
  132. return this._invoke(method, arg);
  133. });
  134. });
  135. }
  136. function AsyncIterator(generator, PromiseImpl) {
  137. function invoke(method, arg, resolve, reject) {
  138. var record = tryCatch(generator[method], generator, arg);
  139. if ("throw" !== record.type) {
  140. var result = record.arg,
  141. value = result.value;
  142. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  143. invoke("next", value, resolve, reject);
  144. }, function (err) {
  145. invoke("throw", err, resolve, reject);
  146. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  147. result.value = unwrapped, resolve(result);
  148. }, function (error) {
  149. return invoke("throw", error, resolve, reject);
  150. });
  151. }
  152. reject(record.arg);
  153. }
  154. var previousPromise;
  155. this._invoke = function (method, arg) {
  156. function callInvokeWithMethodAndArg() {
  157. return new PromiseImpl(function (resolve, reject) {
  158. invoke(method, arg, resolve, reject);
  159. });
  160. }
  161. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  162. };
  163. }
  164. function maybeInvokeDelegate(delegate, context) {
  165. var method = delegate.iterator[context.method];
  166. if (undefined === method) {
  167. if (context.delegate = null, "throw" === context.method) {
  168. if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
  169. context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
  170. }
  171. return ContinueSentinel;
  172. }
  173. var record = tryCatch(method, delegate.iterator, context.arg);
  174. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  175. var info = record.arg;
  176. return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel);
  177. }
  178. function pushTryEntry(locs) {
  179. var entry = {
  180. tryLoc: locs[0]
  181. };
  182. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  183. }
  184. function resetTryEntry(entry) {
  185. var record = entry.completion || {};
  186. record.type = "normal", delete record.arg, entry.completion = record;
  187. }
  188. function Context(tryLocsList) {
  189. this.tryEntries = [{
  190. tryLoc: "root"
  191. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  192. }
  193. function values(iterable) {
  194. if (iterable) {
  195. var iteratorMethod = iterable[iteratorSymbol];
  196. if (iteratorMethod) return iteratorMethod.call(iterable);
  197. if ("function" == typeof iterable.next) return iterable;
  198. if (!isNaN(iterable.length)) {
  199. var i = -1,
  200. next = function next() {
  201. for (; ++i < iterable.length;) {
  202. if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  203. }
  204. return next.value = undefined, next.done = !0, next;
  205. };
  206. return next.next = next;
  207. }
  208. }
  209. return {
  210. next: doneResult
  211. };
  212. }
  213. function doneResult() {
  214. return {
  215. value: undefined,
  216. done: !0
  217. };
  218. }
  219. return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  220. var ctor = "function" == typeof genFun && genFun.constructor;
  221. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  222. }, exports.mark = function (genFun) {
  223. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  224. }, exports.awrap = function (arg) {
  225. return {
  226. __await: arg
  227. };
  228. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  229. return this;
  230. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  231. void 0 === PromiseImpl && (PromiseImpl = Promise);
  232. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  233. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  234. return result.done ? result.value : iter.next();
  235. });
  236. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  237. return this;
  238. }), define(Gp, "toString", function () {
  239. return "[object Generator]";
  240. }), exports.keys = function (object) {
  241. var keys = [];
  242. for (var key in object) {
  243. keys.push(key);
  244. }
  245. return keys.reverse(), function next() {
  246. for (; keys.length;) {
  247. var key = keys.pop();
  248. if (key in object) return next.value = key, next.done = !1, next;
  249. }
  250. return next.done = !0, next;
  251. };
  252. }, exports.values = values, Context.prototype = {
  253. constructor: Context,
  254. reset: function (skipTempReset) {
  255. if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) {
  256. "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  257. }
  258. },
  259. stop: function () {
  260. this.done = !0;
  261. var rootRecord = this.tryEntries[0].completion;
  262. if ("throw" === rootRecord.type) throw rootRecord.arg;
  263. return this.rval;
  264. },
  265. dispatchException: function (exception) {
  266. if (this.done) throw exception;
  267. var context = this;
  268. function handle(loc, caught) {
  269. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  270. }
  271. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  272. var entry = this.tryEntries[i],
  273. record = entry.completion;
  274. if ("root" === entry.tryLoc) return handle("end");
  275. if (entry.tryLoc <= this.prev) {
  276. var hasCatch = hasOwn.call(entry, "catchLoc"),
  277. hasFinally = hasOwn.call(entry, "finallyLoc");
  278. if (hasCatch && hasFinally) {
  279. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  280. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  281. } else if (hasCatch) {
  282. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  283. } else {
  284. if (!hasFinally) throw new Error("try statement without catch or finally");
  285. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  286. }
  287. }
  288. }
  289. },
  290. abrupt: function (type, arg) {
  291. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  292. var entry = this.tryEntries[i];
  293. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  294. var finallyEntry = entry;
  295. break;
  296. }
  297. }
  298. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  299. var record = finallyEntry ? finallyEntry.completion : {};
  300. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  301. },
  302. complete: function (record, afterLoc) {
  303. if ("throw" === record.type) throw record.arg;
  304. return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel;
  305. },
  306. finish: function (finallyLoc) {
  307. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  308. var entry = this.tryEntries[i];
  309. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  310. }
  311. },
  312. catch: function (tryLoc) {
  313. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  314. var entry = this.tryEntries[i];
  315. if (entry.tryLoc === tryLoc) {
  316. var record = entry.completion;
  317. if ("throw" === record.type) {
  318. var thrown = record.arg;
  319. resetTryEntry(entry);
  320. }
  321. return thrown;
  322. }
  323. }
  324. throw new Error("illegal catch attempt");
  325. },
  326. delegateYield: function (iterable, resultName, nextLoc) {
  327. return this.delegate = {
  328. iterator: values(iterable),
  329. resultName: resultName,
  330. nextLoc: nextLoc
  331. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  332. }
  333. }, exports;
  334. }
  335. var LocalDatastoreController = {
  336. fromPinWithName: function (name) {
  337. var values, objects;
  338. return _regeneratorRuntime().async(function (_context) {
  339. while (1) {
  340. switch (_context.prev = _context.next) {
  341. case 0:
  342. _context.next = 2;
  343. return _regeneratorRuntime().awrap(_Storage.default.getItemAsync(name));
  344. case 2:
  345. values = _context.sent;
  346. if (values) {
  347. _context.next = 5;
  348. break;
  349. }
  350. return _context.abrupt("return", []);
  351. case 5:
  352. objects = JSON.parse(values);
  353. return _context.abrupt("return", objects);
  354. case 7:
  355. case "end":
  356. return _context.stop();
  357. }
  358. }
  359. }, null, null, null, Promise);
  360. },
  361. pinWithName: function (name, value) {
  362. var values = JSON.stringify(value);
  363. return _Storage.default.setItemAsync(name, values);
  364. },
  365. unPinWithName: function (name) {
  366. return _Storage.default.removeItemAsync(name);
  367. },
  368. getAllContents: function () {
  369. var keys;
  370. return _regeneratorRuntime().async(function (_context3) {
  371. while (1) {
  372. switch (_context3.prev = _context3.next) {
  373. case 0:
  374. _context3.next = 2;
  375. return _regeneratorRuntime().awrap(_Storage.default.getAllKeysAsync());
  376. case 2:
  377. keys = _context3.sent;
  378. return _context3.abrupt("return", keys.reduce(function (previousPromise, key) {
  379. var LDS, value;
  380. return _regeneratorRuntime().async(function (_context2) {
  381. while (1) {
  382. switch (_context2.prev = _context2.next) {
  383. case 0:
  384. _context2.next = 2;
  385. return _regeneratorRuntime().awrap(previousPromise);
  386. case 2:
  387. LDS = _context2.sent;
  388. if (!(0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  389. _context2.next = 8;
  390. break;
  391. }
  392. _context2.next = 6;
  393. return _regeneratorRuntime().awrap(_Storage.default.getItemAsync(key));
  394. case 6:
  395. value = _context2.sent;
  396. try {
  397. LDS[key] = JSON.parse(value);
  398. } catch (error) {
  399. console.error('Error getAllContents: ', error);
  400. }
  401. case 8:
  402. return _context2.abrupt("return", LDS);
  403. case 9:
  404. case "end":
  405. return _context2.stop();
  406. }
  407. }
  408. }, null, null, null, Promise);
  409. }, Promise.resolve({})));
  410. case 4:
  411. case "end":
  412. return _context3.stop();
  413. }
  414. }
  415. }, null, null, null, Promise);
  416. },
  417. getRawStorage: function () {
  418. var keys;
  419. return _regeneratorRuntime().async(function (_context5) {
  420. while (1) {
  421. switch (_context5.prev = _context5.next) {
  422. case 0:
  423. _context5.next = 2;
  424. return _regeneratorRuntime().awrap(_Storage.default.getAllKeysAsync());
  425. case 2:
  426. keys = _context5.sent;
  427. return _context5.abrupt("return", keys.reduce(function (previousPromise, key) {
  428. var LDS, value;
  429. return _regeneratorRuntime().async(function (_context4) {
  430. while (1) {
  431. switch (_context4.prev = _context4.next) {
  432. case 0:
  433. _context4.next = 2;
  434. return _regeneratorRuntime().awrap(previousPromise);
  435. case 2:
  436. LDS = _context4.sent;
  437. _context4.next = 5;
  438. return _regeneratorRuntime().awrap(_Storage.default.getItemAsync(key));
  439. case 5:
  440. value = _context4.sent;
  441. LDS[key] = value;
  442. return _context4.abrupt("return", LDS);
  443. case 8:
  444. case "end":
  445. return _context4.stop();
  446. }
  447. }
  448. }, null, null, null, Promise);
  449. }, Promise.resolve({})));
  450. case 4:
  451. case "end":
  452. return _context5.stop();
  453. }
  454. }
  455. }, null, null, null, Promise);
  456. },
  457. clear: function () {
  458. var keys, toRemove, _iterator, _step, key, promises;
  459. return _regeneratorRuntime().async(function (_context6) {
  460. while (1) {
  461. switch (_context6.prev = _context6.next) {
  462. case 0:
  463. _context6.next = 2;
  464. return _regeneratorRuntime().awrap(_Storage.default.getAllKeysAsync());
  465. case 2:
  466. keys = _context6.sent;
  467. toRemove = [];
  468. for (_iterator = _createForOfIteratorHelperLoose(keys); !(_step = _iterator()).done;) {
  469. key = _step.value;
  470. if ((0, _LocalDatastoreUtils.isLocalDatastoreKey)(key)) {
  471. toRemove.push(key);
  472. }
  473. }
  474. promises = toRemove.map(this.unPinWithName);
  475. return _context6.abrupt("return", Promise.all(promises));
  476. case 7:
  477. case "end":
  478. return _context6.stop();
  479. }
  480. }
  481. }, null, this, null, Promise);
  482. }
  483. };
  484. module.exports = LocalDatastoreController;