ParseOp.js 13 KB

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