ParseOp.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.UnsetOp = exports.SetOp = exports.RemoveOp = exports.RelationOp = exports.Op = exports.IncrementOp = exports.AddUniqueOp = exports.AddOp = void 0;
  8. exports.opFromJSON = opFromJSON;
  9. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
  10. var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
  11. var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
  12. var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/for-each"));
  13. var _indexOf = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/index-of"));
  14. var _splice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/splice"));
  15. var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/map"));
  16. var _arrayContainsObject = _interopRequireDefault(require("./arrayContainsObject"));
  17. var _decode = _interopRequireDefault(require("./decode"));
  18. var _encode = _interopRequireDefault(require("./encode"));
  19. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  20. var _ParseRelation = _interopRequireDefault(require("./ParseRelation"));
  21. var _unique = _interopRequireDefault(require("./unique"));
  22. function opFromJSON(json) {
  23. if (!json || !json.__op) {
  24. return null;
  25. }
  26. switch (json.__op) {
  27. case 'Delete':
  28. return new UnsetOp();
  29. case 'Increment':
  30. return new IncrementOp(json.amount);
  31. case 'Add':
  32. return new AddOp((0, _decode.default)(json.objects));
  33. case 'AddUnique':
  34. return new AddUniqueOp((0, _decode.default)(json.objects));
  35. case 'Remove':
  36. return new RemoveOp((0, _decode.default)(json.objects));
  37. case 'AddRelation':
  38. {
  39. const toAdd = (0, _decode.default)(json.objects);
  40. if (!(0, _isArray.default)(toAdd)) {
  41. return new RelationOp([], []);
  42. }
  43. return new RelationOp(toAdd, []);
  44. }
  45. case 'RemoveRelation':
  46. {
  47. const toRemove = (0, _decode.default)(json.objects);
  48. if (!(0, _isArray.default)(toRemove)) {
  49. return new RelationOp([], []);
  50. }
  51. return new RelationOp([], toRemove);
  52. }
  53. case 'Batch':
  54. {
  55. let toAdd = [];
  56. let toRemove = [];
  57. for (let i = 0; i < json.ops.length; i++) {
  58. if (json.ops[i].__op === 'AddRelation') {
  59. toAdd = (0, _concat.default)(toAdd).call(toAdd, (0, _decode.default)(json.ops[i].objects));
  60. } else if (json.ops[i].__op === 'RemoveRelation') {
  61. toRemove = (0, _concat.default)(toRemove).call(toRemove, (0, _decode.default)(json.ops[i].objects));
  62. }
  63. }
  64. return new RelationOp(toAdd, toRemove);
  65. }
  66. }
  67. return null;
  68. }
  69. class Op {
  70. // Empty parent class
  71. applyTo() {} /* eslint-disable-line @typescript-eslint/no-unused-vars */
  72. mergeWith() {} /* eslint-disable-line @typescript-eslint/no-unused-vars */
  73. toJSON() {} /* eslint-disable-line @typescript-eslint/no-unused-vars */
  74. }
  75. exports.Op = Op;
  76. class SetOp extends Op {
  77. constructor(value) {
  78. super();
  79. (0, _defineProperty2.default)(this, "_value", void 0);
  80. this._value = value;
  81. }
  82. applyTo() {
  83. return this._value;
  84. }
  85. mergeWith() {
  86. return new SetOp(this._value);
  87. }
  88. toJSON(offline) {
  89. return (0, _encode.default)(this._value, false, true, undefined, offline);
  90. }
  91. }
  92. exports.SetOp = SetOp;
  93. class UnsetOp extends Op {
  94. applyTo() {
  95. return undefined;
  96. }
  97. mergeWith() {
  98. return new UnsetOp();
  99. }
  100. toJSON() {
  101. return {
  102. __op: 'Delete'
  103. };
  104. }
  105. }
  106. exports.UnsetOp = UnsetOp;
  107. class IncrementOp extends Op {
  108. constructor(amount) {
  109. super();
  110. (0, _defineProperty2.default)(this, "_amount", void 0);
  111. if (typeof amount !== 'number') {
  112. throw new TypeError('Increment Op must be initialized with a numeric amount.');
  113. }
  114. this._amount = amount;
  115. }
  116. applyTo(value) {
  117. if (typeof value === 'undefined') {
  118. return this._amount;
  119. }
  120. if (typeof value !== 'number') {
  121. throw new TypeError('Cannot increment a non-numeric value.');
  122. }
  123. return this._amount + value;
  124. }
  125. mergeWith(previous) {
  126. if (!previous) {
  127. return this;
  128. }
  129. if (previous instanceof SetOp) {
  130. return new SetOp(this.applyTo(previous._value));
  131. }
  132. if (previous instanceof UnsetOp) {
  133. return new SetOp(this._amount);
  134. }
  135. if (previous instanceof IncrementOp) {
  136. return new IncrementOp(this.applyTo(previous._amount));
  137. }
  138. throw new Error('Cannot merge Increment Op with the previous Op');
  139. }
  140. toJSON() {
  141. return {
  142. __op: 'Increment',
  143. amount: this._amount
  144. };
  145. }
  146. }
  147. exports.IncrementOp = IncrementOp;
  148. class AddOp extends Op {
  149. constructor(value) {
  150. super();
  151. (0, _defineProperty2.default)(this, "_value", void 0);
  152. this._value = (0, _isArray.default)(value) ? value : [value];
  153. }
  154. applyTo(value) {
  155. if (value == null) {
  156. return this._value;
  157. }
  158. if ((0, _isArray.default)(value)) {
  159. return (0, _concat.default)(value).call(value, this._value);
  160. }
  161. throw new Error('Cannot add elements to a non-array value');
  162. }
  163. mergeWith(previous) {
  164. if (!previous) {
  165. return this;
  166. }
  167. if (previous instanceof SetOp) {
  168. return new SetOp(this.applyTo(previous._value));
  169. }
  170. if (previous instanceof UnsetOp) {
  171. return new SetOp(this._value);
  172. }
  173. if (previous instanceof AddOp) {
  174. return new AddOp(this.applyTo(previous._value));
  175. }
  176. throw new Error('Cannot merge Add Op with the previous Op');
  177. }
  178. toJSON() {
  179. return {
  180. __op: 'Add',
  181. objects: (0, _encode.default)(this._value, false, true)
  182. };
  183. }
  184. }
  185. exports.AddOp = AddOp;
  186. class AddUniqueOp extends Op {
  187. constructor(value) {
  188. super();
  189. (0, _defineProperty2.default)(this, "_value", void 0);
  190. this._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  191. }
  192. applyTo(value) {
  193. if (value == null) {
  194. return this._value || [];
  195. }
  196. if ((0, _isArray.default)(value)) {
  197. var _context;
  198. const ParseObject = _CoreManager.default.getParseObject();
  199. const toAdd = [];
  200. (0, _forEach.default)(_context = this._value).call(_context, v => {
  201. if (v instanceof ParseObject) {
  202. if (!(0, _arrayContainsObject.default)(value, v)) {
  203. toAdd.push(v);
  204. }
  205. } else {
  206. if ((0, _indexOf.default)(value).call(value, v) < 0) {
  207. toAdd.push(v);
  208. }
  209. }
  210. });
  211. return (0, _concat.default)(value).call(value, toAdd);
  212. }
  213. throw new Error('Cannot add elements to a non-array value');
  214. }
  215. mergeWith(previous) {
  216. if (!previous) {
  217. return this;
  218. }
  219. if (previous instanceof SetOp) {
  220. return new SetOp(this.applyTo(previous._value));
  221. }
  222. if (previous instanceof UnsetOp) {
  223. return new SetOp(this._value);
  224. }
  225. if (previous instanceof AddUniqueOp) {
  226. return new AddUniqueOp(this.applyTo(previous._value));
  227. }
  228. throw new Error('Cannot merge AddUnique Op with the previous Op');
  229. }
  230. toJSON() {
  231. return {
  232. __op: 'AddUnique',
  233. objects: (0, _encode.default)(this._value, false, true)
  234. };
  235. }
  236. }
  237. exports.AddUniqueOp = AddUniqueOp;
  238. class RemoveOp extends Op {
  239. constructor(value) {
  240. super();
  241. (0, _defineProperty2.default)(this, "_value", void 0);
  242. this._value = (0, _unique.default)((0, _isArray.default)(value) ? value : [value]);
  243. }
  244. applyTo(value) {
  245. if (value == null) {
  246. return [];
  247. }
  248. if ((0, _isArray.default)(value)) {
  249. const ParseObject = _CoreManager.default.getParseObject();
  250. // var i = value.indexOf(this._value);
  251. const removed = (0, _concat.default)(value).call(value, []);
  252. for (let i = 0; i < this._value.length; i++) {
  253. let index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  254. while (index > -1) {
  255. (0, _splice.default)(removed).call(removed, index, 1);
  256. index = (0, _indexOf.default)(removed).call(removed, this._value[i]);
  257. }
  258. if (this._value[i] instanceof ParseObject && this._value[i].id) {
  259. for (let j = 0; j < removed.length; j++) {
  260. if (removed[j] instanceof ParseObject && this._value[i].id === removed[j].id) {
  261. (0, _splice.default)(removed).call(removed, j, 1);
  262. j--;
  263. }
  264. }
  265. }
  266. }
  267. return removed;
  268. }
  269. throw new Error('Cannot remove elements from a non-array value');
  270. }
  271. mergeWith(previous) {
  272. if (!previous) {
  273. return this;
  274. }
  275. if (previous instanceof SetOp) {
  276. return new SetOp(this.applyTo(previous._value));
  277. }
  278. if (previous instanceof UnsetOp) {
  279. return new UnsetOp();
  280. }
  281. if (previous instanceof RemoveOp) {
  282. var _context2;
  283. const ParseObject = _CoreManager.default.getParseObject();
  284. const uniques = (0, _concat.default)(_context2 = previous._value).call(_context2, []);
  285. for (let i = 0; i < this._value.length; i++) {
  286. if (this._value[i] instanceof ParseObject) {
  287. if (!(0, _arrayContainsObject.default)(uniques, this._value[i])) {
  288. uniques.push(this._value[i]);
  289. }
  290. } else {
  291. if ((0, _indexOf.default)(uniques).call(uniques, this._value[i]) < 0) {
  292. uniques.push(this._value[i]);
  293. }
  294. }
  295. }
  296. return new RemoveOp(uniques);
  297. }
  298. throw new Error('Cannot merge Remove Op with the previous Op');
  299. }
  300. toJSON() {
  301. return {
  302. __op: 'Remove',
  303. objects: (0, _encode.default)(this._value, false, true)
  304. };
  305. }
  306. }
  307. exports.RemoveOp = RemoveOp;
  308. class RelationOp extends Op {
  309. constructor(adds, removes) {
  310. super();
  311. (0, _defineProperty2.default)(this, "_targetClassName", void 0);
  312. (0, _defineProperty2.default)(this, "relationsToAdd", void 0);
  313. (0, _defineProperty2.default)(this, "relationsToRemove", void 0);
  314. this._targetClassName = null;
  315. if ((0, _isArray.default)(adds)) {
  316. this.relationsToAdd = (0, _unique.default)((0, _map.default)(adds).call(adds, this._extractId, this));
  317. }
  318. if ((0, _isArray.default)(removes)) {
  319. this.relationsToRemove = (0, _unique.default)((0, _map.default)(removes).call(removes, this._extractId, this));
  320. }
  321. }
  322. _extractId(obj) {
  323. if (typeof obj === 'string') {
  324. return obj;
  325. }
  326. if (!obj.id) {
  327. throw new Error('You cannot add or remove an unsaved Parse Object from a relation');
  328. }
  329. if (!this._targetClassName) {
  330. this._targetClassName = obj.className;
  331. }
  332. if (this._targetClassName !== obj.className) {
  333. throw new Error('Tried to create a Relation with 2 different object types: ' + this._targetClassName + ' and ' + obj.className + '.');
  334. }
  335. return obj.id;
  336. }
  337. applyTo(value, parent, key) {
  338. if (!value) {
  339. if (!parent || !key) {
  340. throw new Error('Cannot apply a RelationOp without either a previous value, or an object and a key');
  341. }
  342. const relation = new _ParseRelation.default(parent, key);
  343. relation.targetClassName = this._targetClassName;
  344. return relation;
  345. }
  346. if (value instanceof _ParseRelation.default) {
  347. if (this._targetClassName) {
  348. if (value.targetClassName) {
  349. if (this._targetClassName !== value.targetClassName) {
  350. throw new Error('Related object must be a ' + value.targetClassName + ', but a ' + this._targetClassName + ' was passed in.');
  351. }
  352. } else {
  353. value.targetClassName = this._targetClassName;
  354. }
  355. }
  356. return value;
  357. } else {
  358. throw new Error('Relation cannot be applied to a non-relation field');
  359. }
  360. }
  361. mergeWith(previous) {
  362. if (!previous) {
  363. return this;
  364. } else if (previous instanceof UnsetOp) {
  365. throw new Error('You cannot modify a relation after deleting it.');
  366. } else if (previous instanceof SetOp && previous._value instanceof _ParseRelation.default) {
  367. return this;
  368. } else if (previous instanceof RelationOp) {
  369. var _context3, _context4, _context5, _context6, _context7, _context8;
  370. if (previous._targetClassName && previous._targetClassName !== this._targetClassName) {
  371. throw new Error('Related object must be of class ' + previous._targetClassName + ', but ' + (this._targetClassName || 'null') + ' was passed in.');
  372. }
  373. const newAdd = (0, _concat.default)(_context3 = previous.relationsToAdd).call(_context3, []);
  374. (0, _forEach.default)(_context4 = this.relationsToRemove).call(_context4, r => {
  375. const index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  376. if (index > -1) {
  377. (0, _splice.default)(newAdd).call(newAdd, index, 1);
  378. }
  379. });
  380. (0, _forEach.default)(_context5 = this.relationsToAdd).call(_context5, r => {
  381. const index = (0, _indexOf.default)(newAdd).call(newAdd, r);
  382. if (index < 0) {
  383. newAdd.push(r);
  384. }
  385. });
  386. const newRemove = (0, _concat.default)(_context6 = previous.relationsToRemove).call(_context6, []);
  387. (0, _forEach.default)(_context7 = this.relationsToAdd).call(_context7, r => {
  388. const index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  389. if (index > -1) {
  390. (0, _splice.default)(newRemove).call(newRemove, index, 1);
  391. }
  392. });
  393. (0, _forEach.default)(_context8 = this.relationsToRemove).call(_context8, r => {
  394. const index = (0, _indexOf.default)(newRemove).call(newRemove, r);
  395. if (index < 0) {
  396. newRemove.push(r);
  397. }
  398. });
  399. const newRelation = new RelationOp(newAdd, newRemove);
  400. newRelation._targetClassName = this._targetClassName;
  401. return newRelation;
  402. }
  403. throw new Error('Cannot merge Relation Op with the previous Op');
  404. }
  405. toJSON() {
  406. const idToPointer = id => {
  407. return {
  408. __type: 'Pointer',
  409. className: this._targetClassName,
  410. objectId: id
  411. };
  412. };
  413. let pointers = null;
  414. let adds = null;
  415. let removes = null;
  416. if (this.relationsToAdd.length > 0) {
  417. var _context9;
  418. pointers = (0, _map.default)(_context9 = this.relationsToAdd).call(_context9, idToPointer);
  419. adds = {
  420. __op: 'AddRelation',
  421. objects: pointers
  422. };
  423. }
  424. if (this.relationsToRemove.length > 0) {
  425. var _context10;
  426. pointers = (0, _map.default)(_context10 = this.relationsToRemove).call(_context10, idToPointer);
  427. removes = {
  428. __op: 'RemoveRelation',
  429. objects: pointers
  430. };
  431. }
  432. if (adds && removes) {
  433. return {
  434. __op: 'Batch',
  435. ops: [adds, removes]
  436. };
  437. }
  438. return adds || removes || {};
  439. }
  440. }
  441. exports.RelationOp = RelationOp;
  442. _CoreManager.default.setParseOp({
  443. Op,
  444. opFromJSON,
  445. SetOp,
  446. UnsetOp,
  447. IncrementOp,
  448. AddOp,
  449. RelationOp,
  450. RemoveOp,
  451. AddUniqueOp
  452. });