Skip to content

Commit 46e18b8

Browse files
committed
firebase sync fixes
1 parent 2a3b032 commit 46e18b8

File tree

4 files changed

+64
-42
lines changed

4 files changed

+64
-42
lines changed

my-mind.js

+32-21
Original file line numberDiff line numberDiff line change
@@ -395,34 +395,34 @@ MM.Item.prototype.fromJSON = function(data) {
395395
}
396396

397397
MM.Item.prototype.mergeWith = function(data) {
398-
var dirty = false;
398+
var dirty = 0;
399399
if (this.getText() != data.text) { this.setText(data.text); }
400400

401401
if (this._side != data.side) {
402402
this._side = data.side;
403-
dirty = true;
403+
dirty = 1;
404404
}
405405

406406
if (this._color != data.color) {
407407
this._color = data.color;
408-
dirty = true;
408+
dirty = 2;
409409
}
410410

411411
if (this._value != data.value) {
412412
this._value = data.value;
413-
dirty = true;
413+
dirty = 1;
414414
}
415415

416416
if (this._status != data.status) {
417417
this._status = data.status;
418-
dirty = true;
418+
dirty = 1;
419419
}
420420

421421
if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }
422422

423423
if (this.getOwnLayout() != data.layout) {
424424
this._layout = MM.Layout.getById(data.layout);
425-
dirty = true;
425+
dirty = 2;
426426
}
427427

428428
var s = (this._autoShape ? null : this._shape.id);
@@ -431,24 +431,24 @@ MM.Item.prototype.mergeWith = function(data) {
431431
/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
432432
(data.children || []).forEach(function(child, index) {
433433
if (index >= this._children.length) { /* new child */
434-
console.log("adding new child", child, "at", index);
435434
this.insertChild(MM.Item.fromJSON(child));
436-
// dirty = true; FIXME to zaridi to dite, ze?
437435
} else { /* existing child */
438436
var myChild = this._children[index];
439437
if (myChild.getId() == child.id) { /* recursive merge */
440-
console.log("merging child", myChild, "with", child);
441438
myChild.mergeWith(child);
442439
} else { /* changed; replace */
443-
console.log("replacing dead child", myChild, "with new", child);
444440
this.removeChild(this._children[index]);
445441
this.insertChild(MM.Item.fromJSON(child), index);
446-
// dirty = true; FIXME to zaridi to dite, ze?
447442
}
448443
}
449444
}, this);
450445

451-
if (dirty) { this.update(); }
446+
/* remove dead children */
447+
var newLength = (data.children || []).length;
448+
while (this._children.length > newLength) { this.removeChild(this._children[this._children.length-1]); }
449+
450+
if (dirty == 1) { this.update(); }
451+
if (dirty == 2) { this.updateSubtree(); }
452452
}
453453

