Skip to content

Commit 05002d3

Browse files
committed
Implementing setPatch and snapshot for the new protocol
1 parent 1b89fbf commit 05002d3

22 files changed

Lines changed: 553 additions & 304 deletions

src/core/constructors/snapshot.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11

22
dop.core.snapshot = function (mutations) {
3-
this.forward = true;
43
this.mutations = mutations;
5-
// this.patchRedo;
6-
// this.patchUndo;
4+
this.forward = true;
75
};
86

97

10-
// dop.core.snapshot.prototype.undo = function () {
11-
// if (this.forward && this.mutations.length>0) {
12-
// this.forward = false;
13-
// dop.core.invertMutations(this.mutations);
14-
// this.emit();
15-
// }
16-
// };
17-
8+
dop.core.snapshot.prototype.undo = function () {
9+
if (this.forward && this.mutations.length>0) {
10+
this.forward = false;
11+
this.setPatch(this.getUnpatch());
12+
this.emit();
13+
}
14+
};
1815

19-
// dop.core.snapshot.prototype.redo = function () {
20-
// if (!this.forward && this.mutations.length>0) {
21-
// this.forward = true;
22-
// dop.core.invertMutations(this.mutations);
23-
// this.emit();
24-
// }
25-
// };
2616

17+
dop.core.snapshot.prototype.redo = function () {
18+
if (!this.forward && this.mutations.length>0) {
19+
this.forward = true;
20+
this.emit();
21+
}
22+
};
2723

2824

25+
// This should be private
2926
dop.core.snapshot.prototype.emit = function () {
3027
// This is true if we have nodes subscribed to those object/mutations
3128
// Then we have to emit to nodes
32-
if (this.mutations.length>0 && dop.core.emitObservers(this.mutations)) {
33-
dop.core.emitNodes(this.forward ?
34-
(this.redoPatch === undefined) ?
35-
this.redoPatch = dop.core.getPatch(this.mutations) : this.redoPatch
36-
:
37-
(this.undoPatch === undefined) ?
38-
this.undoPatch = dop.core.getPatch(this.mutations) : this.undoPatch
39-
);
40-
}
29+
if (this.mutations.length>0 && dop.core.emitObservers(this.mutations))
30+
dop.core.emitNodes(this.forward ? this.getPatch() : this.getUnpatch());
31+
};
32+
33+
34+
dop.core.snapshot.prototype.getPatch = function() {
35+
return this.patch = (this.patch === undefined) ?
36+
dop.core.getPatch(this.mutations)
37+
:
38+
this.patch;
39+
};
40+
41+
42+
dop.core.snapshot.prototype.getUnpatch = function() {
43+
return this.unpatch = (this.unpatch === undefined) ?
44+
dop.core.getUnpatch(this.mutations)
45+
:
46+
this.unpatch;
47+
};
48+
49+
50+
dop.core.snapshot.prototype.setPatch = function(patch) {
51+
for (object_id in patch)
52+
dop.core.setPatch(patch[object_id].object, patch[object_id].chunks);
4153
};

