EventuallyQueue.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  3. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  4. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  5. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  6. var _Storage = _interopRequireDefault(require("./Storage"));
  7. function _regeneratorRuntime() {
  8. "use strict";
  9. _regeneratorRuntime = function () {
  10. return exports;
  11. };
  12. var exports = {},
  13. Op = Object.prototype,
  14. hasOwn = Op.hasOwnProperty,
  15. $Symbol = "function" == typeof Symbol ? Symbol : {},
  16. iteratorSymbol = $Symbol.iterator || "@@iterator",
  17. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  18. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  19. function define(obj, key, value) {
  20. return Object.defineProperty(obj, key, {
  21. value: value,
  22. enumerable: !0,
  23. configurable: !0,
  24. writable: !0
  25. }), obj[key];
  26. }
  27. try {
  28. define({}, "");
  29. } catch (err) {
  30. define = function (obj, key, value) {
  31. return obj[key] = value;
  32. };
  33. }
  34. function wrap(innerFn, outerFn, self, tryLocsList) {
  35. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  36. generator = Object.create(protoGenerator.prototype),
  37. context = new Context(tryLocsList || []);
  38. return generator._invoke = function (innerFn, self, context) {
  39. var state = "suspendedStart";
  40. return function (method, arg) {
  41. if ("executing" === state) throw new Error("Generator is already running");
  42. if ("completed" === state) {
  43. if ("throw" === method) throw arg;
  44. return doneResult();
  45. }
  46. for (context.method = method, context.arg = arg;;) {
  47. var delegate = context.delegate;
  48. if (delegate) {
  49. var delegateResult = maybeInvokeDelegate(delegate, context);
  50. if (delegateResult) {
  51. if (delegateResult === ContinueSentinel) continue;
  52. return delegateResult;
  53. }
  54. }
  55. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  56. if ("suspendedStart" === state) throw state = "completed", context.arg;
  57. context.dispatchException(context.arg);
  58. } else "return" === context.method && context.abrupt("return", context.arg);
  59. state = "executing";
  60. var record = tryCatch(innerFn, self, context);
  61. if ("normal" === record.type) {
  62. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  63. return {
  64. value: record.arg,
  65. done: context.done
  66. };
  67. }
  68. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  69. }
  70. };
  71. }(innerFn, self, context), generator;
  72. }
  73. function tryCatch(fn, obj, arg) {
  74. try {
  75. return {
  76. type: "normal",
  77. arg: fn.call(obj, arg)
  78. };
  79. } catch (err) {
  80. return {
  81. type: "throw",
  82. arg: err
  83. };
  84. }
  85. }
  86. exports.wrap = wrap;
  87. var ContinueSentinel = {};
  88. function Generator() {}
  89. function GeneratorFunction() {}
  90. function GeneratorFunctionPrototype() {}
  91. var IteratorPrototype = {};
  92. define(IteratorPrototype, iteratorSymbol, function () {
  93. return this;
  94. });
  95. var getProto = Object.getPrototypeOf,
  96. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  97. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  98. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  99. function defineIteratorMethods(prototype) {
  100. ["next", "throw", "return"].forEach(function (method) {
  101. define(prototype, method, function (arg) {
  102. return this._invoke(method, arg);
  103. });
  104. });
  105. }
  106. function AsyncIterator(generator, PromiseImpl) {
  107. function invoke(method, arg, resolve, reject) {
  108. var record = tryCatch(generator[method], generator, arg);
  109. if ("throw" !== record.type) {
  110. var result = record.arg,
  111. value = result.value;
  112. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  113. invoke("next", value, resolve, reject);
  114. }, function (err) {
  115. invoke("throw", err, resolve, reject);
  116. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  117. result.value = unwrapped, resolve(result);
  118. }, function (error) {
  119. return invoke("throw", error, resolve, reject);
  120. });
  121. }
  122. reject(record.arg);
  123. }
  124. var previousPromise;
  125. this._invoke = function (method, arg) {
  126. function callInvokeWithMethodAndArg() {
  127. return new PromiseImpl(function (resolve, reject) {
  128. invoke(method, arg, resolve, reject);
  129. });
  130. }
  131. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  132. };
  133. }
  134. function maybeInvokeDelegate(delegate, context) {
  135. var method = delegate.iterator[context.method];
  136. if (undefined === method) {
  137. if (context.delegate = null, "throw" === context.method) {
  138. if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
  139. context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
  140. }
  141. return ContinueSentinel;
  142. }
  143. var record = tryCatch(method, delegate.iterator, context.arg);
  144. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  145. var info = record.arg;
  146. 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);
  147. }
  148. function pushTryEntry(locs) {
  149. var entry = {
  150. tryLoc: locs[0]
  151. };
  152. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  153. }
  154. function resetTryEntry(entry) {
  155. var record = entry.completion || {};
  156. record.type = "normal", delete record.arg, entry.completion = record;
  157. }
  158. function Context(tryLocsList) {
  159. this.tryEntries = [{
  160. tryLoc: "root"
  161. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  162. }
  163. function values(iterable) {
  164. if (iterable) {
  165. var iteratorMethod = iterable[iteratorSymbol];
  166. if (iteratorMethod) return iteratorMethod.call(iterable);
  167. if ("function" == typeof iterable.next) return iterable;
  168. if (!isNaN(iterable.length)) {
  169. var i = -1,
  170. next = function next() {
  171. for (; ++i < iterable.length;) {
  172. if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  173. }
  174. return next.value = undefined, next.done = !0, next;
  175. };
  176. return next.next = next;
  177. }
  178. }
  179. return {
  180. next: doneResult
  181. };
  182. }
  183. function doneResult() {
  184. return {
  185. value: undefined,
  186. done: !0
  187. };
  188. }
  189. return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  190. var ctor = "function" == typeof genFun && genFun.constructor;
  191. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  192. }, exports.mark = function (genFun) {
  193. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  194. }, exports.awrap = function (arg) {
  195. return {
  196. __await: arg
  197. };
  198. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  199. return this;
  200. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  201. void 0 === PromiseImpl && (PromiseImpl = Promise);
  202. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  203. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  204. return result.done ? result.value : iter.next();
  205. });
  206. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  207. return this;
  208. }), define(Gp, "toString", function () {
  209. return "[object Generator]";
  210. }), exports.keys = function (object) {
  211. var keys = [];
  212. for (var key in object) {
  213. keys.push(key);
  214. }
  215. return keys.reverse(), function next() {
  216. for (; keys.length;) {
  217. var key = keys.pop();
  218. if (key in object) return next.value = key, next.done = !1, next;
  219. }
  220. return next.done = !0, next;
  221. };
  222. }, exports.values = values, Context.prototype = {
  223. constructor: Context,
  224. reset: function (skipTempReset) {
  225. 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) {
  226. "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  227. }
  228. },
  229. stop: function () {
  230. this.done = !0;
  231. var rootRecord = this.tryEntries[0].completion;
  232. if ("throw" === rootRecord.type) throw rootRecord.arg;
  233. return this.rval;
  234. },
  235. dispatchException: function (exception) {
  236. if (this.done) throw exception;
  237. var context = this;
  238. function handle(loc, caught) {
  239. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  240. }
  241. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  242. var entry = this.tryEntries[i],
  243. record = entry.completion;
  244. if ("root" === entry.tryLoc) return handle("end");
  245. if (entry.tryLoc <= this.prev) {
  246. var hasCatch = hasOwn.call(entry, "catchLoc"),
  247. hasFinally = hasOwn.call(entry, "finallyLoc");
  248. if (hasCatch && hasFinally) {
  249. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  250. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  251. } else if (hasCatch) {
  252. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  253. } else {
  254. if (!hasFinally) throw new Error("try statement without catch or finally");
  255. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  256. }
  257. }
  258. }
  259. },
  260. abrupt: function (type, arg) {
  261. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  262. var entry = this.tryEntries[i];
  263. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  264. var finallyEntry = entry;
  265. break;
  266. }
  267. }
  268. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  269. var record = finallyEntry ? finallyEntry.completion : {};
  270. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  271. },
  272. complete: function (record, afterLoc) {
  273. if ("throw" === record.type) throw record.arg;
  274. 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;
  275. },
  276. finish: function (finallyLoc) {
  277. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  278. var entry = this.tryEntries[i];
  279. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  280. }
  281. },
  282. catch: function (tryLoc) {
  283. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  284. var entry = this.tryEntries[i];
  285. if (entry.tryLoc === tryLoc) {
  286. var record = entry.completion;
  287. if ("throw" === record.type) {
  288. var thrown = record.arg;
  289. resetTryEntry(entry);
  290. }
  291. return thrown;
  292. }
  293. }
  294. throw new Error("illegal catch attempt");
  295. },
  296. delegateYield: function (iterable, resultName, nextLoc) {
  297. return this.delegate = {
  298. iterator: values(iterable),
  299. resultName: resultName,
  300. nextLoc: nextLoc
  301. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  302. }
  303. }, exports;
  304. }
  305. var QUEUE_KEY = 'Parse/Eventually/Queue';
  306. var queueCache = [];
  307. var dirtyCache = true;
  308. var polling = undefined;
  309. var EventuallyQueue = {
  310. save: function (object) {
  311. var serverOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  312. return this.enqueue('save', object, serverOptions);
  313. },
  314. destroy: function (object) {
  315. var serverOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  316. return this.enqueue('destroy', object, serverOptions);
  317. },
  318. generateQueueId: function (action, object) {
  319. object._getId();
  320. var className = object.className,
  321. id = object.id,
  322. _localId = object._localId;
  323. var uniqueId = object.get('hash') || _localId;
  324. return [action, className, id, uniqueId].join('_');
  325. },
  326. enqueue: function (action, object, serverOptions) {
  327. var queueData, queueId, index, prop;
  328. return _regeneratorRuntime().async(function (_context) {
  329. while (1) {
  330. switch (_context.prev = _context.next) {
  331. case 0:
  332. _context.next = 2;
  333. return _regeneratorRuntime().awrap(this.getQueue());
  334. case 2:
  335. queueData = _context.sent;
  336. queueId = this.generateQueueId(action, object);
  337. index = this.queueItemExists(queueData, queueId);
  338. if (index > -1) {
  339. for (prop in queueData[index].object) {
  340. if (typeof object.get(prop) === 'undefined') {
  341. object.set(prop, queueData[index].object[prop]);
  342. }
  343. }
  344. } else {
  345. index = queueData.length;
  346. }
  347. queueData[index] = {
  348. queueId: queueId,
  349. action: action,
  350. object: object.toJSON(),
  351. serverOptions: serverOptions,
  352. id: object.id,
  353. className: object.className,
  354. hash: object.get('hash'),
  355. createdAt: new Date()
  356. };
  357. return _context.abrupt("return", this.setQueue(queueData));
  358. case 8:
  359. case "end":
  360. return _context.stop();
  361. }
  362. }
  363. }, null, this, null, Promise);
  364. },
  365. store: function (data) {
  366. return _Storage.default.setItemAsync(QUEUE_KEY, JSON.stringify(data));
  367. },
  368. load: function () {
  369. return _Storage.default.getItemAsync(QUEUE_KEY);
  370. },
  371. getQueue: function () {
  372. return _regeneratorRuntime().async(function (_context2) {
  373. while (1) {
  374. switch (_context2.prev = _context2.next) {
  375. case 0:
  376. if (!dirtyCache) {
  377. _context2.next = 10;
  378. break;
  379. }
  380. _context2.t0 = JSON;
  381. _context2.next = 4;
  382. return _regeneratorRuntime().awrap(this.load());
  383. case 4:
  384. _context2.t1 = _context2.sent;
  385. if (_context2.t1) {
  386. _context2.next = 7;
  387. break;
  388. }
  389. _context2.t1 = '[]';
  390. case 7:
  391. _context2.t2 = _context2.t1;
  392. queueCache = _context2.t0.parse.call(_context2.t0, _context2.t2);
  393. dirtyCache = false;
  394. case 10:
  395. return _context2.abrupt("return", queueCache);
  396. case 11:
  397. case "end":
  398. return _context2.stop();
  399. }
  400. }
  401. }, null, this, null, Promise);
  402. },
  403. setQueue: function (queue) {
  404. queueCache = queue;
  405. return this.store(queueCache);
  406. },
  407. remove: function (queueId) {
  408. var queueData, index;
  409. return _regeneratorRuntime().async(function (_context3) {
  410. while (1) {
  411. switch (_context3.prev = _context3.next) {
  412. case 0:
  413. _context3.next = 2;
  414. return _regeneratorRuntime().awrap(this.getQueue());
  415. case 2:
  416. queueData = _context3.sent;
  417. index = this.queueItemExists(queueData, queueId);
  418. if (!(index > -1)) {
  419. _context3.next = 8;
  420. break;
  421. }
  422. queueData.splice(index, 1);
  423. _context3.next = 8;
  424. return _regeneratorRuntime().awrap(this.setQueue(queueData));
  425. case 8:
  426. case "end":
  427. return _context3.stop();
  428. }
  429. }
  430. }, null, this, null, Promise);
  431. },
  432. clear: function () {
  433. queueCache = [];
  434. return this.store([]);
  435. },
  436. queueItemExists: function (queue, queueId) {
  437. return queue.findIndex(function (data) {
  438. return data.queueId === queueId;
  439. });
  440. },
  441. length: function () {
  442. var queueData;
  443. return _regeneratorRuntime().async(function (_context4) {
  444. while (1) {
  445. switch (_context4.prev = _context4.next) {
  446. case 0:
  447. _context4.next = 2;
  448. return _regeneratorRuntime().awrap(this.getQueue());
  449. case 2:
  450. queueData = _context4.sent;
  451. return _context4.abrupt("return", queueData.length);
  452. case 4:
  453. case "end":
  454. return _context4.stop();
  455. }
  456. }
  457. }, null, this, null, Promise);
  458. },
  459. sendQueue: function () {
  460. var queue, queueData, i, queueObject, id, hash, className, ObjectType;
  461. return _regeneratorRuntime().async(function (_context5) {
  462. while (1) {
  463. switch (_context5.prev = _context5.next) {
  464. case 0:
  465. _context5.next = 2;
  466. return _regeneratorRuntime().awrap(this.getQueue());
  467. case 2:
  468. queue = _context5.sent;
  469. queueData = (0, _toConsumableArray2.default)(queue);
  470. if (!(queueData.length === 0)) {
  471. _context5.next = 6;
  472. break;
  473. }
  474. return _context5.abrupt("return", false);
  475. case 6:
  476. i = 0;
  477. case 7:
  478. if (!(i < queueData.length)) {
  479. _context5.next = 26;
  480. break;
  481. }
  482. queueObject = queueData[i];
  483. id = queueObject.id, hash = queueObject.hash, className = queueObject.className;
  484. ObjectType = _ParseObject.default.extend(className);
  485. if (!id) {
  486. _context5.next = 16;
  487. break;
  488. }
  489. _context5.next = 14;
  490. return _regeneratorRuntime().awrap(this.process.byId(ObjectType, queueObject));
  491. case 14:
  492. _context5.next = 23;
  493. break;
  494. case 16:
  495. if (!hash) {
  496. _context5.next = 21;
  497. break;
  498. }
  499. _context5.next = 19;
  500. return _regeneratorRuntime().awrap(this.process.byHash(ObjectType, queueObject));
  501. case 19:
  502. _context5.next = 23;
  503. break;
  504. case 21:
  505. _context5.next = 23;
  506. return _regeneratorRuntime().awrap(this.process.create(ObjectType, queueObject));
  507. case 23:
  508. i += 1;
  509. _context5.next = 7;
  510. break;
  511. case 26:
  512. return _context5.abrupt("return", true);
  513. case 27:
  514. case "end":
  515. return _context5.stop();
  516. }
  517. }
  518. }, null, this, null, Promise);
  519. },
  520. sendQueueCallback: function (object, queueObject) {
  521. return _regeneratorRuntime().async(function (_context6) {
  522. while (1) {
  523. switch (_context6.prev = _context6.next) {
  524. case 0:
  525. if (object) {
  526. _context6.next = 2;
  527. break;
  528. }
  529. return _context6.abrupt("return", this.remove(queueObject.queueId));
  530. case 2:
  531. _context6.t0 = queueObject.action;
  532. _context6.next = _context6.t0 === 'save' ? 5 : _context6.t0 === 'destroy' ? 20 : 33;
  533. break;
  534. case 5:
  535. if (!(typeof object.updatedAt !== 'undefined' && object.updatedAt > new Date(queueObject.object.createdAt))) {
  536. _context6.next = 7;
  537. break;
  538. }
  539. return _context6.abrupt("return", this.remove(queueObject.queueId));
  540. case 7:
  541. _context6.prev = 7;
  542. _context6.next = 10;
  543. return _regeneratorRuntime().awrap(object.save(queueObject.object, queueObject.serverOptions));
  544. case 10:
  545. _context6.next = 12;
  546. return _regeneratorRuntime().awrap(this.remove(queueObject.queueId));
  547. case 12:
  548. _context6.next = 19;
  549. break;
  550. case 14:
  551. _context6.prev = 14;
  552. _context6.t1 = _context6["catch"](7);
  553. if (!(_context6.t1.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"')) {
  554. _context6.next = 19;
  555. break;
  556. }
  557. _context6.next = 19;
  558. return _regeneratorRuntime().awrap(this.remove(queueObject.queueId));
  559. case 19:
  560. return _context6.abrupt("break", 33);
  561. case 20:
  562. _context6.prev = 20;
  563. _context6.next = 23;
  564. return _regeneratorRuntime().awrap(object.destroy(queueObject.serverOptions));
  565. case 23:
  566. _context6.next = 25;
  567. return _regeneratorRuntime().awrap(this.remove(queueObject.queueId));
  568. case 25:
  569. _context6.next = 32;
  570. break;
  571. case 27:
  572. _context6.prev = 27;
  573. _context6.t2 = _context6["catch"](20);
  574. if (!(_context6.t2.message !== 'XMLHttpRequest failed: "Unable to connect to the Parse API"')) {
  575. _context6.next = 32;
  576. break;
  577. }
  578. _context6.next = 32;
  579. return _regeneratorRuntime().awrap(this.remove(queueObject.queueId));
  580. case 32:
  581. return _context6.abrupt("break", 33);
  582. case 33:
  583. case "end":
  584. return _context6.stop();
  585. }
  586. }
  587. }, null, this, [[7, 14], [20, 27]], Promise);
  588. },
  589. poll: function () {
  590. var _this = this;
  591. var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2000;
  592. if (polling) {
  593. return;
  594. }
  595. polling = setInterval(function () {
  596. var RESTController = _CoreManager.default.getRESTController();
  597. RESTController.request('GET', 'health').then(function (_ref) {
  598. var status = _ref.status;
  599. if (status === 'ok') {
  600. _this.stopPoll();
  601. return _this.sendQueue();
  602. }
  603. }).catch(function (e) {
  604. return e;
  605. });
  606. }, ms);
  607. },
  608. stopPoll: function () {
  609. clearInterval(polling);
  610. polling = undefined;
  611. },
  612. isPolling: function () {
  613. return !!polling;
  614. },
  615. _setPolling: function (flag) {
  616. polling = flag;
  617. },
  618. process: {
  619. create: function (ObjectType, queueObject) {
  620. var object = new ObjectType();
  621. return EventuallyQueue.sendQueueCallback(object, queueObject);
  622. },
  623. byId: function (ObjectType, queueObject) {
  624. var sessionToken, query, results;
  625. return _regeneratorRuntime().async(function (_context7) {
  626. while (1) {
  627. switch (_context7.prev = _context7.next) {
  628. case 0:
  629. sessionToken = queueObject.serverOptions.sessionToken;
  630. query = new _ParseQuery.default(ObjectType);
  631. query.equalTo('objectId', queueObject.id);
  632. _context7.next = 5;
  633. return _regeneratorRuntime().awrap(query.find({
  634. sessionToken: sessionToken
  635. }));
  636. case 5:
  637. results = _context7.sent;
  638. return _context7.abrupt("return", EventuallyQueue.sendQueueCallback(results[0], queueObject));
  639. case 7:
  640. case "end":
  641. return _context7.stop();
  642. }
  643. }
  644. }, null, null, null, Promise);
  645. },
  646. byHash: function (ObjectType, queueObject) {
  647. var sessionToken, query, results;
  648. return _regeneratorRuntime().async(function (_context8) {
  649. while (1) {
  650. switch (_context8.prev = _context8.next) {
  651. case 0:
  652. sessionToken = queueObject.serverOptions.sessionToken;
  653. query = new _ParseQuery.default(ObjectType);
  654. query.equalTo('hash', queueObject.hash);
  655. _context8.next = 5;
  656. return _regeneratorRuntime().awrap(query.find({
  657. sessionToken: sessionToken
  658. }));
  659. case 5:
  660. results = _context8.sent;
  661. if (!(results.length > 0)) {
  662. _context8.next = 8;
  663. break;
  664. }
  665. return _context8.abrupt("return", EventuallyQueue.sendQueueCallback(results[0], queueObject));
  666. case 8:
  667. return _context8.abrupt("return", EventuallyQueue.process.create(ObjectType, queueObject));
  668. case 9:
  669. case "end":
  670. return _context8.stop();
  671. }
  672. }
  673. }, null, null, null, Promise);
  674. }
  675. }
  676. };
  677. module.exports = EventuallyQueue;