454454
MM.Item.prototype.clone = function() {
@@ -3154,14 +3154,20 @@ MM.Backend.Firebase.reset = function() {
31543154
* Merge current (remote) data with updated map
31553155
*/
31563156
MM.Backend.Firebase.mergeWith = function(data, name) {
3157+
var id = this._current.id;
3158+
31573159
if (name != this._current.name) {
31583160
this._current.name = name;
3159-
this.ref.child("names/" + this._current.id).set(name);
3161+
this.ref.child("names/" + id).set(name);
31603162
}
31613163

3162-
var dataRef = this.ref.child("data/" + this._current.id);
3163-
this._recursiveRefMerge(dataRef, this._current.data, data);
3164-
this._current.data = data;
3164+
3165+
var dataRef = this.ref.child("data/" + id);
3166+
var oldData = this._current.data;
3167+
3168+
this._listenStop();
3169+
this._recursiveRefMerge(dataRef, oldData, data);
3170+
this._listenStart(data, id);
31653171
}
31663172

31673173
/**
@@ -3239,12 +3245,15 @@ MM.Backend.Firebase._listenStop = function() {
32393245

32403246

32413247
/**
3242-
* Monitored remote ref changed
3243-
* FIXME use timeout to buffer changes?
3248+
* Monitored remote ref changed.
3249+
* FIXME move timeout logic to ui.backend.firebase?
32443250
*/
32453251
MM.Backend.Firebase._valueChange = function(snap) {
32463252
this._current.data = snap.val();
3247-
MM.publish("firebase-change", this, this._current.data);
3253+
if (this._changeTimeout) { clearTimeout(this._changeTimeout); }
3254+
this._changeTimeout = setTimeout(function() {
3255+
MM.publish("firebase-change", this, this._current.data);
3256+
}.bind(this), 200);
32483257
}
32493258

32503259
MM.Backend.Firebase._login = function(type) {
@@ -4302,22 +4311,24 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
43024311

43034312
case "firebase-change":
43044313
if (data) {
4314+
MM.unsubscribe("item-change", this);
43054315
MM.App.map.mergeWith(data);
4316+
MM.subscribe("item-change", this);
43064317
} else { /* FIXME */
43074318
console.log("remote data disappeared");
43084319
}
43094320
break;
43104321

43114322
case "item-change":
43124323
if (this._itemChangeTimeout) { clearTimeout(this._itemChangeTimeout); }
4313-
this._itemChangeTimeout = setTimeout(this._itemChange.bind(this), 300);
4324+
this._itemChangeTimeout = setTimeout(this._itemChange.bind(this), 200);
43144325
break;
43154326
}
43164327
}
43174328

43184329
MM.UI.Backend.Firebase.reset = function() {
4319-
MM.unsubscribe("item-change", this);
43204330
this._backend.reset();
4331+
MM.unsubscribe("item-change", this);
43214332
}
43224333

43234334
MM.UI.Backend.Firebase._itemChange = function() {

src/backend.firebase.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ MM.Backend.Firebase.reset = function() {
8484
* Merge current (remote) data with updated map
8585
*/
8686
MM.Backend.Firebase.mergeWith = function(data, name) {
87+
var id = this._current.id;
88+
8789
if (name != this._current.name) {
8890
this._current.name = name;
89-
this.ref.child("names/" + this._current.id).set(name);
91+
this.ref.child("names/" + id).set(name);
9092
}
9193

92-
var dataRef = this.ref.child("data/" + this._current.id);
93-
this._recursiveRefMerge(dataRef, this._current.data, data);
94-
this._current.data = data;
94+
95+
var dataRef = this.ref.child("data/" + id);
96+
var oldData = this._current.data;
97+
98+
this._listenStop();
99+
this._recursiveRefMerge(dataRef, oldData, data);
100+
this._listenStart(data, id);
95101
}
96102

97103
/**
@@ -169,12 +175,15 @@ MM.Backend.Firebase._listenStop = function() {
169175

170176

171177
/**
172-
* Monitored remote ref changed
173-
* FIXME use timeout to buffer changes?
178+
* Monitored remote ref changed.
179+
* FIXME move timeout logic to ui.backend.firebase?
174180
*/
175181
MM.Backend.Firebase._valueChange = function(snap) {
176182
this._current.data = snap.val();
177-
MM.publish("firebase-change", this, this._current.data);
183+
if (this._changeTimeout) { clearTimeout(this._changeTimeout); }
184+
this._changeTimeout = setTimeout(function() {
185+
MM.publish("firebase-change", this, this._current.data);
186+
}.bind(this), 200);
178187
}
179188

180189
MM.Backend.Firebase._login = function(type) {

src/item.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,34 @@ MM.Item.prototype.fromJSON = function(data) {
106106
}
107107

108108
MM.Item.prototype.mergeWith = function(data) {
109-
var dirty = false;
109+
var dirty = 0;
110110
if (this.getText() != data.text) { this.setText(data.text); }
111111

112112
if (this._side != data.side) {
113113
this._side = data.side;
114-
dirty = true;
114+
dirty = 1;
115115
}
116116

117117
if (this._color != data.color) {
118118
this._color = data.color;
119-
dirty = true;
119+
dirty = 2;
120120
}
121121

122122
if (this._value != data.value) {
123123
this._value = data.value;
124-
dirty = true;
124+
dirty = 1;
125125
}
126126

127127
if (this._status != data.status) {
128128
this._status = data.status;
129-
dirty = true;
129+
dirty = 1;
130130
}
131131

132132
if (this._collapsed != !!data.collapsed) { this[this._collapsed ? "expand" : "collapse"](); }
133133

134134
if (this.getOwnLayout() != data.layout) {
135135
this._layout = MM.Layout.getById(data.layout);
136-
dirty = true;
136+
dirty = 2;
137137
}
138138

139139
var s = (this._autoShape ? null : this._shape.id);
@@ -142,24 +142,24 @@ MM.Item.prototype.mergeWith = function(data) {
142142
/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
143143
(data.children || []).forEach(function(child, index) {
144144
if (index >= this._children.length) { /* new child */
145-
console.log("adding new child", child, "at", index);
146145
this.insertChild(MM.Item.fromJSON(child));
147-
// dirty = true; FIXME to zaridi to dite, ze?
148146
} else { /* existing child */
149147
var myChild = this._children[index];
150148
if (myChild.getId() == child.id) { /* recursive merge */
151-
console.log("merging child", myChild, "with", child);
152149
myChild.mergeWith(child);
153150
} else { /* changed; replace */
154-
console.log("replacing dead child", myChild, "with new", child);
155151
this.removeChild(this._children[index]);
156152
this.insertChild(MM.Item.fromJSON(child), index);
157-
// dirty = true; FIXME to zaridi to dite, ze?
158153
}
159154
}
160155
}, this);
161156

162-
if (dirty) { this.update(); }
157+
/* remove dead children */
158+
var newLength = (data.children || []).length;
159+
while (this._children.length > newLength) { this.removeChild(this._children[this._children.length-1]); }
160+
161+
if (dirty == 1) { this.update(); }
162+
if (dirty == 2) { this.updateSubtree(); }
163163
}
164164

165165
MM.Item.prototype.clone = function() {

src/ui.backend.firebase.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,24 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
7676

7777
case "firebase-change":
7878
if (data) {
79+
MM.unsubscribe("item-change", this);
7980
MM.App.map.mergeWith(data);
81+
MM.subscribe("item-change", this);
8082
} else { /* FIXME */
8183
console.log("remote data disappeared");
8284
}
8385
break;
8486

8587
case "item-change":
8688
if (this._itemChangeTimeout) { clearTimeout(this._itemChangeTimeout); }
87-
this._itemChangeTimeout = setTimeout(this._itemChange.bind(this), 300);
89+
this._itemChangeTimeout = setTimeout(this._itemChange.bind(this), 200);
8890
break;
8991
}
9092
}
9193

9294
MM.UI.Backend.Firebase.reset = function() {
93-
MM.unsubscribe("item-change", this);
9495
this._backend.reset();
96+
MM.unsubscribe("item-change", this);
9597
}
9698

9799
MM.UI.Backend.Firebase._itemChange = function() {

0 commit comments

Comments
 (0)