Skip to content

Commit 99172d3

Browse files
committed
Changed protocol (undefined is now delete instruction)
1 parent 89343c3 commit 99172d3

File tree

8 files changed

+58
-35
lines changed

8 files changed

+58
-35
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.27.0",
3+
"version": "0.28.0",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",
@@ -57,7 +57,7 @@
5757
"tape-run": "^2.1.4"
5858
},
5959
"scripts": {
60-
"grunt": "grunt",
60+
"dev": "grunt",
6161
"build": "grunt --build",
6262
"test": "tap test/**.js test/protocol/**.js",
6363
"test-proxy": "tap test/**.js test/protocol/**.js --test-arg=proxy",

src/core/mutators/set.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dop.core.set = function(object, property, value, options) {
88
options.shadow = typeof options.shadow == 'boolean' ? options.shadow : false
99

1010
// If is a different value
11-
if (object[property] !== value) {
11+
if (object[property] !== value || !object.hasOwnProperty(property)) {
1212

1313
var descriptor = Object.getOwnPropertyDescriptor(object, property);
1414

src/core/objects/injectMutationInPatch.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ dop.core.injectMutationInPatch = function(patch, mutation) {
44
var prop = mutation.prop,
55
path = mutation.path,
66
value = mutation.value,
7+
isMutationDelete = !mutation.hasOwnProperty('value'),
78
isMutationSplice = mutation.splice!==undefined,
89
isMutationSwaps = mutation.swaps!==undefined,
910
isMutationArray = isMutationSplice || isMutationSwaps,
@@ -79,7 +80,6 @@ dop.core.injectMutationInPatch = function(patch, mutation) {
7980
}
8081
}
8182

82-
8383
// Mutations over arrays
8484
else if (isMutationArray) {
8585
if (isNewObject)
@@ -106,6 +106,9 @@ dop.core.injectMutationInPatch = function(patch, mutation) {
106106
}
107107
}
108108

109+
// Delete
110+
else if (isMutationDelete)
111+
chunk[prop] = [instructionsPatchs.delete];
109112

110113
// Others values
111114
else

src/core/objects/setPatch.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ dop.core.setPatchMutator = function(destiny, prop, value, typeofValue) {
2727
if (typeofValue == 'array') {
2828
typeInstruction = value[0];
2929

30+
// Delete
31+
if (typeInstruction === instructionsPatchs.delete)
32+
dop.del(destiny, prop);
33+
3034
// New object/array
31-
if (typeInstruction === instructionsPatchs.object)
35+
else if (typeInstruction === instructionsPatchs.object)
3236
dop.set(destiny, prop, dop.util.clone(value[1]));
3337

3438
// Array mutations
@@ -51,9 +55,7 @@ dop.core.setPatchMutator = function(destiny, prop, value, typeofValue) {
5155
return true; // Skiping to dont go inside of [instructionPatch, ...]
5256
}
5357

54-
// Delete
55-
else if (typeofValue=='undefined')
56-
dop.del(destiny, prop);
58+
5759

5860
// Set value
5961
else if (typeofValue!='object')

src/protocol/instructionsPatchs.js

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

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

99
// Non standards, only for JavaScript
10+
undefined: '~U', // Undefined
1011
nan: '~N',
1112
regex: '~R',
1213
infinity: '~I',

test/getpatchs.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test(header+'Changing property', function(t) {
5050
var collector = dop.collect();
5151
set(objectServer, 'one', 11);
5252
var patchGenerated = getPatch(collector);
53-
t.equal(collector.mutations.length, 1, 'Mutations expecteds: '+collector.mutations.length);
53+
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
5454
maketest(t, patchGenerated, patchExpected);
5555
});
5656

@@ -70,7 +70,7 @@ test(header+'Changing property with the same value', function(t) {
7070

7171
test(header+'Deleting property', function(t) {
7272
var objectServer = dop.register({one:11});
73-
var patchExpected = [{one:undefined}];
73+
var patchExpected = [{one:[0]}];
7474
var mutationsExpected = 1;
7575

7676
var collector = dop.collect();
@@ -82,7 +82,7 @@ test(header+'Deleting property', function(t) {
8282

8383
test(header+'Change and delete a removed item', function(t) {
8484
var objectServer = dop.register({one:11});
85-
var patchExpected = [{two:2,one:undefined}];
85+
var patchExpected = [{two:2,one:[0]}];
8686
var mutationsExpected = 3;
8787

8888
var collector = dop.collect();
@@ -91,7 +91,7 @@ test(header+'Change and delete a removed item', function(t) {
9191
set(objectServer, 'two', 2);
9292
var patchGenerated = getPatch(collector);
9393
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
94-
maketest(t, patchGenerated, patchExpected);
94+
maketest(t, patchGenerated, patchExpected, false);
9595
});
9696

9797

@@ -112,6 +112,20 @@ test(header+'Setting property array', function(t) {
112112

113113

114114

115+
test(header+'Adding undefineds', function(t) {
116+
var objectServer = dop.register({one:1,array:[]});
117+
var patchExpected = [{array: [ 2, [ 0, 0, undefined ] ], one:undefined,two:undefined}];
118+
var mutationsExpected = 3;
119+
120+
121+
var collector = dop.collect();
122+
set(objectServer, 'one', undefined);
123+
set(objectServer, 'two', undefined);
124+
objectServer.array.push(undefined)
125+
var patchGenerated = getPatch(collector);
126+
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
127+
maketest(t, patchGenerated, patchExpected);
128+
});
115129

116130

117131

@@ -125,7 +139,7 @@ test(header+'Setting an array and mutating it', function(t) {
125139
var collector = dop.collect();
126140

127141

128-
var patchExpected = [{"array":[0,["c","b",{"B1":"string"},false,true]]}];
142+
var patchExpected = [{"array":[1,["c","b",{"B1":"string"},false,true]]}];
129143
var mutationsExpected = 5;
130144
set(object, 'array', [true,false]);
131145
object.array.push('a','b','c')
@@ -149,7 +163,7 @@ test(header+'Mutating array then mutating nested objects', function(t) {
149163
var collector = dop.collect();
150164

151165

152-
var patchExpected = [{"array":[2,[0,1]]},{"array":{"2":[0,{"B1":false}],"length":3}}];
166+
var patchExpected = [{"array":[3,[0,1]]},{"array":{"2":[1,{"B1":false}],"length":3}}];
153167
var mutationsExpected = 3;
154168
object.array.reverse();
155169
set(object.array, 2, {B1:false});
@@ -170,7 +184,7 @@ test(header+'Mutating nested objects then mutating parent array', function(t) {
170184
var collector = dop.collect();
171185

172186

173-
var patchExpected = [{"array":{"2":[0,{"B1":false}],"length":3}},{"array":[2,[0,2]]}];
187+
var patchExpected = [{"array":{"2":[1,{"B1":false}],"length":3}},{"array":[3,[0,2]]}];
174188
var mutationsExpected = 3;
175189
set(object.array, 2, {B1:false});
176190
object.array.reverse();
@@ -191,7 +205,7 @@ test(header+'Mutating array twice', function(t) {
191205
var collector = dop.collect();
192206

193207

194-
var patchExpected = [{"array":[[1,[3,0,5,4,6]],[2,[0,1,1,2,3,4]]]}];
208+
var patchExpected = [{"array":[[2,[3,0,5,4,6]],[3,[0,1,1,2,3,4]]]}];
195209
var mutationsExpected = 2;
196210
object.array.push(5,4,6);
197211
object.array.sort();
@@ -211,7 +225,7 @@ test(header+'Mutating array and mutating array deeper', function(t) {
211225
var collector = dop.collect();
212226

213227

214-
var patchExpected = [{"array":[2,[0,2]]},{"array":{"0":[2,[0,1]]}}];
228+
var patchExpected = [{"array":[3,[0,2]]},{"array":{"0":[3,[0,1]]}}];
215229
var mutationsExpected = 2;
216230
object.array.reverse();
217231
object.array[0].reverse();
@@ -230,7 +244,7 @@ test(header+'Mutating array deeper and mutating container', function(t) {
230244
var collector = dop.collect();
231245

232246

233-
var patchExpected = [{"array":{"2":[2,[0,1]]}},{"array":[2,[0,2]]}];
247+
var patchExpected = [{"array":{"2":[3,[0,1]]}},{"array":[3,[0,2]]}];
234248
var mutationsExpected = 2;
235249
object.array[2].reverse();
236250
object.array.reverse();

test/getunpatchs.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var header = '--- ';
3030

3131
test(header+'Adding property', function(t) {
3232
var objectServer = dop.register({});
33-
var patchExpected = [{one:undefined}];
33+
var patchExpected = [{one:[0]}];
3434
var mutationsExpected = 1;
3535

3636

@@ -88,15 +88,15 @@ test(header+'Change and delete a removed item', function(t) {
8888

8989

9090
var mutationsExpected = 3;
91-
var patchExpected = [{one:11,two:undefined}];
91+
var patchExpected = [{one:11,two:[0]}];
9292
set(objectServer, 'one', 'Changeddd');
9393
del(objectServer, 'one');
9494
set(objectServer, 'two', 2);
9595

9696

9797
var patchGenerated = applyPatch(collector);
9898
t.equal(collector.mutations.length, mutationsExpected, 'Mutations expecteds: '+collector.mutations.length);
99-
maketest(t, patchGenerated, patchExpected);
99+
maketest(t, patchGenerated, patchExpected, false);
100100
});
101101

102102

@@ -107,7 +107,7 @@ test(header+'Setting property array', function(t) {
107107
var collector = dop.collect();
108108

109109

110-
var patchExpected = [{"array":{"5":undefined,"length":2}}];
110+
var patchExpected = [{"array":{"5":[0],"length":2}}];
111111
var mutationsExpected = 2;
112112
set(object.array, 5, "three");
113113

@@ -130,7 +130,7 @@ test(header+'Setting an array and mutating it', function(t) {
130130
var collector = dop.collect();
131131

132132

133-
var patchExpected = [{"array":[2,[2,0]]},{"array":undefined}];
133+
var patchExpected = [{"array":[3,[2,0]]},{"array":[0]}];
134134
var mutationsExpected = 4;
135135
set(object, 'array', [true,false]);
136136
set(object.array, 2, {B1:[true,false]});
@@ -152,7 +152,7 @@ test(header+'Mutating array then mutating nested objects', function(t) {
152152
var collector = dop.collect();
153153

154154

155-
var patchExpected = [{"array":{"2":undefined,"length":2}},{"array":[2,[1,0]]}];
155+
var patchExpected = [{"array":{"2":[0],"length":2}},{"array":[3,[1,0]]}];
156156
var mutationsExpected = 3;
157157
object.array.reverse();
158158
set(object.array, 2, {B1:false});
@@ -173,7 +173,7 @@ test(header+'Mutating nested objects then mutating parent array', function(t) {
173173
var collector = dop.collect();
174174

175175

176-
var patchExpected = [{"array":[2,[2,0]]},{"array":{"2":undefined,"length":2}}];
176+
var patchExpected = [{"array":[3,[2,0]]},{"array":{"2":[0],"length":2}}];
177177
var mutationsExpected = 3;
178178
set(object.array, 2, {B1:false});
179179
object.array.reverse();
@@ -194,7 +194,7 @@ test(header+'Mutating array twice', function(t) {
194194
var collector = dop.collect();
195195

196196

197-
var patchExpected = [{"array":[[2,[4,3,2,1,1,0]],[1,[3,3]]]}];
197+
var patchExpected = [{"array":[[3,[4,3,2,1,1,0]],[2,[3,3]]]}];
198198
var mutationsExpected = 2;
199199
object.array.push(5,4,6);
200200
object.array.sort();
@@ -213,7 +213,7 @@ test(header+'Mutating array and mutating array deeper', function(t) {
213213
var collector = dop.collect();
214214

215215

216-
var patchExpected = [{"array":{"0":[2,[1,0]]}},{"array":[2,[2,0]]}];
216+
var patchExpected = [{"array":{"0":[3,[1,0]]}},{"array":[3,[2,0]]}];
217217
var mutationsExpected = 2;
218218
object.array.reverse();
219219
object.array[0].reverse();
@@ -232,7 +232,7 @@ test(header+'Mutating array deeper and mutating container', function(t) {
232232
var collector = dop.collect();
233233

234234

235-
var patchExpected = [{"array":[2,[2,0]]},{"array":{"2":[2,[1,0]]}}];
235+
var patchExpected = [{"array":[3,[2,0]]},{"array":{"2":[3,[1,0]]}}];
236236
var mutationsExpected = 2;
237237
object.array[2].reverse();
238238
object.array.reverse();

test/undoredo.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ var del = dop.del;
88

99
function makeTest(t, snapshot, original, object, mutated) {
1010

11-
var patch, unpatch;
1211
var object_id = dop.getObjectId(object)
1312
var target = dop.getObjectTarget(object)
13+
var unpatch = snapshot.getUnpatch()[object_id].chunks
14+
var patch = snapshot.getPatch()[object_id].chunks
1415

15-
unpatch = dop.decode(dop.encode(snapshot.getUnpatch()[object_id].chunks))
16+
17+
unpatch = dop.decode(dop.encode(unpatch))
1618
// console.log('UNDO', unpatch.length, JSON.stringify(unpatch))
17-
patch = dop.decode(dop.encode(snapshot.getPatch()[object_id].chunks))
19+
patch = dop.decode(dop.encode(patch))
1820
// console.log('REDO', patch.length, JSON.stringify(patch))
1921

2022
// Undo
@@ -61,6 +63,7 @@ test('Set array', function(t) {
6163
var original = dop.util.clone(object)
6264
var collector = dop.collect()
6365
set(object, 'dos', {e:4})
66+
set(object, 'three', undefined)
6467
var snapshot = collector.emit()
6568
var mutated = dop.util.clone(object)
6669

0 commit comments

Comments
 (0)