ParseOp.js 13 KB

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