ParseLiveQuery.js 18 KB

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