Skip to content

Commit d9654f8

Browse files
committed
setPatch implemented and undoredo test passed
1 parent 05002d3 commit d9654f8

File tree

6 files changed

+252
-227
lines changed

6 files changed

+252
-227
lines changed

src/core/constructors/snapshot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dop.core.snapshot.prototype.undo = function () {
1717
dop.core.snapshot.prototype.redo = function () {
1818
if (!this.forward && this.mutations.length>0) {
1919
this.forward = true;
20+
this.setPatch(this.getPatch());
2021
this.emit();
2122
}
2223
};

src/core/mutators/swap.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11

2-
// dop.core.swap = function(array, swaps) {
2+
dop.core.swap = function(array, swaps) {
3+
var objectTarget = dop.getObjectTarget(array),
4+
objectProxy = dop.getObjectProxy(array);
35

4-
// if (swaps.length>1) {
6+
var result = dop.util.swap(objectTarget, swaps);
57

6-
// var objectTarget = dop.getObjectTarget(array),
7-
// objectProxy = dop.getObjectProxy(array);
8+
if (objectTarget===objectProxy || array===objectProxy)
9+
dop.core.storeMutation({
10+
object: objectProxy,
11+
prop: dop.getObjectProperty(array),
12+
path: dop.getObjectPath(array),
13+
swaps: swaps.slice(0)
14+
});
815

9-
// var result = dop.util.swap(objectTarget, swaps, function(swapA, swapB){
10-
// // Updating path
11-
// dop.core.updatePathArray(objectTarget, swapA);
12-
// dop.core.updatePathArray(objectTarget, swapB);
13-
// })
14-
15-
// if (objectTarget===objectProxy || array===objectProxy)
16-
// dop.core.storeMutation({
17-
// object:dop.getObjectProxy(dop.getObjectParent(array)),
18-
// name:dop.getObjectProperty(array),
19-
// swaps:swaps
20-
// });
21-
22-
// return result;
23-
// }
24-
// };
16+
return result;
17+
};

src/core/objects/injectMutationInPatch.js

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

2-
dop.core.injectMutationInPatch = function(patchs, mutation) {
2+
dop.core.injectMutationInPatch = function(patch, mutation) {
33

44
var prop = mutation.prop,
55
path = mutation.path,
@@ -9,7 +9,7 @@ dop.core.injectMutationInPatch = function(patchs, mutation) {
99
isMutationArray = isMutationSplice || isMutationSwaps,
1010
typeofValue = dop.util.typeof(value),
1111
index = 1,
12-
chunk = chunkParent = patchs.chunks[patchs.chunks.length-1],
12+
chunk = chunkParent = patch.chunks[patch.chunks.length-1],
1313
chunkNext = chunkNextParent = chunkNextRoot = {},
1414
tofCurrentObject,
1515
specialInstruction,
@@ -42,15 +42,15 @@ dop.core.injectMutationInPatch = function(patchs, mutation) {
4242
else if (!isMutationArray || (isMutationArray && index+1<path.length)) {
4343
isNewChunk = true;
4444
chunk = chunkNext;
45-
patchs.chunks.push(chunkNextRoot);
45+
patch.chunks.push(chunkNextRoot);
4646
}
4747
}
4848

4949
else if (!isNewChunk && isMutationArray && tofCurrentObject == 'object') {
5050
// isNewChunk = true;
5151
chunkParent = chunkNextParent;
5252
chunk = chunkNext;
53-
patchs.chunks.push(chunkNextRoot);
53+
patch.chunks.push(chunkNextRoot);
5454
}
5555

5656
else if (tofCurrentObject == 'object')

src/core/objects/setPatch.js

Lines changed: 103 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,125 @@
11

22
dop.core.setPatch = function(object, patch) {
3-
dop.util.path(patch, null, object, dop.core.setPatchMutator);
3+
if (!isArray(patch))
4+
patch = [patch];
5+
6+
for (var index=0,total=patch.length; index<total; ++index)
7+
dop.util.path(patch[index], null, object, dop.core.setPatchMutator);
48
};
59

610

711
dop.core.setPatchMutator = function(destiny, prop, value, typeofValue, path) {
8-
912
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)) {
13+
typeofDestiny = dop.util.typeof(destiny[prop]),
14+
typeInstruction,
15+
instructionsPatchs = dop.protocol.instructionsPatchs,
16+
mutation;
1417

15-
var mutations = value[dop.cons.DOP],
16-
mutation,
17-
index=0,
18-
total=mutations.length,
19-
typeArrayMutation;
18+
// console.log( prop, typeofValue, value );
19+
if (typeofValue == 'array') {
20+
typeInstruction = value[0];
2021

21-
// if (typeofDestiny!='array')
22-
// dop.set(destiny, prop, []);
22+
// New object/array
23+
if (typeInstruction === instructionsPatchs.object)
24+
dop.set(destiny, prop, dop.util.clone(value[1]));
2325

24-
for (;index<total; ++index) {
25-
typeArrayMutation = mutations[index][0]; // 0=swaps 1=splices
26-
mutation = mutations[index].slice(1);
26+
// Array mutations
27+
else {
28+
if (!isArray(typeInstruction))
29+
value = [value];
2730

28-
// swap
29-
if (typeArrayMutation===0)
30-
dop.core.swap(destiny[prop], mutation);
31+
for (var index=0,total=value.length; index<total; ++index) {
32+
mutation = value[index];
3133

32-
// length
33-
else if (typeArrayMutation===2)
34-
dop.set(destiny[prop], 'length', mutation[0]);
34+
// Splice
35+
if (mutation[0] === instructionsPatchs.splice)
36+
dop.core.splice(destiny[prop], mutation[1]);
3537

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);
38+
else if (mutation[0] === instructionsPatchs.swaps)
39+
dop.core.swap(destiny[prop], mutation[1]);
5340
}
5441
}
5542

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:...}
43+
return true; // Skiping to dont go inside of [instructionPatch, ...]
6044
}
6145

62-
else //if (path.length > 1) {
46+
// Delete
47+
else if (typeofValue=='undefined')
48+
dop.del(destiny, prop);
49+
50+
// Set value
51+
else if (typeofValue!='object')
52+
dop.set(destiny, prop, value);
53+
};
54+
55+
56+
57+
58+
// // Array mutations
59+
// if (typeofValue=='object' && typeofDestiny=='array' && value.hasOwnProperty(dop.cons.DOP)) {
6360

64-
// Objects
65-
if (typeofValue=='object' && typeofDestiny!='object') //!destiny.hasOwnProperty(prop)
66-
dop.set(destiny, prop, {});
61+
// var mutations = value[dop.cons.DOP],
62+
// mutation,
63+
// index=0,
64+
// total=mutations.length,
65+
// typeArrayMutation;
6766

68-
// Arrays
69-
else if (typeofValue=='array' && typeofDestiny!='array')
70-
dop.set(destiny, prop, []);
67+
// // if (typeofDestiny!='array')
68+
// // dop.set(destiny, prop, []);
7169

72-
// Delete
73-
else if (typeofValue=='undefined')
74-
dop.del(destiny, prop);
70+
// for (;index<total; ++index) {
71+
// typeArrayMutation = mutations[index][0]; // 0=swaps 1=splices
72+
// mutation = mutations[index].slice(1);
7573

76-
// Set value
77-
else if (typeofValue!='object')
78-
dop.set(destiny, prop, value);
79-
//}
80-
};
74+
// // swap
75+
// if (typeArrayMutation===0)
76+
// dop.core.swap(destiny[prop], mutation);
77+
78+
// // length
79+
// else if (typeArrayMutation===2)
80+
// dop.set(destiny[prop], 'length', mutation[0]);
81+
82+
// // splice & set & del
83+
// else {
84+
// // We have to update the length of the array in case that is lower than before
85+
// if (destiny[prop].length<mutation[0])
86+
// dop.getObjectTarget(destiny[prop]).length = mutation[0];
87+
88+
// // set
89+
// if (mutation.length===3 && mutation[1]===1) {
90+
// (mutation[2] === undefined) ?
91+
// dop.del(destiny[prop], mutation[0])
92+
// :
93+
// dop.set(destiny[prop], mutation[0], mutation[2]);
94+
// }
95+
96+
// // splice
97+
// else
98+
// dop.core.splice(destiny[prop], mutation);
99+
// }
100+
// }
101+
102+
// // if (typeof value.length == 'number' && value.length>-1)
103+
// // destiny[prop].length = value.length;
104+
105+
// return true; // Skiping to dont go inside of {~dop:...}
106+
// }
107+
108+
// else //if (path.length > 1) {
109+
110+
// // Objects
111+
// if (typeofValue=='object' && typeofDestiny!='object') //!destiny.hasOwnProperty(prop)
112+
// dop.set(destiny, prop, {});
113+
114+
// // Arrays
115+
// else if (typeofValue=='array' && typeofDestiny!='array')
116+
// dop.set(destiny, prop, []);
117+
118+
// // Delete
119+
// else if (typeofValue=='undefined')
120+
// dop.del(destiny, prop);
121+
122+
// // Set value
123+
// else if (typeofValue!='object')
124+
// dop.set(destiny, prop, value);
125+
// //}

src/protocol/instructionsPatchs.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11

22
dop.protocol.instructionsPatchs = {
3+
undefined: '~U', // Delete
4+
function: '~F', // Remote function
5+
object: 2, // New object or array
6+
splice: 3, // Splice array
7+
swaps: 4, // Swap array
38

4-
undefined: '~U', // delete
5-
6-
function: '~F', // remote function
7-
8-
object: 2, // new object or array
9-
10-
splice: 3,
11-
12-
swaps: 4,
13-
14-
15-
// No standard only for JavaScript
9+
// Non standards, only for JavaScript
1610
nan: '~N',
1711
regex: '~R',
1812
infinity: '~I',

0 commit comments

Comments
 (0)