ParseOp.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.UnsetOp = exports.SetOp = exports.RemoveOp = exports.RelationOp = exports.Op = exports.IncrementOp = exports.AddUniqueOp = exports.AddOp = void 0;
  6. exports.opFromJSON = opFromJSON;
  7. var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
  8. var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  9. var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  10. var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  11. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  12. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  13. var _arrayContainsObject = _interopRequireDefault(require("./arrayContainsObject"));
  14. var _decode = _interopRequireDefault(require("./decode"));
  15. var _encode = _interopRequireDefault(require("./encode"));
  16. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  17. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  18. var _unique = _interopRequireDefault(require("./unique"));
  19. function _createSuper(Derived) {
  20. var hasNativeReflectConstruct = _isNativeReflectConstruct();
  21. return function () {
  22. var Super = (0, _getPrototypeOf2.default)(Derived),
  23. result;
  24. if (hasNativeReflectConstruct) {
  25. var NewTarget = (0, _getPrototypeOf2.default)(this).constructor;
  26. result = Reflect.construct(Super, arguments, NewTarget);
  27. } else {
  28. result = Super.apply(this, arguments);
  29. }
  30. return (0, _possibleConstructorReturn2.default)(this, result);
  31. };
  32. }
  33. function _isNativeReflectConstruct() {
  34. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  35. if (Reflect.construct.sham) return false;
  36. if (typeof Proxy === "function") return true;
  37. try {
  38. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  39. return true;
  40. } catch (e) {
  41. return false;
  42. }
  43. }
  44. function opFromJSON(json) {
  45. if (!json || !json.__op) {
  46. return null;
  47. }
  48. switch (json.__op) {
  49. case 'Delete':
  50. return new UnsetOp();
  51. case 'Increment':
  52. return new IncrementOp(json.amount);
  53. case 'Add':
  54. return new AddOp((0, _decode.default)(json.objects));
  55. case 'AddUnique':
  56. return new AddUniqueOp((0, _decode.default)(json.objects));
  57. case 'Remove':
  58. return new RemoveOp((0, _decode.default)(json.objects));
  59. case 'AddRelation':
  60. {
  61. var toAdd = (0, _decode.default)(json.objects);
  62. if (!Array.isArray(toAdd)) {
  63. return new RelationOp([], []);
  64. }
  65. return new RelationOp(toAdd, []);
  66. }
  67. case 'RemoveRelation':
  68. {
  69. var toRemove = (0, _decode.default)(json.objects);
  70. if (!Array.isArray(toRemove)) {
  71. return new RelationOp([], []);
  72. }
  73. return new RelationOp([], toRemove);
  74. }
  75. case 'Batch':
  76. {
  77. var _toAdd = [];
  78. var _toRemove = [];
  79. for (var i = 0; i < json.ops.length; i++) {
  80. if (json.ops[i].__op === 'AddRelation') {
  81. _toAdd = _toAdd.concat((0, _decode.default)(json.ops[i].objects));
  82. } else if (json.ops[i].__op === 'RemoveRelation') {
  83. _toRemove = _toRemove.concat((0, _decode.default)(json.ops[i].objects));
  84. }
  85. }
  86. return new RelationOp(_toAdd, _toRemove);
  87. }
  88. default:
  89. return null;
  90. }
  91. }
  92. var Op = function () {
  93. function Op() {
  94. (0, _classCallCheck2.default)(this, Op);
  95. }
  96. (0, _createClass2.default)(Op, [{
  97. key: "applyTo",
  98. value: function () {}
  99. }, {
  100. key: "mergeWith",
  101. value: function () {}
  102. }, {
  103. key: "toJSON",
  104. value: function () {}
  105. }]);
  106. return Op;
  107. }();
  108. exports.Op = Op;
  109. var SetOp = function (_Op) {
  110. (0, _inherits2.default)(SetOp, _Op);
  111. var _super = _createSuper(SetOp);
  112. function SetOp(value) {
  113. var _this;
  114. (0, _classCallCheck2.default)(this, SetOp);
  115. _this = _super.call(this);
  116. _this._value = value;
  117. return _this;
  118. }
  119. (0, _createClass2.default)(SetOp, [{
  120. key: "applyTo",
  121. value: function () {
  122. return this._value;
  123. }
  124. }, {
  125. key: "mergeWith",
  126. value: function () {
  127. return new SetOp(this._value);
  128. }
  129. }, {
  130. key: "toJSON",
  131. value: function (offline) {
  132. return (0, _encode.default)(this._value, false, true, undefined, offline);
  133. }
  134. }]);
  135. return SetOp;
  136. }(Op);
  137. exports.SetOp = SetOp;
  138. var UnsetOp = function (_Op2) {
  139. (0, _inherits2.default)(UnsetOp, _Op2);
  140. var _super2 = _createSuper(UnsetOp);
  141. function UnsetOp() {
  142. (0, _classCallCheck2.default)(this, UnsetOp);
  143. return _super2.apply(this, arguments);
  144. }
  145. (0, _createClass2.default)(UnsetOp, [{
  146. key: "applyTo",
  147. value: function () {
  148. return undefined;
  149. }
  150. }, {
  151. key: "mergeWith",
  152. value: function () {
  153. return new UnsetOp();
  154. }
  155. }, {
  156. key: "toJSON",
  157. value: function () {
  158. return {
  159. __op: 'Delete'
  160. };
  161. }
  162. }]);
  163. return UnsetOp;
  164. }(Op);
  165. exports.UnsetOp = UnsetOp;
  166. var IncrementOp = function (_Op3) {
  167. (0, _inherits2.default)(IncrementOp, _Op3);
  168. var _super3 = _createSuper(IncrementOp);
  169. function IncrementOp(amount) {
  170. var _this2;
  171. (0, _classCallCheck2.default)(this, IncrementOp);
  172. _this2 = _super3.call(this);
  173. if (typeof amount !== 'number') {
  174. throw new TypeError('Increment Op must be initialized with a numeric amount.');
  175. }
  176. _this2._amount = amount;
  177. return _this2;
  178. }
  179. (0, _createClass2.default)(IncrementOp, [{
  180. key: "applyTo",
  181. value: function (value) {
  182. if (typeof value === 'undefined') {
  183. return this._amount;
  184. }
  185. if (typeof value !== 'number') {
  186. throw new TypeError('Cannot increment a non-numeric value.');
  187. }
  188. return this._amount + value;
  189. }
  190. }, {
  191. key: "mergeWith",
  192. value: function (previous) {
  193. if (!previous) {
  194. return this;
  195. }
  196. if (previous instanceof SetOp) {
  197. return new SetOp(this.applyTo(previous._value));
  198. }
  199. if (previous instanceof UnsetOp) {
  200. return new SetOp(this._amount);
  201. }
  202. if (previous instanceof IncrementOp) {
  203. return new IncrementOp(this.applyTo(previous._amount));
  204. }
  205. throw new Error('Cannot merge Increment Op with the previous Op');
  206. }
  207. }, {
  208. key: "toJSON",
  209. value: function () {
  210. return {
  211. __op: 'Increment',
  212. amount: this._amount
  213. };
  214. }
  215. }]);
  216. return IncrementOp;
  217. }(Op);
  218. exports.IncrementOp = IncrementOp;
  219. var AddOp = function (_Op4) {
  220. (0, _inherits2.default)(AddOp, _Op4);
  221. var _super4 = _createSuper(AddOp);
  222. function AddOp(value) {
  223. var _this3;
  224. (0, _classCallCheck2.default)(this, AddOp);
  225. _this3 = _super4.call(this);
  226. _this3._value = Array.isArray(value) ? value : [value];
  227. return _this3;
  228. }
  229. (0, _createClass2.default)(AddOp, [{
  230. key: "applyTo",
  231. value: function (value) {
  232. if (value == null) {
  233. return this._value;
  234. }
  235. if (Array.isArray(value)) {
  236. return value.concat(this._value);
  237. }
  238. throw new Error('Cannot add elements to a non-array value');
  239. }
  240. }, {
  241. key: "mergeWith",
  242. value: function (previous) {
  243. if (!previous) {
  244. return this;
  245. }
  246. if (previous instanceof SetOp) {
  247. return new SetOp(this.applyTo(previous._value));
  248. }
  249. if (previous instanceof UnsetOp) {
  250. return new SetOp(this._value);
  251. }
  252. if (previous instanceof AddOp) {
  253. return new AddOp(this.applyTo(previous._value));
  254. }
  255. throw new Error('Cannot merge Add Op with the previous Op');
  256. }
  257. }, {
  258. key: "toJSON",
  259. value: function () {
  260. return {
  261. __op: 'Add',
  262. objects: (0, _encode.default)(this._value, false, true)
  263. };
  264. }
  265. }]);
  266. return AddOp;
  267. }(Op);
  268. exports.AddOp = AddOp;
  269. var AddUniqueOp = function (_Op5) {
  270. (0, _inherits2.default)(AddUniqueOp, _Op5);
  271. var _super5 = _createSuper(AddUniqueOp);
  272. function AddUniqueOp(value) {
  273. var _this4;
  274. (0, _classCallCheck2.default)(this, AddUniqueOp);
  275. _this4 = _super5.call(this);
  276. _this4._value = (0, _unique.default)(Array.isArray(value) ? value : [value]);
  277. return _this4;
  278. }
  279. (0, _createClass2.default)(AddUniqueOp, [{
  280. key: "applyTo",
  281. value: function (value) {
  282. if (value == null) {
  283. return this._value || [];
  284. }
  285. if (Array.isArray(value)) {
  286. var toAdd = [];
  287. this._value.forEach(function (v) {
  288. if (v instanceof _ParseObject.default) {
  289. if (!(0, _arrayContainsObject.default)(value, v)) {
  290. toAdd.push(v);
  291. }
  292. } else {
  293. if (value.indexOf(v) < 0) {
  294. toAdd.push(v);
  295. }
  296. }
  297. });
  298. return value.concat(toAdd);
  299. }
  300. throw new Error('Cannot add elements to a non-array value');
  301. }
  302. }, {
  303. key: "mergeWith",
  304. value: function (previous) {
  305. if (!previous) {
  306. return this;
  307. }
  308. if (previous instanceof SetOp) {
  309. return new SetOp(this.applyTo(previous._value));
  310. }
  311. if (previous instanceof UnsetOp) {
  312. return new SetOp(this._value);
  313. }
  314. if (previous instanceof AddUniqueOp) {
  315. return new AddUniqueOp(this.applyTo(previous._value));
  316. }
  317. throw new Error('Cannot merge AddUnique Op with the previous Op');
  318. }
  319. }, {
  320. key: "toJSON",
  321. value: function () {
  322. return {
  323. __op: 'AddUnique',
  324. objects: (0, _encode.default)(this._value, false, true)
  325. };
  326. }
  327. }]);
  328. return AddUniqueOp;
  329. }(Op);
  330. exports.AddUniqueOp = AddUniqueOp;
  331. var RemoveOp = function (_Op6) {
  332. (0, _inherits2.default)(RemoveOp, _Op6);
  333. var _super6 = _createSuper(RemoveOp);
  334. function RemoveOp(value) {
  335. var _this5;
  336. (0, _classCallCheck2.default)(this, RemoveOp);
  337. _this5 = _super6.call(this);
  338. _this5._value = (0, _unique.default)(Array.isArray(value) ? value : [value]);
  339. return _this5;
  340. }
  341. (0, _createClass2.default)(RemoveOp, [{
  342. key: "applyTo",
  343. value: function (value) {
  344. if (value == null) {
  345. return [];
  346. }
  347. if (Array.isArray(value)) {
  348. var removed = value.concat([]);
  349. for (var i = 0; i < this._value.length; i++) {
  350. var index = removed.indexOf(this._value[i]);
  351. while (index > -1) {
  352. removed.splice(index, 1);
  353. index = removed.indexOf(this._value[i]);
  354. }
  355. if (this._value[i] instanceof _ParseObject.default && this._value[i].id) {
  356. for (var j = 0; j < removed.length; j++) {
  357. if (removed[j] instanceof _ParseObject.default && this._value[i].id === removed[j].id) {
  358. removed.splice(j, 1);
  359. j--;
  360. }
  361. }
  362. }
  363. }
  364. return removed;
  365. }
  366. throw new Error('Cannot remove elements from a non-array value');
  367. }
  368. }, {
  369. key: "mergeWith",
  370. value: function (previous) {
  371. if (!previous) {
  372. return this;
  373. }
  374. if (previous instanceof SetOp) {
  375. return new SetOp(this.applyTo(previous._value));
  376. }
  377. if (previous instanceof UnsetOp) {
  378. return new UnsetOp();
  379. }
  380. if (previous instanceof RemoveOp) {
  381. var uniques = previous._value.concat([]);
  382. for (var i = 0; i < this._value.length; i++) {
  383. if (this._value[i] instanceof _ParseObject.default) {
  384. if (!(0, _arrayContainsObject.default)(uniques, this._value[i])) {
  385. uniques.push(this._value[i]);
  386. }
  387. } else {
  388. if (uniques.indexOf(this._value[i]) < 0) {
  389. uniques.push(this._value[i]);
  390. }
  391. }
  392. }
  393. return new RemoveOp(uniques);
  394. }
  395. throw new Error('Cannot merge Remove Op with the previous Op');
  396. }
  397. }, {
  398. key: "toJSON",
  399. value: function () {
  400. return {
  401. __op: 'Remove',
  402. objects: (0, _encode.default)(this._value, false, true)
  403. };
  404. }
  405. }]);
  406. return RemoveOp;
  407. }(Op);
  408. exports.RemoveOp = RemoveOp;
  409. var RelationOp = function (_Op7) {
  410. (0, _inherits2.default)(RelationOp, _Op7);
  411. var _super7 = _createSuper(RelationOp);
  412. function RelationOp(adds, removes) {
  413. var _this6;
  414. (0, _classCallCheck2.default)(this, RelationOp);
  415. _this6 = _super7.call(this);
  416. _this6._targetClassName = null;
  417. if (Array.isArray(adds)) {
  418. _this6.relationsToAdd = (0, _unique.default)(adds.map(_this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  419. }
  420. if (Array.isArray(removes)) {
  421. _this6.relationsToRemove = (0, _unique.default)(removes.map(_this6._extractId, (0, _assertThisInitialized2.default)(_this6)));
  422. }
  423. return _this6;
  424. }
  425. (0, _createClass2.default)(RelationOp, [{
  426. key: "_extractId",
  427. value: function (obj) {
  428. if (typeof obj === 'string') {
  429. return obj;
  430. }
  431. if (!obj.id) {
  432. throw new Error('You cannot add or remove an unsaved Parse Object from a relation');
  433. }
  434. if (!this._targetClassName) {
  435. this._targetClassName = obj.className;
  436. }
  437. if (this._targetClassName !== obj.className) {
  438. throw new Error("Tried to create a Relation with 2 different object types: " + this._targetClassName + " and " + obj.className + ".");
  439. }
  440. return obj.id;
  441. }
  442. }, {
  443. key: "applyTo",
  444. value: function (value, object, key) {
  445. if (!value) {
  446. if (!object || !key) {
  447. throw new Error('Cannot apply a RelationOp without either a previous value, or an object and a key');
  448. }
  449. var parent = new _ParseObject.default(object.className);
  450. if (object.id && object.id.indexOf('local') === 0) {
  451. parent._localId = object.id;
  452. } else if (object.id) {
  453. parent.id = object.id;
  454. }
  455. var relation = new _ParseRelation.default(parent, key);
  456. relation.targetClassName = this._targetClassName;
  457. return relation;
  458. }
  459. if (value instanceof _ParseRelation.default) {
  460. if (this._targetClassName) {
  461. if (value.targetClassName) {
  462. if (this._targetClassName !== value.targetClassName) {
  463. throw new Error("Related object must be a " + value.targetClassName + ", but a " + this._targetClassName + " was passed in.");
  464. }
  465. } else {
  466. value.targetClassName = this._targetClassName;
  467. }
  468. }
  469. return value;
  470. }
  471. throw new Error('Relation cannot be applied to a non-relation field');
  472. }
  473. }, {
  474. key: "mergeWith",
  475. value: function (previous) {
  476. if (!previous) {
  477. return this;
  478. }
  479. if (previous instanceof UnsetOp) {
  480. throw new Error('You cannot modify a relation after deleting it.');
  481. }
  482. if (previous instanceof SetOp && previous._value instanceof _ParseRelation.default) {
  483. return this;
  484. }
  485. if (previous instanceof RelationOp) {
  486. if (previous._targetClassName && previous._targetClassName !== this._targetClassName) {
  487. throw new Error("Related object must be of class " + previous._targetClassName + ", but " + (this._targetClassName || 'null') + " was passed in.");
  488. }
  489. var newAdd = previous.relationsToAdd.concat([]);
  490. this.relationsToRemove.forEach(function (r) {
  491. var index = newAdd.indexOf(r);
  492. if (index > -1) {
  493. newAdd.splice(index, 1);
  494. }
  495. });
  496. this.relationsToAdd.forEach(function (r) {
  497. var index = newAdd.indexOf(r);
  498. if (index < 0) {
  499. newAdd.push(r);
  500. }
  501. });
  502. var newRemove = previous.relationsToRemove.concat([]);
  503. this.relationsToAdd.forEach(function (r) {
  504. var index = newRemove.indexOf(r);
  505. if (index > -1) {
  506. newRemove.splice(index, 1);
  507. }
  508. });
  509. this.relationsToRemove.forEach(function (r) {
  510. var index = newRemove.indexOf(r);
  511. if (index < 0) {
  512. newRemove.push(r);
  513. }
  514. });
  515. var newRelation = new RelationOp(newAdd, newRemove);
  516. newRelation._targetClassName = this._targetClassName;
  517. return newRelation;
  518. }
  519. throw new Error('Cannot merge Relation Op with the previous Op');
  520. }
  521. }, {
  522. key: "toJSON",
  523. value: function () {
  524. var _this7 = this;
  525. var idToPointer = function (id) {
  526. return {
  527. __type: 'Pointer',
  528. className: _this7._targetClassName,
  529. objectId: id
  530. };
  531. };
  532. var adds = null;
  533. var removes = null;
  534. var pointers = null;
  535. if (this.relationsToAdd.length > 0) {
  536. pointers = this.relationsToAdd.map(idToPointer);
  537. adds = {
  538. __op: 'AddRelation',
  539. objects: pointers
  540. };
  541. }
  542. if (this.relationsToRemove.length > 0) {
  543. pointers = this.relationsToRemove.map(idToPointer);
  544. removes = {
  545. __op: 'RemoveRelation',
  546. objects: pointers
  547. };
  548. }
  549. if (adds && removes) {
  550. return {
  551. __op: 'Batch',
  552. ops: [adds, removes]
  553. };
  554. }
  555. return adds || removes || {};
  556. }
  557. }]);
  558. return RelationOp;
  559. }(Op);
  560. exports.RelationOp = RelationOp;