ParseOp.js 17 KB

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