ParseOp.js 14 KB

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