@@ -395,34 +395,34 @@ MM.Item.prototype.fromJSON = function(data) {
395
395
}
396
396
397
397
MM . Item . prototype . mergeWith = function ( data ) {
398
- var dirty = false ;
398
+ var dirty = 0 ;
399
399
if ( this . getText ( ) != data . text ) { this . setText ( data . text ) ; }
400
400
401
401
if ( this . _side != data . side ) {
402
402
this . _side = data . side ;
403
- dirty = true ;
403
+ dirty = 1 ;
404
404
}
405
405
406
406
if ( this . _color != data . color ) {
407
407
this . _color = data . color ;
408
- dirty = true ;
408
+ dirty = 2 ;
409
409
}
410
410
411
411
if ( this . _value != data . value ) {
412
412
this . _value = data . value ;
413
- dirty = true ;
413
+ dirty = 1 ;
414
414
}
415
415
416
416
if ( this . _status != data . status ) {
417
417
this . _status = data . status ;
418
- dirty = true ;
418
+ dirty = 1 ;
419
419
}
420
420
421
421
if ( this . _collapsed != ! ! data . collapsed ) { this [ this . _collapsed ? "expand" : "collapse" ] ( ) ; }
422
422
423
423
if ( this . getOwnLayout ( ) != data . layout ) {
424
424
this . _layout = MM . Layout . getById ( data . layout ) ;
425
- dirty = true ;
425
+ dirty = 2 ;
426
426
}
427
427
428
428
var s = ( this . _autoShape ? null : this . _shape . id ) ;
@@ -431,24 +431,24 @@ MM.Item.prototype.mergeWith = function(data) {
431
431
/* FIXME children - co kdyz je nekdo z nas zrovna aktivni, nerkuli editovatelny? */
432
432
( data . children || [ ] ) . forEach ( function ( child , index ) {
433
433
if ( index >= this . _children . length ) { /* new child */
434
- console . log ( "adding new child" , child , "at" , index ) ;
435
434
this . insertChild ( MM . Item . fromJSON ( child ) ) ;
436
- // dirty = true; FIXME to zaridi to dite, ze?
437
435
} else { /* existing child */
438
436
var myChild = this . _children [ index ] ;
439
437
if ( myChild . getId ( ) == child . id ) { /* recursive merge */
440
- console . log ( "merging child" , myChild , "with" , child ) ;
441
438
myChild . mergeWith ( child ) ;
442
439
} else { /* changed; replace */
443
- console . log ( "replacing dead child" , myChild , "with new" , child ) ;
444
440
this . removeChild ( this . _children [ index ] ) ;
445
441
this . insertChild ( MM . Item . fromJSON ( child ) , index ) ;
446
- // dirty = true; FIXME to zaridi to dite, ze?
447
442
}
448
443
}
449
444
} , this ) ;
450
445
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 ( ) ; }
452
452
}
453
453
454
454
MM . Item . prototype . clone = function ( ) {
@@ -3154,14 +3154,20 @@ MM.Backend.Firebase.reset = function() {
3154
3154
* Merge current (remote) data with updated map
3155
3155
*/
3156
3156
MM . Backend . Firebase . mergeWith = function ( data , name ) {
3157
+ var id = this . _current . id ;
3158
+
3157
3159
if ( name != this . _current . name ) {
3158
3160
this . _current . name = name ;
3159
- this . ref . child ( "names/" + this . _current . id ) . set ( name ) ;
3161
+ this . ref . child ( "names/" + id ) . set ( name ) ;
3160
3162
}
3161
3163
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 ) ;
3165
3171
}
3166
3172
3167
3173
/**
@@ -3239,12 +3245,15 @@ MM.Backend.Firebase._listenStop = function() {
3239
3245
3240
3246
3241
3247
/**
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 ?
3244
3250
*/
3245
3251
MM . Backend . Firebase . _valueChange = function ( snap ) {
3246
3252
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 ) ;
3248
3257
}
3249
3258
3250
3259
MM . Backend . Firebase . _login = function ( type ) {
@@ -4302,22 +4311,24 @@ MM.UI.Backend.Firebase.handleMessage = function(message, publisher, data) {
4302
4311
4303
4312
case "firebase-change" :
4304
4313
if ( data ) {
4314
+ MM . unsubscribe ( "item-change" , this ) ;
4305
4315
MM . App . map . mergeWith ( data ) ;
4316
+ MM . subscribe ( "item-change" , this ) ;
4306
4317
} else { /* FIXME */
4307
4318
console . log ( "remote data disappeared" ) ;
4308
4319
}
4309
4320
break ;
4310
4321
4311
4322
case "item-change" :
4312
4323
if ( this . _itemChangeTimeout ) { clearTimeout ( this . _itemChangeTimeout ) ; }
4313
- this . _itemChangeTimeout = setTimeout ( this . _itemChange . bind ( this ) , 300 ) ;
4324
+ this . _itemChangeTimeout = setTimeout ( this . _itemChange . bind ( this ) , 200 ) ;
4314
4325
break ;
4315
4326
}
4316
4327
}
4317
4328
4318
4329
MM . UI . Backend . Firebase . reset = function ( ) {
4319
- MM . unsubscribe ( "item-change" , this ) ;
4320
4330
this . _backend . reset ( ) ;
4331
+ MM . unsubscribe ( "item-change" , this ) ;
4321
4332
}
4322
4333
4323
4334
MM . UI . Backend . Firebase . _itemChange = function ( ) {
0 commit comments