Skip to content

Commit e00ed24

Browse files
Merge pull request #437 from canjs/436-update-once
Prevent trying to remove and insert a value in the same place
2 parents 46b4eae + b01b240 commit e00ed24

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

real-time/real-time.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ function updateList(list, getRecord, currentIndex, newIndex) {
163163

164164

165165
function updateListWithItem(list, recordData, currentIndex, newIndex, connection, set){
166+
// we are inserting right where we already are.
167+
if(currentIndex !== -1 && (newIndex === currentIndex + 1 || newIndex === currentIndex)) {
168+
return;
169+
}
166170
if(list[spliceSymbol] !== undefined) {
167171
updateList(list, function(){
168172
return connection.hydrateInstance(recordData);

real-time/real-time_test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var QUnit = require("steal-qunit");
1212
var assign = require("can-reflect").assignMap;
1313
var canDev = require('can-log/dev/dev');
1414
var QueryLogic = require("can-query-logic");
15+
var canReflect = require("can-reflect");
1516

1617
QUnit.module("can-connect/real-time",{});
1718

@@ -583,3 +584,72 @@ QUnit.test("instances should be removed from 'length-bound' lists when destroyed
583584
done();
584585
});
585586
});
587+
588+
QUnit.test("real-time doesn't handle updates when the id doesn't change (#436)", function (assert) {
589+
var done = assert.async();
590+
assert.expect(1);
591+
592+
var todos = [{
593+
name: "test the store",
594+
id: "def"
595+
}, {
596+
name: "rock the house",
597+
id: "ghi"
598+
}];
599+
var connection = connect([
600+
function(){
601+
return {
602+
list: function(items) {
603+
var list = new DefineList(items.data);
604+
// simulate the can-connet/can/map/map behaviour
605+
// adding the list to the listStore
606+
connection.addListReference(list, {});
607+
return list;
608+
},
609+
getListData: function(){
610+
return Promise.resolve(todos);
611+
},
612+
destroyData: function() {
613+
// simulate an empty object response from server
614+
return Promise.resolve({});
615+
},
616+
updateData: function(data){
617+
return Promise.resolve(canReflect.serialize(data));
618+
}
619+
};
620+
},
621+
dataCallbacks,
622+
realTime,
623+
constructor,
624+
constructorStore
625+
], {
626+
queryLogic: new QueryLogic({
627+
identity: ["id"]
628+
})
629+
});
630+
631+
connection.getList({}).then(function(list){
632+
list.on("length", function(){
633+
assert.ok(false, "Length should not change");
634+
});
635+
connection.save( list[0] );
636+
return list;
637+
}).then(function(list) {
638+
list.off("length");
639+
list.on("length", function(){
640+
assert.ok(true, "Length should change when adding item in position 0");
641+
});
642+
return connection.save({
643+
name: "go to sleep",
644+
id: "abc"
645+
});
646+
}).then(function(){
647+
done();
648+
}, function(err){
649+
setTimeout(function(){
650+
throw err;
651+
},1);
652+
done();
653+
})
654+
655+
});

0 commit comments

Comments
 (0)