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