ParseOp.js 12 KB

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