src/core/mutators/set.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dop.core.set = function(object, property, value) {
1111
objectProxy = dop.getObjectProxy(object),
1212
oldValue = objectTarget[property],
1313
length = objectTarget.length,
14+
isNewProperty = !objectTarget.hasOwnProperty(property),
1415
path;
1516

1617
// Setting
@@ -30,7 +31,7 @@ dop.core.set = function(object, property, value) {
3031
path: path,
3132
value: dop.util.clone(value)
3233
};
33-
if (objectTarget.hasOwnProperty(property))
34+
if (!isNewProperty)
3435
mutation.oldValue = dop.util.clone(oldValue)
3536

3637
// If is array and length is different we must store the length
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
dop.core.getMutationInverted = function(mutation) {
3+
4+
var mutationInverted = {
5+
object: mutation.object,
6+
path: mutation.path,
7+
prop: mutation.prop
8+
};
9+
10+
// splice
11+
if (mutation.splice !== undefined) {
12+
var splice = mutation.splice,
13+
spliced = (mutation.spliced === undefined) ? [] : mutation.spliced;
14+
15+
mutationInverted.splice = [splice[0], splice.length-2];
16+
Array.prototype.push.apply(mutationInverted.splice, spliced);
17+
18+
mutationInverted.spliced = splice.slice(2);
19+
if (mutationInverted.spliced.length === 0)
20+
delete mutationInverted.spliced;
21+
}
22+
23+
// swaps
24+
else if (mutation.swaps !== undefined)
25+
mutationInverted.swaps = mutation.swaps.slice(0).reverse();
26+
27+
// new value
28+
else if (!mutation.hasOwnProperty('oldValue'))
29+
mutationInverted.oldValue = mutation.value;
30+
31+
// delete
32+
else if (!mutation.hasOwnProperty('value'))
33+
mutationInverted.value = mutation.oldValue;
34+
35+
// set
36+
else {
37+
mutationInverted.oldValue = mutation.value;
38+
mutationInverted.value = mutation.oldValue;
39+
}
40+
41+
return mutationInverted;
42+
};

src/core/objects/getPatch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
dop.core.getPatch = function(mutations) {
2+
dop.core.getPatch = function(mutations, isUnpatch) {
33

44
var patchs = {},
55
index = 0,
@@ -8,12 +8,12 @@ dop.core.getPatch = function(mutations) {
88
object_id;
99

1010
for (;index<total; ++index) {
11-
mutation = mutations[index];
11+
mutation = isUnpatch ? dop.core.getMutationInverted(mutations[index]) : mutations[index];
1212
object_id = dop.getObjectId(mutation.object);
1313
if (patchs[object_id] === undefined)
1414
patchs[object_id] = {chunks:[{}], object:dop.getObjectRoot(mutation.object)};
1515
dop.core.injectMutationInPatch(patchs[object_id], mutation);
16-
console.log(JSON.stringify(patchs[object_id].chunks))
16+
// console.log(JSON.stringify(patchs[object_id].chunks))
1717
}
1818

1919
return patchs;

src/core/objects/getUnpatch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
dop.core.getUnpatch = function(mutations) {
3+
return dop.core.getPatch(mutations.slice(0).reverse(), true);
4+
};

src/core/objects/injectMutationInPatch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dop.core.injectMutationInPatch = function(patchs, mutation) {
3535
if (tofCurrentObject == 'array') {
3636
specialInstruction = chunk[propPath];
3737
// Is a new object
38-
if (specialInstruction[0] === instructionsPatchs.object.value) {
38+
if (specialInstruction[0] === instructionsPatchs.object) {
3939
isNewObject = true;
4040
chunk = specialInstruction[1];
4141
}
@@ -70,7 +70,7 @@ dop.core.injectMutationInPatch = function(patchs, mutation) {
7070
chunk[prop] = valueMerged;
7171
else {
7272
chunk[prop] = [
73-
instructionsPatchs.object.value,
73+
instructionsPatchs.object,
7474
valueMerged
7575
];
7676
}
@@ -87,9 +87,9 @@ dop.core.injectMutationInPatch = function(patchs, mutation) {
8787

8888
else {
8989
newSpecialInstruction = (isMutationSplice) ?
90-
[instructionsPatchs.splice.value,mutation.splice.slice(0)]
90+
[instructionsPatchs.splice, mutation.splice.slice(0)]
9191
:
92-
[instructionsPatchs.swaps.value,mutation.swaps.slice(0)]
92+
[instructionsPatchs.swaps, mutation.swaps.slice(0)]
9393

9494
if (!isArray(chunkParent[prop]))
9595
chunkParent[prop] = newSpecialInstruction;

src/core/objects/invertMutation.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/core/objects/invertMutations.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/core/objects/setPatch.js

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
11

22
dop.core.setPatch = function(object, patch) {
33
dop.util.path(patch, null, object, dop.core.setPatchMutator);
4-
return object;
54
};
65

7-
// dop.core.setPatchs = function(patchs) {
8-
// var collector = dop.collectFirst(), object_id;
9-
// for (object_id in patchs)
10-
// dop.core.setPatch(patchs[object_id].object, patchs[object_id].patch);
11-
// return collector;
12-
// };
6+
7+
dop.core.setPatchMutator = function(destiny, prop, value, typeofValue, path) {
8+
9+
var typeofDestinyParent = dop.util.typeof(destiny),
10+
typeofDestiny = dop.util.typeof(destiny[prop]);
11+
12+
// Array mutations
13+
if (typeofValue=='object' && typeofDestiny=='array' && value.hasOwnProperty(dop.cons.DOP)) {
14+
15+
var mutations = value[dop.cons.DOP],
16+
mutation,
17+
index=0,
18+
total=mutations.length,
19+
typeArrayMutation;
20+
21+
// if (typeofDestiny!='array')
22+
// dop.set(destiny, prop, []);
23+
24+
for (;index<total; ++index) {
25+
typeArrayMutation = mutations[index][0]; // 0=swaps 1=splices
26+
mutation = mutations[index].slice(1);
27+
28+
// swap
29+
if (typeArrayMutation===0)
30+
dop.core.swap(destiny[prop], mutation);
31+
32+
// length
33+
else if (typeArrayMutation===2)
34+
dop.set(destiny[prop], 'length', mutation[0]);
35+
36+
// splice & set & del
37+
else {
38+
// We have to update the length of the array in case that is lower than before
39+
if (destiny[prop].length<mutation[0])
40+
dop.getObjectTarget(destiny[prop]).length = mutation[0];
41+
42+
// set
43+
if (mutation.length===3 && mutation[1]===1) {
44+
(mutation[2] === undefined) ?
45+
dop.del(destiny[prop], mutation[0])
46+
:
47+
dop.set(destiny[prop], mutation[0], mutation[2]);
48+
}
49+
50+
// splice
51+
else
52+
dop.core.splice(destiny[prop], mutation);
53+
}
54+
}
55+
56+
// if (typeof value.length == 'number' && value.length>-1)
57+
// destiny[prop].length = value.length;
58+
59+
return true; // Skiping to dont go inside of {~dop:...}
60+
}
61+
62+
else //if (path.length > 1) {
63+
64+
// Objects
65+
if (typeofValue=='object' && typeofDestiny!='object') //!destiny.hasOwnProperty(prop)
66+
dop.set(destiny, prop, {});
67+
68+
// Arrays
69+
else if (typeofValue=='array' && typeofDestiny!='array')
70+
dop.set(destiny, prop, []);
71+
72+
// Delete
73+
else if (typeofValue=='undefined')
74+
dop.del(destiny, prop);
75+
76+
// Set value
77+
else if (typeofValue!='object')
78+
dop.set(destiny, prop, value);
79+
//}
80+
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

2-
dop.core.setPatchFunction = function(object, patch) {
3-
dop.util.path({a:patch}, null, {a:object}, function(destiny, prop, value, typeofValue, path){
4-
if (isFunction(value) && value.name==dop.core.createRemoteFunction.name)
5-
dop.set(destiny, prop, value(dop.getObjectDop(object)[0], path.slice(1)));
6-
else
7-
return dop.core.setPatchMutator(destiny, prop, value, typeofValue, path);
8-
});
9-
return object;
10-
};
2+
// dop.core.setPatchFunction = function(object, patch) {
3+
// dop.util.path({a:patch}, null, {a:object}, function(destiny, prop, value, typeofValue, path){
4+
// if (isFunction(value) && value.name==dop.core.createRemoteFunction.name)
5+
// dop.set(destiny, prop, value(dop.getObjectDop(object)[0], path.slice(1)));
6+
// else
7+
// return dop.core.setPatchMutator(destiny, prop, value, typeofValue, path);
8+
// });
9+
// return object;
10+
// };

0 commit comments

Comments
 (0)