LocalDatastoreController.react-native.js 19 KB

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