ParseObject.js 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _canBeSerialized = _interopRequireDefault(require("./canBeSerialized"));
  11. var _decode = _interopRequireDefault(require("./decode"));
  12. var _encode = _interopRequireDefault(require("./encode"));
  13. var _escape2 = _interopRequireDefault(require("./escape"));
  14. var _EventuallyQueue = _interopRequireDefault(require("./EventuallyQueue"));
  15. var _ParseACL = _interopRequireDefault(require("./ParseACL"));
  16. var _parseDate = _interopRequireDefault(require("./parseDate"));
  17. var _ParseError = _interopRequireDefault(require("./ParseError"));
  18. var _ParseFile = _interopRequireDefault(require("./ParseFile"));
  19. var _promiseUtils = require("./promiseUtils");
  20. var _LocalDatastoreUtils = require("./LocalDatastoreUtils");
  21. var _ParseOp = require("./ParseOp");
  22. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  23. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  24. var SingleInstanceStateController = _interopRequireWildcard(require("./SingleInstanceStateController"));
  25. var _unique = _interopRequireDefault(require("./unique"));
  26. var UniqueInstanceStateController = _interopRequireWildcard(require("./UniqueInstanceStateController"));
  27. var _unsavedChildren = _interopRequireDefault(require("./unsavedChildren"));
  28. function _getRequireWildcardCache(nodeInterop) {
  29. if (typeof WeakMap !== "function") return null;
  30. var cacheBabelInterop = new WeakMap();
  31. var cacheNodeInterop = new WeakMap();
  32. return (_getRequireWildcardCache = function (nodeInterop) {
  33. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  34. })(nodeInterop);
  35. }
  36. function _interopRequireWildcard(obj, nodeInterop) {
  37. if (!nodeInterop && obj && obj.__esModule) {
  38. return obj;
  39. }
  40. if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
  41. return {
  42. default: obj
  43. };
  44. }
  45. var cache = _getRequireWildcardCache(nodeInterop);
  46. if (cache && cache.has(obj)) {
  47. return cache.get(obj);
  48. }
  49. var newObj = {};
  50. var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
  51. for (var key in obj) {
  52. if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
  53. var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
  54. if (desc && (desc.get || desc.set)) {
  55. Object.defineProperty(newObj, key, desc);
  56. } else {
  57. newObj[key] = obj[key];
  58. }
  59. }
  60. }
  61. newObj.default = obj;
  62. if (cache) {
  63. cache.set(obj, newObj);
  64. }
  65. return newObj;
  66. }
  67. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  68. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  69. if (it) return (it = it.call(o)).next.bind(it);
  70. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  71. if (it) o = it;
  72. var i = 0;
  73. return function () {
  74. if (i >= o.length) return {
  75. done: true
  76. };
  77. return {
  78. done: false,
  79. value: o[i++]
  80. };
  81. };
  82. }
  83. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  84. }
  85. function _unsupportedIterableToArray(o, minLen) {
  86. if (!o) return;
  87. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  88. var n = Object.prototype.toString.call(o).slice(8, -1);
  89. if (n === "Object" && o.constructor) n = o.constructor.name;
  90. if (n === "Map" || n === "Set") return Array.from(o);
  91. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  92. }
  93. function _arrayLikeToArray(arr, len) {
  94. if (len == null || len > arr.length) len = arr.length;
  95. for (var i = 0, arr2 = new Array(len); i < len; i++) {
  96. arr2[i] = arr[i];
  97. }
  98. return arr2;
  99. }
  100. function _regeneratorRuntime() {
  101. "use strict";
  102. _regeneratorRuntime = function () {
  103. return exports;
  104. };
  105. var exports = {},
  106. Op = Object.prototype,
  107. hasOwn = Op.hasOwnProperty,
  108. $Symbol = "function" == typeof Symbol ? Symbol : {},
  109. iteratorSymbol = $Symbol.iterator || "@@iterator",
  110. asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator",
  111. toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag";
  112. function define(obj, key, value) {
  113. return Object.defineProperty(obj, key, {
  114. value: value,
  115. enumerable: !0,
  116. configurable: !0,
  117. writable: !0
  118. }), obj[key];
  119. }
  120. try {
  121. define({}, "");
  122. } catch (err) {
  123. define = function (obj, key, value) {
  124. return obj[key] = value;
  125. };
  126. }
  127. function wrap(innerFn, outerFn, self, tryLocsList) {
  128. var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator,
  129. generator = Object.create(protoGenerator.prototype),
  130. context = new Context(tryLocsList || []);
  131. return generator._invoke = function (innerFn, self, context) {
  132. var state = "suspendedStart";
  133. return function (method, arg) {
  134. if ("executing" === state) throw new Error("Generator is already running");
  135. if ("completed" === state) {
  136. if ("throw" === method) throw arg;
  137. return doneResult();
  138. }
  139. for (context.method = method, context.arg = arg;;) {
  140. var delegate = context.delegate;
  141. if (delegate) {
  142. var delegateResult = maybeInvokeDelegate(delegate, context);
  143. if (delegateResult) {
  144. if (delegateResult === ContinueSentinel) continue;
  145. return delegateResult;
  146. }
  147. }
  148. if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) {
  149. if ("suspendedStart" === state) throw state = "completed", context.arg;
  150. context.dispatchException(context.arg);
  151. } else "return" === context.method && context.abrupt("return", context.arg);
  152. state = "executing";
  153. var record = tryCatch(innerFn, self, context);
  154. if ("normal" === record.type) {
  155. if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue;
  156. return {
  157. value: record.arg,
  158. done: context.done
  159. };
  160. }
  161. "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg);
  162. }
  163. };
  164. }(innerFn, self, context), generator;
  165. }
  166. function tryCatch(fn, obj, arg) {
  167. try {
  168. return {
  169. type: "normal",
  170. arg: fn.call(obj, arg)
  171. };
  172. } catch (err) {
  173. return {
  174. type: "throw",
  175. arg: err
  176. };
  177. }
  178. }
  179. exports.wrap = wrap;
  180. var ContinueSentinel = {};
  181. function Generator() {}
  182. function GeneratorFunction() {}
  183. function GeneratorFunctionPrototype() {}
  184. var IteratorPrototype = {};
  185. define(IteratorPrototype, iteratorSymbol, function () {
  186. return this;
  187. });
  188. var getProto = Object.getPrototypeOf,
  189. NativeIteratorPrototype = getProto && getProto(getProto(values([])));
  190. NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype);
  191. var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype);
  192. function defineIteratorMethods(prototype) {
  193. ["next", "throw", "return"].forEach(function (method) {
  194. define(prototype, method, function (arg) {
  195. return this._invoke(method, arg);
  196. });
  197. });
  198. }
  199. function AsyncIterator(generator, PromiseImpl) {
  200. function invoke(method, arg, resolve, reject) {
  201. var record = tryCatch(generator[method], generator, arg);
  202. if ("throw" !== record.type) {
  203. var result = record.arg,
  204. value = result.value;
  205. return value && "object" == typeof value && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) {
  206. invoke("next", value, resolve, reject);
  207. }, function (err) {
  208. invoke("throw", err, resolve, reject);
  209. }) : PromiseImpl.resolve(value).then(function (unwrapped) {
  210. result.value = unwrapped, resolve(result);
  211. }, function (error) {
  212. return invoke("throw", error, resolve, reject);
  213. });
  214. }
  215. reject(record.arg);
  216. }
  217. var previousPromise;
  218. this._invoke = function (method, arg) {
  219. function callInvokeWithMethodAndArg() {
  220. return new PromiseImpl(function (resolve, reject) {
  221. invoke(method, arg, resolve, reject);
  222. });
  223. }
  224. return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
  225. };
  226. }
  227. function maybeInvokeDelegate(delegate, context) {
  228. var method = delegate.iterator[context.method];
  229. if (undefined === method) {
  230. if (context.delegate = null, "throw" === context.method) {
  231. if (delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel;
  232. context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method");
  233. }
  234. return ContinueSentinel;
  235. }
  236. var record = tryCatch(method, delegate.iterator, context.arg);
  237. if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel;
  238. var info = record.arg;
  239. 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);
  240. }
  241. function pushTryEntry(locs) {
  242. var entry = {
  243. tryLoc: locs[0]
  244. };
  245. 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry);
  246. }
  247. function resetTryEntry(entry) {
  248. var record = entry.completion || {};
  249. record.type = "normal", delete record.arg, entry.completion = record;
  250. }
  251. function Context(tryLocsList) {
  252. this.tryEntries = [{
  253. tryLoc: "root"
  254. }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0);
  255. }
  256. function values(iterable) {
  257. if (iterable) {
  258. var iteratorMethod = iterable[iteratorSymbol];
  259. if (iteratorMethod) return iteratorMethod.call(iterable);
  260. if ("function" == typeof iterable.next) return iterable;
  261. if (!isNaN(iterable.length)) {
  262. var i = -1,
  263. next = function next() {
  264. for (; ++i < iterable.length;) {
  265. if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next;
  266. }
  267. return next.value = undefined, next.done = !0, next;
  268. };
  269. return next.next = next;
  270. }
  271. }
  272. return {
  273. next: doneResult
  274. };
  275. }
  276. function doneResult() {
  277. return {
  278. value: undefined,
  279. done: !0
  280. };
  281. }
  282. return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) {
  283. var ctor = "function" == typeof genFun && genFun.constructor;
  284. return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name));
  285. }, exports.mark = function (genFun) {
  286. return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun;
  287. }, exports.awrap = function (arg) {
  288. return {
  289. __await: arg
  290. };
  291. }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () {
  292. return this;
  293. }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
  294. void 0 === PromiseImpl && (PromiseImpl = Promise);
  295. var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
  296. return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) {
  297. return result.done ? result.value : iter.next();
  298. });
  299. }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () {
  300. return this;
  301. }), define(Gp, "toString", function () {
  302. return "[object Generator]";
  303. }), exports.keys = function (object) {
  304. var keys = [];
  305. for (var key in object) {
  306. keys.push(key);
  307. }
  308. return keys.reverse(), function next() {
  309. for (; keys.length;) {
  310. var key = keys.pop();
  311. if (key in object) return next.value = key, next.done = !1, next;
  312. }
  313. return next.done = !0, next;
  314. };
  315. }, exports.values = values, Context.prototype = {
  316. constructor: Context,
  317. reset: function (skipTempReset) {
  318. 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) {
  319. "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined);
  320. }
  321. },
  322. stop: function () {
  323. this.done = !0;
  324. var rootRecord = this.tryEntries[0].completion;
  325. if ("throw" === rootRecord.type) throw rootRecord.arg;
  326. return this.rval;
  327. },
  328. dispatchException: function (exception) {
  329. if (this.done) throw exception;
  330. var context = this;
  331. function handle(loc, caught) {
  332. return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught;
  333. }
  334. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  335. var entry = this.tryEntries[i],
  336. record = entry.completion;
  337. if ("root" === entry.tryLoc) return handle("end");
  338. if (entry.tryLoc <= this.prev) {
  339. var hasCatch = hasOwn.call(entry, "catchLoc"),
  340. hasFinally = hasOwn.call(entry, "finallyLoc");
  341. if (hasCatch && hasFinally) {
  342. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  343. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  344. } else if (hasCatch) {
  345. if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0);
  346. } else {
  347. if (!hasFinally) throw new Error("try statement without catch or finally");
  348. if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc);
  349. }
  350. }
  351. }
  352. },
  353. abrupt: function (type, arg) {
  354. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  355. var entry = this.tryEntries[i];
  356. if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) {
  357. var finallyEntry = entry;
  358. break;
  359. }
  360. }
  361. finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null);
  362. var record = finallyEntry ? finallyEntry.completion : {};
  363. return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record);
  364. },
  365. complete: function (record, afterLoc) {
  366. if ("throw" === record.type) throw record.arg;
  367. 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;
  368. },
  369. finish: function (finallyLoc) {
  370. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  371. var entry = this.tryEntries[i];
  372. if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel;
  373. }
  374. },
  375. catch: function (tryLoc) {
  376. for (var i = this.tryEntries.length - 1; i >= 0; --i) {
  377. var entry = this.tryEntries[i];
  378. if (entry.tryLoc === tryLoc) {
  379. var record = entry.completion;
  380. if ("throw" === record.type) {
  381. var thrown = record.arg;
  382. resetTryEntry(entry);
  383. }
  384. return thrown;
  385. }
  386. }
  387. throw new Error("illegal catch attempt");
  388. },
  389. delegateYield: function (iterable, resultName, nextLoc) {
  390. return this.delegate = {
  391. iterator: values(iterable),
  392. resultName: resultName,
  393. nextLoc: nextLoc
  394. }, "next" === this.method && (this.arg = undefined), ContinueSentinel;
  395. }
  396. }, exports;
  397. }
  398. function ownKeys(object, enumerableOnly) {
  399. var keys = Object.keys(object);
  400. if (Object.getOwnPropertySymbols) {
  401. var symbols = Object.getOwnPropertySymbols(object);
  402. enumerableOnly && (symbols = symbols.filter(function (sym) {
  403. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  404. })), keys.push.apply(keys, symbols);
  405. }
  406. return keys;
  407. }
  408. function _objectSpread(target) {
  409. for (var i = 1; i < arguments.length; i++) {
  410. var source = null != arguments[i] ? arguments[i] : {};
  411. i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
  412. (0, _defineProperty2.default)(target, key, source[key]);
  413. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
  414. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  415. });
  416. }
  417. return target;
  418. }
  419. var uuidv4 = require('./uuid');
  420. var classMap = {};
  421. var objectCount = 0;
  422. var singleInstance = !_CoreManager.default.get('IS_NODE');
  423. if (singleInstance) {
  424. _CoreManager.default.setObjectStateController(SingleInstanceStateController);
  425. } else {
  426. _CoreManager.default.setObjectStateController(UniqueInstanceStateController);
  427. }
  428. function getServerUrlPath() {
  429. var serverUrl = _CoreManager.default.get('SERVER_URL');
  430. if (serverUrl[serverUrl.length - 1] !== '/') {
  431. serverUrl += '/';
  432. }
  433. var url = serverUrl.replace(/https?:\/\//, '');
  434. return url.substr(url.indexOf('/'));
  435. }
  436. var ParseObject = function () {
  437. function ParseObject(className, attributes, options) {
  438. (0, _classCallCheck2.default)(this, ParseObject);
  439. if (typeof this.initialize === 'function') {
  440. this.initialize.apply(this, arguments);
  441. }
  442. var toSet = null;
  443. this._objCount = objectCount++;
  444. if (typeof className === 'string') {
  445. this.className = className;
  446. if (attributes && typeof attributes === 'object') {
  447. toSet = attributes;
  448. }
  449. } else if (className && typeof className === 'object') {
  450. this.className = className.className;
  451. toSet = {};
  452. for (var _attr in className) {
  453. if (_attr !== 'className') {
  454. toSet[_attr] = className[_attr];
  455. }
  456. }
  457. if (attributes && typeof attributes === 'object') {
  458. options = attributes;
  459. }
  460. }
  461. if (toSet && !this.set(toSet, options)) {
  462. throw new Error("Can't create an invalid Parse Object");
  463. }
  464. }
  465. (0, _createClass2.default)(ParseObject, [{
  466. key: "attributes",
  467. get: function () {
  468. var stateController = _CoreManager.default.getObjectStateController();
  469. return Object.freeze(stateController.estimateAttributes(this._getStateIdentifier()));
  470. }
  471. }, {
  472. key: "createdAt",
  473. get: function () {
  474. return this._getServerData().createdAt;
  475. }
  476. }, {
  477. key: "updatedAt",
  478. get: function () {
  479. return this._getServerData().updatedAt;
  480. }
  481. }, {
  482. key: "_getId",
  483. value: function () {
  484. if (typeof this.id === 'string') {
  485. return this.id;
  486. }
  487. if (typeof this._localId === 'string') {
  488. return this._localId;
  489. }
  490. var localId = 'local' + uuidv4();
  491. this._localId = localId;
  492. return localId;
  493. }
  494. }, {
  495. key: "_getStateIdentifier",
  496. value: function () {
  497. if (singleInstance) {
  498. var id = this.id;
  499. if (!id) {
  500. id = this._getId();
  501. }
  502. return {
  503. id: id,
  504. className: this.className
  505. };
  506. } else {
  507. return this;
  508. }
  509. }
  510. }, {
  511. key: "_getServerData",
  512. value: function () {
  513. var stateController = _CoreManager.default.getObjectStateController();
  514. return stateController.getServerData(this._getStateIdentifier());
  515. }
  516. }, {
  517. key: "_clearServerData",
  518. value: function () {
  519. var serverData = this._getServerData();
  520. var unset = {};
  521. for (var _attr2 in serverData) {
  522. unset[_attr2] = undefined;
  523. }
  524. var stateController = _CoreManager.default.getObjectStateController();
  525. stateController.setServerData(this._getStateIdentifier(), unset);
  526. }
  527. }, {
  528. key: "_getPendingOps",
  529. value: function () {
  530. var stateController = _CoreManager.default.getObjectStateController();
  531. return stateController.getPendingOps(this._getStateIdentifier());
  532. }
  533. }, {
  534. key: "_clearPendingOps",
  535. value: function (keysToClear) {
  536. var pending = this._getPendingOps();
  537. var latest = pending[pending.length - 1];
  538. var keys = keysToClear || Object.keys(latest);
  539. keys.forEach(function (key) {
  540. delete latest[key];
  541. });
  542. }
  543. }, {
  544. key: "_getDirtyObjectAttributes",
  545. value: function () {
  546. var attributes = this.attributes;
  547. var stateController = _CoreManager.default.getObjectStateController();
  548. var objectCache = stateController.getObjectCache(this._getStateIdentifier());
  549. var dirty = {};
  550. for (var _attr3 in attributes) {
  551. var val = attributes[_attr3];
  552. if (val && typeof val === 'object' && !(val instanceof ParseObject) && !(val instanceof _ParseFile.default) && !(val instanceof _ParseRelation.default)) {
  553. try {
  554. var json = (0, _encode.default)(val, false, true);
  555. var stringified = JSON.stringify(json);
  556. if (objectCache[_attr3] !== stringified) {
  557. dirty[_attr3] = val;
  558. }
  559. } catch (e) {
  560. dirty[_attr3] = val;
  561. }
  562. }
  563. }
  564. return dirty;
  565. }
  566. }, {
  567. key: "_toFullJSON",
  568. value: function (seen, offline) {
  569. var json = this.toJSON(seen, offline);
  570. json.__type = 'Object';
  571. json.className = this.className;
  572. return json;
  573. }
  574. }, {
  575. key: "_getSaveJSON",
  576. value: function () {
  577. var pending = this._getPendingOps();
  578. var dirtyObjects = this._getDirtyObjectAttributes();
  579. var json = {};
  580. for (var attr in dirtyObjects) {
  581. var isDotNotation = false;
  582. for (var i = 0; i < pending.length; i += 1) {
  583. for (var field in pending[i]) {
  584. if (field.includes('.')) {
  585. var fieldName = field.split('.')[0];
  586. if (fieldName === attr) {
  587. isDotNotation = true;
  588. break;
  589. }
  590. }
  591. }
  592. }
  593. if (!isDotNotation) {
  594. json[attr] = new _ParseOp.SetOp(dirtyObjects[attr]).toJSON();
  595. }
  596. }
  597. for (attr in pending[0]) {
  598. json[attr] = pending[0][attr].toJSON();
  599. }
  600. return json;
  601. }
  602. }, {
  603. key: "_getSaveParams",
  604. value: function () {
  605. var method = this.id ? 'PUT' : 'POST';
  606. var body = this._getSaveJSON();
  607. var path = 'classes/' + this.className;
  608. if (_CoreManager.default.get('ALLOW_CUSTOM_OBJECT_ID')) {
  609. if (!this.createdAt) {
  610. method = 'POST';
  611. body.objectId = this.id;
  612. } else {
  613. method = 'PUT';
  614. path += '/' + this.id;
  615. }
  616. } else if (this.id) {
  617. path += '/' + this.id;
  618. } else if (this.className === '_User') {
  619. path = 'users';
  620. }
  621. return {
  622. method: method,
  623. body: body,
  624. path: path
  625. };
  626. }
  627. }, {
  628. key: "_finishFetch",
  629. value: function (serverData) {
  630. if (!this.id && serverData.objectId) {
  631. this.id = serverData.objectId;
  632. }
  633. var stateController = _CoreManager.default.getObjectStateController();
  634. stateController.initializeState(this._getStateIdentifier());
  635. var decoded = {};
  636. for (var _attr4 in serverData) {
  637. if (_attr4 === 'ACL') {
  638. decoded[_attr4] = new _ParseACL.default(serverData[_attr4]);
  639. } else if (_attr4 !== 'objectId') {
  640. decoded[_attr4] = (0, _decode.default)(serverData[_attr4]);
  641. if (decoded[_attr4] instanceof _ParseRelation.default) {
  642. decoded[_attr4]._ensureParentAndKey(this, _attr4);
  643. }
  644. }
  645. }
  646. if (decoded.createdAt && typeof decoded.createdAt === 'string') {
  647. decoded.createdAt = (0, _parseDate.default)(decoded.createdAt);
  648. }
  649. if (decoded.updatedAt && typeof decoded.updatedAt === 'string') {
  650. decoded.updatedAt = (0, _parseDate.default)(decoded.updatedAt);
  651. }
  652. if (!decoded.updatedAt && decoded.createdAt) {
  653. decoded.updatedAt = decoded.createdAt;
  654. }
  655. stateController.commitServerChanges(this._getStateIdentifier(), decoded);
  656. }
  657. }, {
  658. key: "_setExisted",
  659. value: function (existed) {
  660. var stateController = _CoreManager.default.getObjectStateController();
  661. var state = stateController.getState(this._getStateIdentifier());
  662. if (state) {
  663. state.existed = existed;
  664. }
  665. }
  666. }, {
  667. key: "_migrateId",
  668. value: function (serverId) {
  669. if (this._localId && serverId) {
  670. if (singleInstance) {
  671. var stateController = _CoreManager.default.getObjectStateController();
  672. var oldState = stateController.removeState(this._getStateIdentifier());
  673. this.id = serverId;
  674. delete this._localId;
  675. if (oldState) {
  676. stateController.initializeState(this._getStateIdentifier(), oldState);
  677. }
  678. } else {
  679. this.id = serverId;
  680. delete this._localId;
  681. }
  682. }
  683. }
  684. }, {
  685. key: "_handleSaveResponse",
  686. value: function (response, status) {
  687. var changes = {};
  688. var stateController = _CoreManager.default.getObjectStateController();
  689. var pending = stateController.popPendingState(this._getStateIdentifier());
  690. for (var attr in pending) {
  691. if (pending[attr] instanceof _ParseOp.RelationOp) {
  692. changes[attr] = pending[attr].applyTo(undefined, this, attr);
  693. } else if (!(attr in response)) {
  694. changes[attr] = pending[attr].applyTo(undefined);
  695. }
  696. }
  697. for (attr in response) {
  698. if ((attr === 'createdAt' || attr === 'updatedAt') && typeof response[attr] === 'string') {
  699. changes[attr] = (0, _parseDate.default)(response[attr]);
  700. } else if (attr === 'ACL') {
  701. changes[attr] = new _ParseACL.default(response[attr]);
  702. } else if (attr !== 'objectId') {
  703. var val = (0, _decode.default)(response[attr]);
  704. if (val && Object.getPrototypeOf(val) === Object.prototype) {
  705. changes[attr] = _objectSpread(_objectSpread({}, this.attributes[attr]), val);
  706. } else {
  707. changes[attr] = val;
  708. }
  709. if (changes[attr] instanceof _ParseOp.UnsetOp) {
  710. changes[attr] = undefined;
  711. }
  712. }
  713. }
  714. if (changes.createdAt && !changes.updatedAt) {
  715. changes.updatedAt = changes.createdAt;
  716. }
  717. this._migrateId(response.objectId);
  718. if (status !== 201) {
  719. this._setExisted(true);
  720. }
  721. stateController.commitServerChanges(this._getStateIdentifier(), changes);
  722. }
  723. }, {
  724. key: "_handleSaveError",
  725. value: function () {
  726. var stateController = _CoreManager.default.getObjectStateController();
  727. stateController.mergeFirstPendingState(this._getStateIdentifier());
  728. }
  729. }, {
  730. key: "initialize",
  731. value: function () {}
  732. }, {
  733. key: "toJSON",
  734. value: function (seen, offline) {
  735. var seenEntry = this.id ? this.className + ':' + this.id : this;
  736. seen = seen || [seenEntry];
  737. var json = {};
  738. var attrs = this.attributes;
  739. for (var _attr5 in attrs) {
  740. if ((_attr5 === 'createdAt' || _attr5 === 'updatedAt') && attrs[_attr5].toJSON) {
  741. json[_attr5] = attrs[_attr5].toJSON();
  742. } else {
  743. json[_attr5] = (0, _encode.default)(attrs[_attr5], false, false, seen, offline);
  744. }
  745. }
  746. var pending = this._getPendingOps();
  747. for (var _attr6 in pending[0]) {
  748. json[_attr6] = pending[0][_attr6].toJSON(offline);
  749. }
  750. if (this.id) {
  751. json.objectId = this.id;
  752. }
  753. return json;
  754. }
  755. }, {
  756. key: "equals",
  757. value: function (other) {
  758. if (this === other) {
  759. return true;
  760. }
  761. return other instanceof ParseObject && this.className === other.className && this.id === other.id && typeof this.id !== 'undefined';
  762. }
  763. }, {
  764. key: "dirty",
  765. value: function (attr) {
  766. if (!this.id) {
  767. return true;
  768. }
  769. var pendingOps = this._getPendingOps();
  770. var dirtyObjects = this._getDirtyObjectAttributes();
  771. if (attr) {
  772. if (dirtyObjects.hasOwnProperty(attr)) {
  773. return true;
  774. }
  775. for (var i = 0; i < pendingOps.length; i++) {
  776. if (pendingOps[i].hasOwnProperty(attr)) {
  777. return true;
  778. }
  779. }
  780. return false;
  781. }
  782. if (Object.keys(pendingOps[0]).length !== 0) {
  783. return true;
  784. }
  785. if (Object.keys(dirtyObjects).length !== 0) {
  786. return true;
  787. }
  788. return false;
  789. }
  790. }, {
  791. key: "dirtyKeys",
  792. value: function () {
  793. var pendingOps = this._getPendingOps();
  794. var keys = {};
  795. for (var i = 0; i < pendingOps.length; i++) {
  796. for (var _attr7 in pendingOps[i]) {
  797. keys[_attr7] = true;
  798. }
  799. }
  800. var dirtyObjects = this._getDirtyObjectAttributes();
  801. for (var _attr8 in dirtyObjects) {
  802. keys[_attr8] = true;
  803. }
  804. return Object.keys(keys);
  805. }
  806. }, {
  807. key: "isDataAvailable",
  808. value: function () {
  809. var serverData = this._getServerData();
  810. return !!Object.keys(serverData).length;
  811. }
  812. }, {
  813. key: "toPointer",
  814. value: function () {
  815. if (!this.id) {
  816. throw new Error('Cannot create a pointer to an unsaved ParseObject');
  817. }
  818. return {
  819. __type: 'Pointer',
  820. className: this.className,
  821. objectId: this.id
  822. };
  823. }
  824. }, {
  825. key: "toOfflinePointer",
  826. value: function () {
  827. if (!this._localId) {
  828. throw new Error('Cannot create a offline pointer to a saved ParseObject');
  829. }
  830. return {
  831. __type: 'Object',
  832. className: this.className,
  833. _localId: this._localId
  834. };
  835. }
  836. }, {
  837. key: "get",
  838. value: function (attr) {
  839. return this.attributes[attr];
  840. }
  841. }, {
  842. key: "relation",
  843. value: function (attr) {
  844. var value = this.get(attr);
  845. if (value) {
  846. if (!(value instanceof _ParseRelation.default)) {
  847. throw new Error('Called relation() on non-relation field ' + attr);
  848. }
  849. value._ensureParentAndKey(this, attr);
  850. return value;
  851. }
  852. return new _ParseRelation.default(this, attr);
  853. }
  854. }, {
  855. key: "escape",
  856. value: function (attr) {
  857. var val = this.attributes[attr];
  858. if (val == null) {
  859. return '';
  860. }
  861. if (typeof val !== 'string') {
  862. if (typeof val.toString !== 'function') {
  863. return '';
  864. }
  865. val = val.toString();
  866. }
  867. return (0, _escape2.default)(val);
  868. }
  869. }, {
  870. key: "has",
  871. value: function (attr) {
  872. var attributes = this.attributes;
  873. if (attributes.hasOwnProperty(attr)) {
  874. return attributes[attr] != null;
  875. }
  876. return false;
  877. }
  878. }, {
  879. key: "set",
  880. value: function (key, value, options) {
  881. var changes = {};
  882. var newOps = {};
  883. if (key && typeof key === 'object') {
  884. changes = key;
  885. options = value;
  886. } else if (typeof key === 'string') {
  887. changes[key] = value;
  888. } else {
  889. return this;
  890. }
  891. options = options || {};
  892. var readonly = [];
  893. if (typeof this.constructor.readOnlyAttributes === 'function') {
  894. readonly = readonly.concat(this.constructor.readOnlyAttributes());
  895. }
  896. for (var k in changes) {
  897. if (k === 'createdAt' || k === 'updatedAt') {
  898. continue;
  899. }
  900. if (readonly.indexOf(k) > -1) {
  901. throw new Error('Cannot modify readonly attribute: ' + k);
  902. }
  903. if (options.unset) {
  904. newOps[k] = new _ParseOp.UnsetOp();
  905. } else if (changes[k] instanceof _ParseOp.Op) {
  906. newOps[k] = changes[k];
  907. } else if (changes[k] && typeof changes[k] === 'object' && typeof changes[k].__op === 'string') {
  908. newOps[k] = (0, _ParseOp.opFromJSON)(changes[k]);
  909. } else if (k === 'objectId' || k === 'id') {
  910. if (typeof changes[k] === 'string') {
  911. this.id = changes[k];
  912. }
  913. } else if (k === 'ACL' && typeof changes[k] === 'object' && !(changes[k] instanceof _ParseACL.default)) {
  914. newOps[k] = new _ParseOp.SetOp(new _ParseACL.default(changes[k]));
  915. } else if (changes[k] instanceof _ParseRelation.default) {
  916. var relation = new _ParseRelation.default(this, k);
  917. relation.targetClassName = changes[k].targetClassName;
  918. newOps[k] = new _ParseOp.SetOp(relation);
  919. } else {
  920. newOps[k] = new _ParseOp.SetOp(changes[k]);
  921. }
  922. }
  923. var currentAttributes = this.attributes;
  924. var newValues = {};
  925. for (var _attr9 in newOps) {
  926. if (newOps[_attr9] instanceof _ParseOp.RelationOp) {
  927. newValues[_attr9] = newOps[_attr9].applyTo(currentAttributes[_attr9], this, _attr9);
  928. } else if (!(newOps[_attr9] instanceof _ParseOp.UnsetOp)) {
  929. newValues[_attr9] = newOps[_attr9].applyTo(currentAttributes[_attr9]);
  930. }
  931. }
  932. if (!options.ignoreValidation) {
  933. var validation = this.validate(newValues);
  934. if (validation) {
  935. if (typeof options.error === 'function') {
  936. options.error(this, validation);
  937. }
  938. return false;
  939. }
  940. }
  941. var pendingOps = this._getPendingOps();
  942. var last = pendingOps.length - 1;
  943. var stateController = _CoreManager.default.getObjectStateController();
  944. for (var _attr10 in newOps) {
  945. var nextOp = newOps[_attr10].mergeWith(pendingOps[last][_attr10]);
  946. stateController.setPendingOp(this._getStateIdentifier(), _attr10, nextOp);
  947. }
  948. return this;
  949. }
  950. }, {
  951. key: "unset",
  952. value: function (attr, options) {
  953. options = options || {};
  954. options.unset = true;
  955. return this.set(attr, null, options);
  956. }
  957. }, {
  958. key: "increment",
  959. value: function (attr, amount) {
  960. if (typeof amount === 'undefined') {
  961. amount = 1;
  962. }
  963. if (typeof amount !== 'number') {
  964. throw new Error('Cannot increment by a non-numeric amount.');
  965. }
  966. return this.set(attr, new _ParseOp.IncrementOp(amount));
  967. }
  968. }, {
  969. key: "decrement",
  970. value: function (attr, amount) {
  971. if (typeof amount === 'undefined') {
  972. amount = 1;
  973. }
  974. if (typeof amount !== 'number') {
  975. throw new Error('Cannot decrement by a non-numeric amount.');
  976. }
  977. return this.set(attr, new _ParseOp.IncrementOp(amount * -1));
  978. }
  979. }, {
  980. key: "add",
  981. value: function (attr, item) {
  982. return this.set(attr, new _ParseOp.AddOp([item]));
  983. }
  984. }, {
  985. key: "addAll",
  986. value: function (attr, items) {
  987. return this.set(attr, new _ParseOp.AddOp(items));
  988. }
  989. }, {
  990. key: "addUnique",
  991. value: function (attr, item) {
  992. return this.set(attr, new _ParseOp.AddUniqueOp([item]));
  993. }
  994. }, {
  995. key: "addAllUnique",
  996. value: function (attr, items) {
  997. return this.set(attr, new _ParseOp.AddUniqueOp(items));
  998. }
  999. }, {
  1000. key: "remove",
  1001. value: function (attr, item) {
  1002. return this.set(attr, new _ParseOp.RemoveOp([item]));
  1003. }
  1004. }, {
  1005. key: "removeAll",
  1006. value: function (attr, items) {
  1007. return this.set(attr, new _ParseOp.RemoveOp(items));
  1008. }
  1009. }, {
  1010. key: "op",
  1011. value: function (attr) {
  1012. var pending = this._getPendingOps();
  1013. for (var i = pending.length; i--;) {
  1014. if (pending[i][attr]) {
  1015. return pending[i][attr];
  1016. }
  1017. }
  1018. }
  1019. }, {
  1020. key: "clone",
  1021. value: function clone() {
  1022. var clone = new this.constructor(this.className);
  1023. var attributes = this.attributes;
  1024. if (typeof this.constructor.readOnlyAttributes === 'function') {
  1025. var readonly = this.constructor.readOnlyAttributes() || [];
  1026. var copy = {};
  1027. for (var a in attributes) {
  1028. if (readonly.indexOf(a) < 0) {
  1029. copy[a] = attributes[a];
  1030. }
  1031. }
  1032. attributes = copy;
  1033. }
  1034. if (clone.set) {
  1035. clone.set(attributes);
  1036. }
  1037. return clone;
  1038. }
  1039. }, {
  1040. key: "newInstance",
  1041. value: function () {
  1042. var clone = new this.constructor(this.className);
  1043. clone.id = this.id;
  1044. if (singleInstance) {
  1045. return clone;
  1046. }
  1047. var stateController = _CoreManager.default.getObjectStateController();
  1048. if (stateController) {
  1049. stateController.duplicateState(this._getStateIdentifier(), clone._getStateIdentifier());
  1050. }
  1051. return clone;
  1052. }
  1053. }, {
  1054. key: "isNew",
  1055. value: function () {
  1056. return !this.id;
  1057. }
  1058. }, {
  1059. key: "existed",
  1060. value: function () {
  1061. if (!this.id) {
  1062. return false;
  1063. }
  1064. var stateController = _CoreManager.default.getObjectStateController();
  1065. var state = stateController.getState(this._getStateIdentifier());
  1066. if (state) {
  1067. return state.existed;
  1068. }
  1069. return false;
  1070. }
  1071. }, {
  1072. key: "exists",
  1073. value: function (options) {
  1074. var query;
  1075. return _regeneratorRuntime().async(function (_context) {
  1076. while (1) {
  1077. switch (_context.prev = _context.next) {
  1078. case 0:
  1079. if (this.id) {
  1080. _context.next = 2;
  1081. break;
  1082. }
  1083. return _context.abrupt("return", false);
  1084. case 2:
  1085. _context.prev = 2;
  1086. query = new _ParseQuery.default(this.className);
  1087. _context.next = 6;
  1088. return _regeneratorRuntime().awrap(query.get(this.id, options));
  1089. case 6:
  1090. return _context.abrupt("return", true);
  1091. case 9:
  1092. _context.prev = 9;
  1093. _context.t0 = _context["catch"](2);
  1094. if (!(_context.t0.code === _ParseError.default.OBJECT_NOT_FOUND)) {
  1095. _context.next = 13;
  1096. break;
  1097. }
  1098. return _context.abrupt("return", false);
  1099. case 13:
  1100. throw _context.t0;
  1101. case 14:
  1102. case "end":
  1103. return _context.stop();
  1104. }
  1105. }
  1106. }, null, this, [[2, 9]], Promise);
  1107. }
  1108. }, {
  1109. key: "isValid",
  1110. value: function () {
  1111. return !this.validate(this.attributes);
  1112. }
  1113. }, {
  1114. key: "validate",
  1115. value: function (attrs) {
  1116. if (attrs.hasOwnProperty('ACL') && !(attrs.ACL instanceof _ParseACL.default)) {
  1117. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, 'ACL must be a Parse ACL.');
  1118. }
  1119. for (var _key in attrs) {
  1120. if (!/^[A-Za-z][0-9A-Za-z_.]*$/.test(_key)) {
  1121. return new _ParseError.default(_ParseError.default.INVALID_KEY_NAME);
  1122. }
  1123. }
  1124. return false;
  1125. }
  1126. }, {
  1127. key: "getACL",
  1128. value: function () {
  1129. var acl = this.get('ACL');
  1130. if (acl instanceof _ParseACL.default) {
  1131. return acl;
  1132. }
  1133. return null;
  1134. }
  1135. }, {
  1136. key: "setACL",
  1137. value: function (acl, options) {
  1138. return this.set('ACL', acl, options);
  1139. }
  1140. }, {
  1141. key: "revert",
  1142. value: function () {
  1143. var keysToRevert;
  1144. for (var _len = arguments.length, keys = new Array(_len), _key2 = 0; _key2 < _len; _key2++) {
  1145. keys[_key2] = arguments[_key2];
  1146. }
  1147. if (keys.length) {
  1148. keysToRevert = [];
  1149. for (var _iterator = _createForOfIteratorHelperLoose(keys), _step; !(_step = _iterator()).done;) {
  1150. var _key3 = _step.value;
  1151. if (typeof _key3 === 'string') {
  1152. keysToRevert.push(_key3);
  1153. } else {
  1154. throw new Error('Parse.Object#revert expects either no, or a list of string, arguments.');
  1155. }
  1156. }
  1157. }
  1158. this._clearPendingOps(keysToRevert);
  1159. }
  1160. }, {
  1161. key: "clear",
  1162. value: function () {
  1163. var attributes = this.attributes;
  1164. var erasable = {};
  1165. var readonly = ['createdAt', 'updatedAt'];
  1166. if (typeof this.constructor.readOnlyAttributes === 'function') {
  1167. readonly = readonly.concat(this.constructor.readOnlyAttributes());
  1168. }
  1169. for (var _attr11 in attributes) {
  1170. if (readonly.indexOf(_attr11) < 0) {
  1171. erasable[_attr11] = true;
  1172. }
  1173. }
  1174. return this.set(erasable, {
  1175. unset: true
  1176. });
  1177. }
  1178. }, {
  1179. key: "fetch",
  1180. value: function (options) {
  1181. options = options || {};
  1182. var fetchOptions = {};
  1183. if (options.hasOwnProperty('useMasterKey')) {
  1184. fetchOptions.useMasterKey = options.useMasterKey;
  1185. }
  1186. if (options.hasOwnProperty('sessionToken')) {
  1187. fetchOptions.sessionToken = options.sessionToken;
  1188. }
  1189. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  1190. fetchOptions.context = options.context;
  1191. }
  1192. if (options.hasOwnProperty('include')) {
  1193. fetchOptions.include = [];
  1194. if (Array.isArray(options.include)) {
  1195. options.include.forEach(function (key) {
  1196. if (Array.isArray(key)) {
  1197. fetchOptions.include = fetchOptions.include.concat(key);
  1198. } else {
  1199. fetchOptions.include.push(key);
  1200. }
  1201. });
  1202. } else {
  1203. fetchOptions.include.push(options.include);
  1204. }
  1205. }
  1206. var controller = _CoreManager.default.getObjectController();
  1207. return controller.fetch(this, true, fetchOptions);
  1208. }
  1209. }, {
  1210. key: "fetchWithInclude",
  1211. value: function (keys, options) {
  1212. options = options || {};
  1213. options.include = keys;
  1214. return this.fetch(options);
  1215. }
  1216. }, {
  1217. key: "saveEventually",
  1218. value: function (options) {
  1219. return _regeneratorRuntime().async(function (_context2) {
  1220. while (1) {
  1221. switch (_context2.prev = _context2.next) {
  1222. case 0:
  1223. _context2.prev = 0;
  1224. _context2.next = 3;
  1225. return _regeneratorRuntime().awrap(this.save(null, options));
  1226. case 3:
  1227. _context2.next = 11;
  1228. break;
  1229. case 5:
  1230. _context2.prev = 5;
  1231. _context2.t0 = _context2["catch"](0);
  1232. if (!(_context2.t0.message === 'XMLHttpRequest failed: "Unable to connect to the Parse API"')) {
  1233. _context2.next = 11;
  1234. break;
  1235. }
  1236. _context2.next = 10;
  1237. return _regeneratorRuntime().awrap(_EventuallyQueue.default.save(this, options));
  1238. case 10:
  1239. _EventuallyQueue.default.poll();
  1240. case 11:
  1241. return _context2.abrupt("return", this);
  1242. case 12:
  1243. case "end":
  1244. return _context2.stop();
  1245. }
  1246. }
  1247. }, null, this, [[0, 5]], Promise);
  1248. }
  1249. }, {
  1250. key: "save",
  1251. value: function (arg1, arg2, arg3) {
  1252. var _this = this;
  1253. var attrs;
  1254. var options;
  1255. if (typeof arg1 === 'object' || typeof arg1 === 'undefined') {
  1256. attrs = arg1;
  1257. if (typeof arg2 === 'object') {
  1258. options = arg2;
  1259. }
  1260. } else {
  1261. attrs = {};
  1262. attrs[arg1] = arg2;
  1263. options = arg3;
  1264. }
  1265. if (attrs) {
  1266. var validation = this.validate(attrs);
  1267. if (validation) {
  1268. return Promise.reject(validation);
  1269. }
  1270. this.set(attrs, options);
  1271. }
  1272. options = options || {};
  1273. var saveOptions = {};
  1274. if (options.hasOwnProperty('useMasterKey')) {
  1275. saveOptions.useMasterKey = !!options.useMasterKey;
  1276. }
  1277. if (options.hasOwnProperty('sessionToken') && typeof options.sessionToken === 'string') {
  1278. saveOptions.sessionToken = options.sessionToken;
  1279. }
  1280. if (options.hasOwnProperty('installationId') && typeof options.installationId === 'string') {
  1281. saveOptions.installationId = options.installationId;
  1282. }
  1283. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  1284. saveOptions.context = options.context;
  1285. }
  1286. var controller = _CoreManager.default.getObjectController();
  1287. var unsaved = options.cascadeSave !== false ? (0, _unsavedChildren.default)(this) : null;
  1288. return controller.save(unsaved, saveOptions).then(function () {
  1289. return controller.save(_this, saveOptions);
  1290. });
  1291. }
  1292. }, {
  1293. key: "destroyEventually",
  1294. value: function (options) {
  1295. return _regeneratorRuntime().async(function (_context3) {
  1296. while (1) {
  1297. switch (_context3.prev = _context3.next) {
  1298. case 0:
  1299. _context3.prev = 0;
  1300. _context3.next = 3;
  1301. return _regeneratorRuntime().awrap(this.destroy(options));
  1302. case 3:
  1303. _context3.next = 11;
  1304. break;
  1305. case 5:
  1306. _context3.prev = 5;
  1307. _context3.t0 = _context3["catch"](0);
  1308. if (!(_context3.t0.message === 'XMLHttpRequest failed: "Unable to connect to the Parse API"')) {
  1309. _context3.next = 11;
  1310. break;
  1311. }
  1312. _context3.next = 10;
  1313. return _regeneratorRuntime().awrap(_EventuallyQueue.default.destroy(this, options));
  1314. case 10:
  1315. _EventuallyQueue.default.poll();
  1316. case 11:
  1317. return _context3.abrupt("return", this);
  1318. case 12:
  1319. case "end":
  1320. return _context3.stop();
  1321. }
  1322. }
  1323. }, null, this, [[0, 5]], Promise);
  1324. }
  1325. }, {
  1326. key: "destroy",
  1327. value: function (options) {
  1328. options = options || {};
  1329. var destroyOptions = {};
  1330. if (options.hasOwnProperty('useMasterKey')) {
  1331. destroyOptions.useMasterKey = options.useMasterKey;
  1332. }
  1333. if (options.hasOwnProperty('sessionToken')) {
  1334. destroyOptions.sessionToken = options.sessionToken;
  1335. }
  1336. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  1337. destroyOptions.context = options.context;
  1338. }
  1339. if (!this.id) {
  1340. return Promise.resolve();
  1341. }
  1342. return _CoreManager.default.getObjectController().destroy(this, destroyOptions);
  1343. }
  1344. }, {
  1345. key: "pin",
  1346. value: function () {
  1347. return ParseObject.pinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, [this]);
  1348. }
  1349. }, {
  1350. key: "unPin",
  1351. value: function () {
  1352. return ParseObject.unPinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, [this]);
  1353. }
  1354. }, {
  1355. key: "isPinned",
  1356. value: function () {
  1357. var localDatastore, objectKey, pin;
  1358. return _regeneratorRuntime().async(function (_context4) {
  1359. while (1) {
  1360. switch (_context4.prev = _context4.next) {
  1361. case 0:
  1362. localDatastore = _CoreManager.default.getLocalDatastore();
  1363. if (localDatastore.isEnabled) {
  1364. _context4.next = 3;
  1365. break;
  1366. }
  1367. return _context4.abrupt("return", Promise.reject('Parse.enableLocalDatastore() must be called first'));
  1368. case 3:
  1369. objectKey = localDatastore.getKeyForObject(this);
  1370. _context4.next = 6;
  1371. return _regeneratorRuntime().awrap(localDatastore.fromPinWithName(objectKey));
  1372. case 6:
  1373. pin = _context4.sent;
  1374. return _context4.abrupt("return", pin.length > 0);
  1375. case 8:
  1376. case "end":
  1377. return _context4.stop();
  1378. }
  1379. }
  1380. }, null, this, null, Promise);
  1381. }
  1382. }, {
  1383. key: "pinWithName",
  1384. value: function (name) {
  1385. return ParseObject.pinAllWithName(name, [this]);
  1386. }
  1387. }, {
  1388. key: "unPinWithName",
  1389. value: function (name) {
  1390. return ParseObject.unPinAllWithName(name, [this]);
  1391. }
  1392. }, {
  1393. key: "fetchFromLocalDatastore",
  1394. value: function () {
  1395. var localDatastore, objectKey, pinned, result;
  1396. return _regeneratorRuntime().async(function (_context5) {
  1397. while (1) {
  1398. switch (_context5.prev = _context5.next) {
  1399. case 0:
  1400. localDatastore = _CoreManager.default.getLocalDatastore();
  1401. if (localDatastore.isEnabled) {
  1402. _context5.next = 3;
  1403. break;
  1404. }
  1405. throw new Error('Parse.enableLocalDatastore() must be called first');
  1406. case 3:
  1407. objectKey = localDatastore.getKeyForObject(this);
  1408. _context5.next = 6;
  1409. return _regeneratorRuntime().awrap(localDatastore._serializeObject(objectKey));
  1410. case 6:
  1411. pinned = _context5.sent;
  1412. if (pinned) {
  1413. _context5.next = 9;
  1414. break;
  1415. }
  1416. throw new Error('Cannot fetch an unsaved ParseObject');
  1417. case 9:
  1418. result = ParseObject.fromJSON(pinned);
  1419. this._finishFetch(result.toJSON());
  1420. return _context5.abrupt("return", this);
  1421. case 12:
  1422. case "end":
  1423. return _context5.stop();
  1424. }
  1425. }
  1426. }, null, this, null, Promise);
  1427. }
  1428. }], [{
  1429. key: "_getClassMap",
  1430. value: function () {
  1431. return classMap;
  1432. }
  1433. }, {
  1434. key: "_clearAllState",
  1435. value: function () {
  1436. var stateController = _CoreManager.default.getObjectStateController();
  1437. stateController.clearAllState();
  1438. }
  1439. }, {
  1440. key: "fetchAll",
  1441. value: function (list) {
  1442. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1443. var queryOptions = {};
  1444. if (options.hasOwnProperty('useMasterKey')) {
  1445. queryOptions.useMasterKey = options.useMasterKey;
  1446. }
  1447. if (options.hasOwnProperty('sessionToken')) {
  1448. queryOptions.sessionToken = options.sessionToken;
  1449. }
  1450. if (options.hasOwnProperty('include')) {
  1451. queryOptions.include = ParseObject.handleIncludeOptions(options);
  1452. }
  1453. return _CoreManager.default.getObjectController().fetch(list, true, queryOptions);
  1454. }
  1455. }, {
  1456. key: "fetchAllWithInclude",
  1457. value: function (list, keys, options) {
  1458. options = options || {};
  1459. options.include = keys;
  1460. return ParseObject.fetchAll(list, options);
  1461. }
  1462. }, {
  1463. key: "fetchAllIfNeededWithInclude",
  1464. value: function (list, keys, options) {
  1465. options = options || {};
  1466. options.include = keys;
  1467. return ParseObject.fetchAllIfNeeded(list, options);
  1468. }
  1469. }, {
  1470. key: "fetchAllIfNeeded",
  1471. value: function (list, options) {
  1472. options = options || {};
  1473. var queryOptions = {};
  1474. if (options.hasOwnProperty('useMasterKey')) {
  1475. queryOptions.useMasterKey = options.useMasterKey;
  1476. }
  1477. if (options.hasOwnProperty('sessionToken')) {
  1478. queryOptions.sessionToken = options.sessionToken;
  1479. }
  1480. if (options.hasOwnProperty('include')) {
  1481. queryOptions.include = ParseObject.handleIncludeOptions(options);
  1482. }
  1483. return _CoreManager.default.getObjectController().fetch(list, false, queryOptions);
  1484. }
  1485. }, {
  1486. key: "handleIncludeOptions",
  1487. value: function (options) {
  1488. var include = [];
  1489. if (Array.isArray(options.include)) {
  1490. options.include.forEach(function (key) {
  1491. if (Array.isArray(key)) {
  1492. include = include.concat(key);
  1493. } else {
  1494. include.push(key);
  1495. }
  1496. });
  1497. } else {
  1498. include.push(options.include);
  1499. }
  1500. return include;
  1501. }
  1502. }, {
  1503. key: "destroyAll",
  1504. value: function (list) {
  1505. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1506. var destroyOptions = {};
  1507. if (options.hasOwnProperty('useMasterKey')) {
  1508. destroyOptions.useMasterKey = options.useMasterKey;
  1509. }
  1510. if (options.hasOwnProperty('sessionToken')) {
  1511. destroyOptions.sessionToken = options.sessionToken;
  1512. }
  1513. if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
  1514. destroyOptions.batchSize = options.batchSize;
  1515. }
  1516. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  1517. destroyOptions.context = options.context;
  1518. }
  1519. return _CoreManager.default.getObjectController().destroy(list, destroyOptions);
  1520. }
  1521. }, {
  1522. key: "saveAll",
  1523. value: function (list) {
  1524. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1525. var saveOptions = {};
  1526. if (options.hasOwnProperty('useMasterKey')) {
  1527. saveOptions.useMasterKey = options.useMasterKey;
  1528. }
  1529. if (options.hasOwnProperty('sessionToken')) {
  1530. saveOptions.sessionToken = options.sessionToken;
  1531. }
  1532. if (options.hasOwnProperty('batchSize') && typeof options.batchSize === 'number') {
  1533. saveOptions.batchSize = options.batchSize;
  1534. }
  1535. if (options.hasOwnProperty('context') && typeof options.context === 'object') {
  1536. saveOptions.context = options.context;
  1537. }
  1538. return _CoreManager.default.getObjectController().save(list, saveOptions);
  1539. }
  1540. }, {
  1541. key: "createWithoutData",
  1542. value: function (id) {
  1543. var obj = new this();
  1544. obj.id = id;
  1545. return obj;
  1546. }
  1547. }, {
  1548. key: "fromJSON",
  1549. value: function (json, override, dirty) {
  1550. if (!json.className) {
  1551. throw new Error('Cannot create an object without a className');
  1552. }
  1553. var constructor = classMap[json.className];
  1554. var o = constructor ? new constructor(json.className) : new ParseObject(json.className);
  1555. var otherAttributes = {};
  1556. for (var _attr12 in json) {
  1557. if (_attr12 !== 'className' && _attr12 !== '__type') {
  1558. otherAttributes[_attr12] = json[_attr12];
  1559. if (dirty) {
  1560. o.set(_attr12, json[_attr12]);
  1561. }
  1562. }
  1563. }
  1564. if (override) {
  1565. if (otherAttributes.objectId) {
  1566. o.id = otherAttributes.objectId;
  1567. }
  1568. var preserved = null;
  1569. if (typeof o._preserveFieldsOnFetch === 'function') {
  1570. preserved = o._preserveFieldsOnFetch();
  1571. }
  1572. o._clearServerData();
  1573. if (preserved) {
  1574. o._finishFetch(preserved);
  1575. }
  1576. }
  1577. o._finishFetch(otherAttributes);
  1578. if (json.objectId) {
  1579. o._setExisted(true);
  1580. }
  1581. return o;
  1582. }
  1583. }, {
  1584. key: "registerSubclass",
  1585. value: function (className, constructor) {
  1586. if (typeof className !== 'string') {
  1587. throw new TypeError('The first argument must be a valid class name.');
  1588. }
  1589. if (typeof constructor === 'undefined') {
  1590. throw new TypeError('You must supply a subclass constructor.');
  1591. }
  1592. if (typeof constructor !== 'function') {
  1593. throw new TypeError('You must register the subclass constructor. ' + 'Did you attempt to register an instance of the subclass?');
  1594. }
  1595. classMap[className] = constructor;
  1596. if (!constructor.className) {
  1597. constructor.className = className;
  1598. }
  1599. }
  1600. }, {
  1601. key: "unregisterSubclass",
  1602. value: function (className) {
  1603. if (typeof className !== 'string') {
  1604. throw new TypeError('The first argument must be a valid class name.');
  1605. }
  1606. delete classMap[className];
  1607. }
  1608. }, {
  1609. key: "extend",
  1610. value: function (className, protoProps, classProps) {
  1611. if (typeof className !== 'string') {
  1612. if (className && typeof className.className === 'string') {
  1613. return ParseObject.extend(className.className, className, protoProps);
  1614. } else {
  1615. throw new Error("Parse.Object.extend's first argument should be the className.");
  1616. }
  1617. }
  1618. var adjustedClassName = className;
  1619. if (adjustedClassName === 'User' && _CoreManager.default.get('PERFORM_USER_REWRITE')) {
  1620. adjustedClassName = '_User';
  1621. }
  1622. var parentProto = ParseObject.prototype;
  1623. if (this.hasOwnProperty('__super__') && this.__super__) {
  1624. parentProto = this.prototype;
  1625. } else if (classMap[adjustedClassName]) {
  1626. parentProto = classMap[adjustedClassName].prototype;
  1627. }
  1628. var ParseObjectSubclass = function (attributes, options) {
  1629. this.className = adjustedClassName;
  1630. this._objCount = objectCount++;
  1631. if (typeof this.initialize === 'function') {
  1632. this.initialize.apply(this, arguments);
  1633. }
  1634. if (attributes && typeof attributes === 'object') {
  1635. if (!this.set(attributes || {}, options)) {
  1636. throw new Error("Can't create an invalid Parse Object");
  1637. }
  1638. }
  1639. };
  1640. ParseObjectSubclass.className = adjustedClassName;
  1641. ParseObjectSubclass.__super__ = parentProto;
  1642. ParseObjectSubclass.prototype = Object.create(parentProto, {
  1643. constructor: {
  1644. value: ParseObjectSubclass,
  1645. enumerable: false,
  1646. writable: true,
  1647. configurable: true
  1648. }
  1649. });
  1650. if (protoProps) {
  1651. for (var prop in protoProps) {
  1652. if (prop !== 'className') {
  1653. Object.defineProperty(ParseObjectSubclass.prototype, prop, {
  1654. value: protoProps[prop],
  1655. enumerable: false,
  1656. writable: true,
  1657. configurable: true
  1658. });
  1659. }
  1660. }
  1661. }
  1662. if (classProps) {
  1663. for (var _prop in classProps) {
  1664. if (_prop !== 'className') {
  1665. Object.defineProperty(ParseObjectSubclass, _prop, {
  1666. value: classProps[_prop],
  1667. enumerable: false,
  1668. writable: true,
  1669. configurable: true
  1670. });
  1671. }
  1672. }
  1673. }
  1674. ParseObjectSubclass.extend = function (name, protoProps, classProps) {
  1675. if (typeof name === 'string') {
  1676. return ParseObject.extend.call(ParseObjectSubclass, name, protoProps, classProps);
  1677. }
  1678. return ParseObject.extend.call(ParseObjectSubclass, adjustedClassName, name, protoProps);
  1679. };
  1680. ParseObjectSubclass.createWithoutData = ParseObject.createWithoutData;
  1681. classMap[adjustedClassName] = ParseObjectSubclass;
  1682. return ParseObjectSubclass;
  1683. }
  1684. }, {
  1685. key: "enableSingleInstance",
  1686. value: function () {
  1687. singleInstance = true;
  1688. _CoreManager.default.setObjectStateController(SingleInstanceStateController);
  1689. }
  1690. }, {
  1691. key: "disableSingleInstance",
  1692. value: function () {
  1693. singleInstance = false;
  1694. _CoreManager.default.setObjectStateController(UniqueInstanceStateController);
  1695. }
  1696. }, {
  1697. key: "pinAll",
  1698. value: function (objects) {
  1699. var localDatastore = _CoreManager.default.getLocalDatastore();
  1700. if (!localDatastore.isEnabled) {
  1701. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1702. }
  1703. return ParseObject.pinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, objects);
  1704. }
  1705. }, {
  1706. key: "pinAllWithName",
  1707. value: function (name, objects) {
  1708. var localDatastore = _CoreManager.default.getLocalDatastore();
  1709. if (!localDatastore.isEnabled) {
  1710. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1711. }
  1712. return localDatastore._handlePinAllWithName(name, objects);
  1713. }
  1714. }, {
  1715. key: "unPinAll",
  1716. value: function (objects) {
  1717. var localDatastore = _CoreManager.default.getLocalDatastore();
  1718. if (!localDatastore.isEnabled) {
  1719. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1720. }
  1721. return ParseObject.unPinAllWithName(_LocalDatastoreUtils.DEFAULT_PIN, objects);
  1722. }
  1723. }, {
  1724. key: "unPinAllWithName",
  1725. value: function (name, objects) {
  1726. var localDatastore = _CoreManager.default.getLocalDatastore();
  1727. if (!localDatastore.isEnabled) {
  1728. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1729. }
  1730. return localDatastore._handleUnPinAllWithName(name, objects);
  1731. }
  1732. }, {
  1733. key: "unPinAllObjects",
  1734. value: function () {
  1735. var localDatastore = _CoreManager.default.getLocalDatastore();
  1736. if (!localDatastore.isEnabled) {
  1737. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1738. }
  1739. return localDatastore.unPinWithName(_LocalDatastoreUtils.DEFAULT_PIN);
  1740. }
  1741. }, {
  1742. key: "unPinAllObjectsWithName",
  1743. value: function (name) {
  1744. var localDatastore = _CoreManager.default.getLocalDatastore();
  1745. if (!localDatastore.isEnabled) {
  1746. return Promise.reject('Parse.enableLocalDatastore() must be called first');
  1747. }
  1748. return localDatastore.unPinWithName(_LocalDatastoreUtils.PIN_PREFIX + name);
  1749. }
  1750. }]);
  1751. return ParseObject;
  1752. }();
  1753. var DefaultController = {
  1754. fetch: function (target, forceFetch, options) {
  1755. var localDatastore = _CoreManager.default.getLocalDatastore();
  1756. if (Array.isArray(target)) {
  1757. if (target.length < 1) {
  1758. return Promise.resolve([]);
  1759. }
  1760. var objs = [];
  1761. var ids = [];
  1762. var className = null;
  1763. var results = [];
  1764. var error = null;
  1765. target.forEach(function (el) {
  1766. if (error) {
  1767. return;
  1768. }
  1769. if (!className) {
  1770. className = el.className;
  1771. }
  1772. if (className !== el.className) {
  1773. error = new _ParseError.default(_ParseError.default.INVALID_CLASS_NAME, 'All objects should be of the same class');
  1774. }
  1775. if (!el.id) {
  1776. error = new _ParseError.default(_ParseError.default.MISSING_OBJECT_ID, 'All objects must have an ID');
  1777. }
  1778. if (forceFetch || !el.isDataAvailable()) {
  1779. ids.push(el.id);
  1780. objs.push(el);
  1781. }
  1782. results.push(el);
  1783. });
  1784. if (error) {
  1785. return Promise.reject(error);
  1786. }
  1787. var query = new _ParseQuery.default(className);
  1788. query.containedIn('objectId', ids);
  1789. if (options && options.include) {
  1790. query.include(options.include);
  1791. }
  1792. query._limit = ids.length;
  1793. return query.find(options).then(function (objects) {
  1794. var idMap, i, obj, _i, _obj, id, _iterator2, _step2, object;
  1795. return _regeneratorRuntime().async(function (_context6) {
  1796. while (1) {
  1797. switch (_context6.prev = _context6.next) {
  1798. case 0:
  1799. idMap = {};
  1800. objects.forEach(function (o) {
  1801. idMap[o.id] = o;
  1802. });
  1803. i = 0;
  1804. case 3:
  1805. if (!(i < objs.length)) {
  1806. _context6.next = 11;
  1807. break;
  1808. }
  1809. obj = objs[i];
  1810. if (!(!obj || !obj.id || !idMap[obj.id])) {
  1811. _context6.next = 8;
  1812. break;
  1813. }
  1814. if (!forceFetch) {
  1815. _context6.next = 8;
  1816. break;
  1817. }
  1818. return _context6.abrupt("return", Promise.reject(new _ParseError.default(_ParseError.default.OBJECT_NOT_FOUND, 'All objects must exist on the server.')));
  1819. case 8:
  1820. i++;
  1821. _context6.next = 3;
  1822. break;
  1823. case 11:
  1824. if (!singleInstance) {
  1825. for (_i = 0; _i < results.length; _i++) {
  1826. _obj = results[_i];
  1827. if (_obj && _obj.id && idMap[_obj.id]) {
  1828. id = _obj.id;
  1829. _obj._finishFetch(idMap[id].toJSON());
  1830. results[_i] = idMap[id];
  1831. }
  1832. }
  1833. }
  1834. _iterator2 = _createForOfIteratorHelperLoose(results);
  1835. case 13:
  1836. if ((_step2 = _iterator2()).done) {
  1837. _context6.next = 19;
  1838. break;
  1839. }
  1840. object = _step2.value;
  1841. _context6.next = 17;
  1842. return _regeneratorRuntime().awrap(localDatastore._updateObjectIfPinned(object));
  1843. case 17:
  1844. _context6.next = 13;
  1845. break;
  1846. case 19:
  1847. return _context6.abrupt("return", Promise.resolve(results));
  1848. case 20:
  1849. case "end":
  1850. return _context6.stop();
  1851. }
  1852. }
  1853. }, null, null, null, Promise);
  1854. });
  1855. } else if (target instanceof ParseObject) {
  1856. if (!target.id) {
  1857. return Promise.reject(new _ParseError.default(_ParseError.default.MISSING_OBJECT_ID, 'Object does not have an ID'));
  1858. }
  1859. var RESTController = _CoreManager.default.getRESTController();
  1860. var params = {};
  1861. if (options && options.include) {
  1862. params.include = options.include.join();
  1863. }
  1864. return RESTController.request('GET', 'classes/' + target.className + '/' + target._getId(), params, options).then(function (response) {
  1865. return _regeneratorRuntime().async(function (_context7) {
  1866. while (1) {
  1867. switch (_context7.prev = _context7.next) {
  1868. case 0:
  1869. target._clearPendingOps();
  1870. target._clearServerData();
  1871. target._finishFetch(response);
  1872. _context7.next = 5;
  1873. return _regeneratorRuntime().awrap(localDatastore._updateObjectIfPinned(target));
  1874. case 5:
  1875. return _context7.abrupt("return", target);
  1876. case 6:
  1877. case "end":
  1878. return _context7.stop();
  1879. }
  1880. }
  1881. }, null, null, null, Promise);
  1882. });
  1883. }
  1884. return Promise.resolve();
  1885. },
  1886. destroy: function (target, options) {
  1887. var batchSize, localDatastore, RESTController, batches, deleteCompleted, errors;
  1888. return _regeneratorRuntime().async(function (_context10) {
  1889. while (1) {
  1890. switch (_context10.prev = _context10.next) {
  1891. case 0:
  1892. batchSize = options && options.batchSize ? options.batchSize : _CoreManager.default.get('REQUEST_BATCH_SIZE');
  1893. localDatastore = _CoreManager.default.getLocalDatastore();
  1894. RESTController = _CoreManager.default.getRESTController();
  1895. if (!Array.isArray(target)) {
  1896. _context10.next = 15;
  1897. break;
  1898. }
  1899. if (!(target.length < 1)) {
  1900. _context10.next = 6;
  1901. break;
  1902. }
  1903. return _context10.abrupt("return", Promise.resolve([]));
  1904. case 6:
  1905. batches = [[]];
  1906. target.forEach(function (obj) {
  1907. if (!obj.id) {
  1908. return;
  1909. }
  1910. batches[batches.length - 1].push(obj);
  1911. if (batches[batches.length - 1].length >= batchSize) {
  1912. batches.push([]);
  1913. }
  1914. });
  1915. if (batches[batches.length - 1].length === 0) {
  1916. batches.pop();
  1917. }
  1918. deleteCompleted = Promise.resolve();
  1919. errors = [];
  1920. batches.forEach(function (batch) {
  1921. deleteCompleted = deleteCompleted.then(function () {
  1922. return RESTController.request('POST', 'batch', {
  1923. requests: batch.map(function (obj) {
  1924. return {
  1925. method: 'DELETE',
  1926. path: getServerUrlPath() + 'classes/' + obj.className + '/' + obj._getId(),
  1927. body: {}
  1928. };
  1929. })
  1930. }, options).then(function (results) {
  1931. for (var i = 0; i < results.length; i++) {
  1932. if (results[i] && results[i].hasOwnProperty('error')) {
  1933. var err = new _ParseError.default(results[i].error.code, results[i].error.error);
  1934. err.object = batch[i];
  1935. errors.push(err);
  1936. }
  1937. }
  1938. });
  1939. });
  1940. });
  1941. return _context10.abrupt("return", deleteCompleted.then(function () {
  1942. var aggregate, _iterator3, _step3, object;
  1943. return _regeneratorRuntime().async(function (_context8) {
  1944. while (1) {
  1945. switch (_context8.prev = _context8.next) {
  1946. case 0:
  1947. if (!errors.length) {
  1948. _context8.next = 4;
  1949. break;
  1950. }
  1951. aggregate = new _ParseError.default(_ParseError.default.AGGREGATE_ERROR);
  1952. aggregate.errors = errors;
  1953. return _context8.abrupt("return", Promise.reject(aggregate));
  1954. case 4:
  1955. _iterator3 = _createForOfIteratorHelperLoose(target);
  1956. case 5:
  1957. if ((_step3 = _iterator3()).done) {
  1958. _context8.next = 11;
  1959. break;
  1960. }
  1961. object = _step3.value;
  1962. _context8.next = 9;
  1963. return _regeneratorRuntime().awrap(localDatastore._destroyObjectIfPinned(object));
  1964. case 9:
  1965. _context8.next = 5;
  1966. break;
  1967. case 11:
  1968. return _context8.abrupt("return", Promise.resolve(target));
  1969. case 12:
  1970. case "end":
  1971. return _context8.stop();
  1972. }
  1973. }
  1974. }, null, null, null, Promise);
  1975. }));
  1976. case 15:
  1977. if (!(target instanceof ParseObject)) {
  1978. _context10.next = 17;
  1979. break;
  1980. }
  1981. return _context10.abrupt("return", RESTController.request('DELETE', 'classes/' + target.className + '/' + target._getId(), {}, options).then(function () {
  1982. return _regeneratorRuntime().async(function (_context9) {
  1983. while (1) {
  1984. switch (_context9.prev = _context9.next) {
  1985. case 0:
  1986. _context9.next = 2;
  1987. return _regeneratorRuntime().awrap(localDatastore._destroyObjectIfPinned(target));
  1988. case 2:
  1989. return _context9.abrupt("return", Promise.resolve(target));
  1990. case 3:
  1991. case "end":
  1992. return _context9.stop();
  1993. }
  1994. }
  1995. }, null, null, null, Promise);
  1996. }));
  1997. case 17:
  1998. return _context10.abrupt("return", Promise.resolve(target));
  1999. case 18:
  2000. case "end":
  2001. return _context10.stop();
  2002. }
  2003. }
  2004. }, null, null, null, Promise);
  2005. },
  2006. save: function (target, options) {
  2007. var batchSize = options && options.batchSize ? options.batchSize : _CoreManager.default.get('REQUEST_BATCH_SIZE');
  2008. var localDatastore = _CoreManager.default.getLocalDatastore();
  2009. var mapIdForPin = {};
  2010. var RESTController = _CoreManager.default.getRESTController();
  2011. var stateController = _CoreManager.default.getObjectStateController();
  2012. var allowCustomObjectId = _CoreManager.default.get('ALLOW_CUSTOM_OBJECT_ID');
  2013. options = options || {};
  2014. options.returnStatus = options.returnStatus || true;
  2015. if (Array.isArray(target)) {
  2016. if (target.length < 1) {
  2017. return Promise.resolve([]);
  2018. }
  2019. var unsaved = target.concat();
  2020. for (var i = 0; i < target.length; i++) {
  2021. if (target[i] instanceof ParseObject) {
  2022. unsaved = unsaved.concat((0, _unsavedChildren.default)(target[i], true));
  2023. }
  2024. }
  2025. unsaved = (0, _unique.default)(unsaved);
  2026. var filesSaved = [];
  2027. var pending = [];
  2028. unsaved.forEach(function (el) {
  2029. if (el instanceof _ParseFile.default) {
  2030. filesSaved.push(el.save(options));
  2031. } else if (el instanceof ParseObject) {
  2032. pending.push(el);
  2033. }
  2034. });
  2035. return Promise.all(filesSaved).then(function () {
  2036. var objectError = null;
  2037. return (0, _promiseUtils.continueWhile)(function () {
  2038. return pending.length > 0;
  2039. }, function () {
  2040. var batch = [];
  2041. var nextPending = [];
  2042. pending.forEach(function (el) {
  2043. if (allowCustomObjectId && Object.prototype.hasOwnProperty.call(el, 'id') && !el.id) {
  2044. throw new _ParseError.default(_ParseError.default.MISSING_OBJECT_ID, 'objectId must not be empty or null');
  2045. }
  2046. if (batch.length < batchSize && (0, _canBeSerialized.default)(el)) {
  2047. batch.push(el);
  2048. } else {
  2049. nextPending.push(el);
  2050. }
  2051. });
  2052. pending = nextPending;
  2053. if (batch.length < 1) {
  2054. return Promise.reject(new _ParseError.default(_ParseError.default.OTHER_CAUSE, 'Tried to save a batch with a cycle.'));
  2055. }
  2056. var batchReturned = new _promiseUtils.resolvingPromise();
  2057. var batchReady = [];
  2058. var batchTasks = [];
  2059. batch.forEach(function (obj, index) {
  2060. var ready = new _promiseUtils.resolvingPromise();
  2061. batchReady.push(ready);
  2062. var task = function task() {
  2063. ready.resolve();
  2064. return batchReturned.then(function (responses) {
  2065. if (responses[index].hasOwnProperty('success')) {
  2066. var objectId = responses[index].success.objectId;
  2067. var status = responses[index]._status;
  2068. delete responses[index]._status;
  2069. mapIdForPin[objectId] = obj._localId;
  2070. obj._handleSaveResponse(responses[index].success, status);
  2071. } else {
  2072. if (!objectError && responses[index].hasOwnProperty('error')) {
  2073. var serverError = responses[index].error;
  2074. objectError = new _ParseError.default(serverError.code, serverError.error);
  2075. pending = [];
  2076. }
  2077. obj._handleSaveError();
  2078. }
  2079. });
  2080. };
  2081. stateController.pushPendingState(obj._getStateIdentifier());
  2082. batchTasks.push(stateController.enqueueTask(obj._getStateIdentifier(), task));
  2083. });
  2084. (0, _promiseUtils.when)(batchReady).then(function () {
  2085. return RESTController.request('POST', 'batch', {
  2086. requests: batch.map(function (obj) {
  2087. var params = obj._getSaveParams();
  2088. params.path = getServerUrlPath() + params.path;
  2089. return params;
  2090. })
  2091. }, options);
  2092. }).then(batchReturned.resolve, function (error) {
  2093. batchReturned.reject(new _ParseError.default(_ParseError.default.INCORRECT_TYPE, error.message));
  2094. });
  2095. return (0, _promiseUtils.when)(batchTasks);
  2096. }).then(function _callee5() {
  2097. var _iterator4, _step4, object;
  2098. return _regeneratorRuntime().async(function _callee5$(_context11) {
  2099. while (1) {
  2100. switch (_context11.prev = _context11.next) {
  2101. case 0:
  2102. if (!objectError) {
  2103. _context11.next = 2;
  2104. break;
  2105. }
  2106. return _context11.abrupt("return", Promise.reject(objectError));
  2107. case 2:
  2108. _iterator4 = _createForOfIteratorHelperLoose(target);
  2109. case 3:
  2110. if ((_step4 = _iterator4()).done) {
  2111. _context11.next = 11;
  2112. break;
  2113. }
  2114. object = _step4.value;
  2115. _context11.next = 7;
  2116. return _regeneratorRuntime().awrap(localDatastore._updateLocalIdForObject(mapIdForPin[object.id], object));
  2117. case 7:
  2118. _context11.next = 9;
  2119. return _regeneratorRuntime().awrap(localDatastore._updateObjectIfPinned(object));
  2120. case 9:
  2121. _context11.next = 3;
  2122. break;
  2123. case 11:
  2124. return _context11.abrupt("return", Promise.resolve(target));
  2125. case 12:
  2126. case "end":
  2127. return _context11.stop();
  2128. }
  2129. }
  2130. }, null, null, null, Promise);
  2131. });
  2132. });
  2133. } else if (target instanceof ParseObject) {
  2134. if (allowCustomObjectId && Object.prototype.hasOwnProperty.call(target, 'id') && !target.id) {
  2135. throw new _ParseError.default(_ParseError.default.MISSING_OBJECT_ID, 'objectId must not be empty or null');
  2136. }
  2137. target._getId();
  2138. var localId = target._localId;
  2139. var targetCopy = target;
  2140. var task = function task() {
  2141. var params = targetCopy._getSaveParams();
  2142. return RESTController.request(params.method, params.path, params.body, options).then(function (response) {
  2143. var status = response._status;
  2144. delete response._status;
  2145. targetCopy._handleSaveResponse(response, status);
  2146. }, function (error) {
  2147. targetCopy._handleSaveError();
  2148. return Promise.reject(error);
  2149. });
  2150. };
  2151. stateController.pushPendingState(target._getStateIdentifier());
  2152. return stateController.enqueueTask(target._getStateIdentifier(), task).then(function _callee6() {
  2153. return _regeneratorRuntime().async(function _callee6$(_context12) {
  2154. while (1) {
  2155. switch (_context12.prev = _context12.next) {
  2156. case 0:
  2157. _context12.next = 2;
  2158. return _regeneratorRuntime().awrap(localDatastore._updateLocalIdForObject(localId, target));
  2159. case 2:
  2160. _context12.next = 4;
  2161. return _regeneratorRuntime().awrap(localDatastore._updateObjectIfPinned(target));
  2162. case 4:
  2163. return _context12.abrupt("return", target);
  2164. case 5:
  2165. case "end":
  2166. return _context12.stop();
  2167. }
  2168. }
  2169. }, null, null, null, Promise);
  2170. }, function (error) {
  2171. return Promise.reject(error);
  2172. });
  2173. }
  2174. return Promise.resolve();
  2175. }
  2176. };
  2177. _CoreManager.default.setObjectController(DefaultController);
  2178. var _default = ParseObject;
  2179. exports.default = _default;