Skip to content

Commit 9971826

Browse files
committed
Changed protocol for array mutations. Now swap and splice are represented with 0 or 1 instead of negative value for swaps
1 parent 780ecff commit 9971826

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/core/objects/injectMutationInAction.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ dop.core.injectMutationInAction = function(action, mutation, isUnaction) {
4141
var swaps = mutation.swaps.slice(0);
4242
if (isUnaction)
4343
swaps.reverse();
44-
var tochange = (swaps[0]>0) ? 0 : 1;
45-
swaps[tochange] = swaps[tochange]*-1;
44+
// var tochange = (swaps[0]>0) ? 0 : 1;
45+
// swaps[tochange] = swaps[tochange]*-1;
46+
swaps.unshift(0); // 0 mean swap
4647
mutations.push(swaps);
4748
}
4849

@@ -55,14 +56,16 @@ dop.core.injectMutationInAction = function(action, mutation, isUnaction) {
5556
}
5657
else
5758
splice = mutation.splice.slice(0);
58-
59+
60+
splice.unshift(1); // 1 mean splice
5961
mutations.push(splice);
6062
}
6163

6264
// set
6365
else
64-
mutations.push([prop, 1, value]);
66+
mutations.push([1, prop, 1, value]);
6567

68+
// We have to update the length of the array in case that is lower than before
6669
if (isUnaction && mutation.length!==undefined && mutation.length!==mutation.object.length)
6770
action.length = mutation.length;
6871
}

src/core/objects/setActionMutator.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@ dop.core.setActionMutator = function(destiny, prop, value, typeofValue, path) {
1111
var mutations = value[dop.cons.DOP],
1212
mutation,
1313
index=0,
14-
total=mutations.length;
14+
total=mutations.length,
15+
typeArrayMutation;
1516

1617
// if (typeofDestiny!='array')
1718
// dop.set(destiny, prop, []);
1819

1920
for (;index<total; ++index) {
20-
mutation = mutations[index];
21-
// swaps
22-
if (mutation[0]<0 || mutation[1]<0) {
23-
mutation = mutation.slice(0);
24-
(mutation[0]<0) ? mutation[0] = mutation[0]*-1 : mutation[1] = mutation[1]*-1;
21+
typeArrayMutation = mutations[index][0]; // 0=swaps 1=splices
22+
mutation = mutations[index].slice(1);
23+
// swap
24+
if (typeArrayMutation===0) {
25+
// mutation = mutation.slice(0);
26+
// (mutation[0]<0) ? mutation[0] = mutation[0]*-1 : mutation[1] = mutation[1]*-1;
2527
dop.core.swap(destiny[prop], mutation);
2628
}
2729
// set
2830
else {
31+
// We have to update the length of the array in case that is lower than before
2932
if (destiny[prop].length<mutation[0])
3033
dop.getObjectTarget(destiny[prop]).length = mutation[0];
3134
// set

test/getactions.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ test('Setting an array and pushing changes', function(t) {
264264

265265

266266
test('Pushing an item', function(t) {
267-
var actionExpected = {one:{"~DOP":[[4,0,7]]}};
267+
var actionExpected = {one:{"~DOP":[[1,4,0,7]]}};
268268
var mutationsExpected = 1;
269269

270270
var collector = dop.collect();
@@ -278,7 +278,7 @@ test('Pushing an item', function(t) {
278278

279279

280280
test('Setting an item of array', function(t) {
281-
var actionExpected = {one:{"~DOP":[[1,1,'DOS']]}};
281+
var actionExpected = {one:{"~DOP":[[1,1,1,'DOS']]}};
282282
var mutationsExpected = 1;
283283

284284
var collector = dop.collect();
@@ -290,7 +290,7 @@ test('Setting an item of array', function(t) {
290290

291291

292292
test('Setting a subobject into an array', function(t) {
293-
var actionExpected = {"one":{"2":{"~DOP":[[2,1,{}]]}}};
293+
var actionExpected = {"one":{"2":{"~DOP":[[1,2,1,{}]]}}};
294294
var mutationsExpected = 1;
295295

296296
var collector = dop.collect();
@@ -325,7 +325,7 @@ test('Setting a property of a subobject that is into an array', function(t) {
325325

326326

327327
test('Pushing an item of a subarray that is into an array', function(t) {
328-
var actionExpected = {"one":{"2":{"2":{"array":{"~DOP":[[1,0,"xD"]]}}}}};
328+
var actionExpected = {"one":{"2":{"2":{"array":{"~DOP":[[1,1,0,"xD"]]}}}}};
329329
var mutationsExpected = 1;
330330

331331
var collector = dop.collect();
@@ -349,7 +349,7 @@ test('Setting a array internaly', function(t) {
349349

350350

351351
test('Pushing items and changing properties internaly', function(t) {
352-
var actionExpected = {one:{3:{2:{array:{"~DOP":[[2,1,"juas"],[3,0,"omg"]]}}},"~DOP":[[5,0,"omg"],[0,-5,1,4,2,3]]},two:undefined};
352+
var actionExpected = {one:{3:{2:{array:{"~DOP":[[1,2,1,"juas"],[1,3,0,"omg"]]}}},"~DOP":[[1,5,0,"omg"],[0,0,5,1,4,2,3]]},two:undefined};
353353
var mutationsExpected = 5;
354354

355355
var collector = dop.collect();
@@ -374,7 +374,7 @@ test('Pushing items and changing properties internaly', function(t) {
374374

375375

376376
test('Setting a property literaly', function(t) {
377-
var actionExpected = {"~DOP":[[0,1,"testing"]]};
377+
var actionExpected = {"~DOP":[[1,0,1,"testing"]]};
378378
var mutationsExpected = 1;
379379

380380
var collector = dop.collect();
@@ -386,7 +386,7 @@ test('Setting a property literaly', function(t) {
386386

387387

388388
test('Pushing a property', function(t) {
389-
var actionExpected = {"~DOP":[[1,0,"second"]]};
389+
var actionExpected = {"~DOP":[[1,1,0,"second"]]};
390390
var mutationsExpected = 1;
391391

392392
var collector = dop.collect();
@@ -397,7 +397,7 @@ test('Pushing a property', function(t) {
397397
});
398398

399399
test('Adding a subobject', function(t) {
400-
var actionExpected = {"~DOP":[[2,0,{"obj":123}]]};
400+
var actionExpected = {"~DOP":[[1,2,0,{"obj":123}]]};
401401
var mutationsExpected = 1;
402402

403403
var collector = dop.collect();
@@ -409,7 +409,7 @@ test('Adding a subobject', function(t) {
409409

410410

411411
test('Shift and editing subobject', function(t) {
412-
var actionExpected = {"1":{"prop":456},"~DOP":[[0,1]]};
412+
var actionExpected = {"1":{"prop":456},"~DOP":[[1,0,1]]};
413413
var mutationsExpected = 2;
414414

415415
var collector = dop.collect();
@@ -423,7 +423,7 @@ test('Shift and editing subobject', function(t) {
423423

424424

425425
test('Pushing literal arrays', function(t) {
426-
var actionExpected = {"1":{"prop":["my","array"]},"~DOP":[[2,0,[7,8,[9,10]],11],[0,1,"first"]]};
426+
var actionExpected = {"1":{"prop":["my","array"]},"~DOP":[[1,2,0,[7,8,[9,10]],11],[1,0,1,"first"]]};
427427
var mutationsExpected = 3;
428428

429429
var collector = dop.collect();

0 commit comments

Comments
 (0)