From 00c4b8dfc623fc50e058b10e65572a60bd891aaa Mon Sep 17 00:00:00 2001 From: christophe-g Date: Wed, 21 Dec 2016 14:26:34 +0100 Subject: [PATCH 1/8] loop over value keys to check wich value need to be updated --- firebase-document.html | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/firebase-document.html b/firebase-document.html index 285258b..6971e67 100644 --- a/firebase-document.html +++ b/firebase-document.html @@ -186,11 +186,22 @@ value = this.zeroValue; } - if (!this.new) { + if (!this.isNew) { this.async(function() { this.syncToMemory(function() { this._log('Updating data from Firebase value:', value); - this.set('data', value); + + // set the value if this.data does not exist or value is primitive or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) + if (!this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { + return this.set('data', value); + } + + // now, we loop over keys + for (var prop in value) { + if(value[prop] !== this.data[prop]) { + this.set(['data', prop], value[prop]); + } + } }); }); } From 70c88c79c81c612a5b52baad3899ef67189d072c Mon Sep 17 00:00:00 2001 From: christophe-g Date: Wed, 21 Dec 2016 15:54:35 +0100 Subject: [PATCH 2/8] modified check for first sync --- firebase-document.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firebase-document.html b/firebase-document.html index 6971e67..96978a2 100644 --- a/firebase-document.html +++ b/firebase-document.html @@ -39,6 +39,7 @@ ], attached: function() { + this.__firstSync = true; this.__refChanged(this.ref, this.ref); }, @@ -192,7 +193,8 @@ this._log('Updating data from Firebase value:', value); // set the value if this.data does not exist or value is primitive or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) - if (!this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { + if (this.__firstSync || !this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { + delete this.__firstSync; return this.set('data', value); } From d31cfe9910f172edee0899a74b50514b91773740 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Wed, 21 Dec 2016 22:15:36 +0100 Subject: [PATCH 3/8] this.__firstSync = false instead of delete this.__firstSync --- firebase-document.html | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/firebase-document.html b/firebase-document.html index 96978a2..79ae530 100644 --- a/firebase-document.html +++ b/firebase-document.html @@ -192,9 +192,13 @@ this.syncToMemory(function() { this._log('Updating data from Firebase value:', value); - // set the value if this.data does not exist or value is primitive or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) + // set the value if: + // it is the first time we run this + // or if this.data does not exist + // or value is primitive + // or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) if (this.__firstSync || !this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { - delete this.__firstSync; + this.__firstSync = false; return this.set('data', value); } From a78c8f80b3c8066ae15c3a3a40477c74301eb1a8 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Fri, 23 Dec 2016 17:46:36 +0100 Subject: [PATCH 4/8] changed __needSync to __needSetData; fixed issue when path is changed for firebase-document: we need to set the data in this case as well --- firebase-database-behavior.html | 1 + firebase-document.html | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index 4c7d05d..503929e 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -93,6 +93,7 @@ if (oldPath != null && !this.disabled && this.__pathReady(path)) { this.syncToMemory(function() { this.data = this.zeroValue; + this.__needSetData = true; }); } }, diff --git a/firebase-document.html b/firebase-document.html index 79ae530..e43bf3d 100644 --- a/firebase-document.html +++ b/firebase-document.html @@ -39,7 +39,7 @@ ], attached: function() { - this.__firstSync = true; + this.__needSetData = true; this.__refChanged(this.ref, this.ref); }, @@ -185,6 +185,7 @@ if (value == null) { value = this.zeroValue; + this.__needSetData = true; } if (!this.isNew) { @@ -193,12 +194,12 @@ this._log('Updating data from Firebase value:', value); // set the value if: - // it is the first time we run this + // it is the first time we run this (or the path has changed and we are back with zeroValue) // or if this.data does not exist // or value is primitive // or if firebase value obj contain less keys than this.data (https://github.com/Polymer/polymer/issues/2565) - if (this.__firstSync || !this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { - this.__firstSync = false; + if (this.__needSetData || !this.data || typeof value !== 'object' || ( Object.keys(value).length < Object.keys(this.data).length)) { + this.__needSetData = false; return this.set('data', value); } From c65ae6ba1c265a3740d30103a5a632c792c03d04 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Tue, 3 Jan 2017 12:04:05 +0100 Subject: [PATCH 5/8] when the path changes, we should not set data to zeroValue only if not null. The old behavior caused data to be instantiated twice to zeroValue when path changed from null -> /userData//profile -> /userData/uid/profile --- firebase-database-behavior.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index 503929e..838a51f 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -90,7 +90,7 @@ }, __pathChanged: function(path, oldPath) { - if (oldPath != null && !this.disabled && this.__pathReady(path)) { + if (oldPath != null && this.__pathReady(oldPath) && !this.disabled && this.__pathReady(path)) { this.syncToMemory(function() { this.data = this.zeroValue; this.__needSetData = true; From c449d8bdc8c38104d58b0fa32e40571e3ceeca8e Mon Sep 17 00:00:00 2001 From: christophe-g Date: Tue, 3 Jan 2017 13:23:25 +0100 Subject: [PATCH 6/8] when the path changes, we should not set data to zeroValue only if not null. The old behavior caused data to be instantiated twice to zeroValue when path changed from null -> /userData//profile -> /userData/uid/profile --- firebase-database-behavior.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index 838a51f..e36e3e9 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -90,7 +90,7 @@ }, __pathChanged: function(path, oldPath) { - if (oldPath != null && this.__pathReady(oldPath) && !this.disabled && this.__pathReady(path)) { + if (!this.disabled && this.__pathReady(path) && !this.valueIsEmpty()) { this.syncToMemory(function() { this.data = this.zeroValue; this.__needSetData = true; From 3c408bc3143cc043f036e2cdd0aa50ee095b5068 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Tue, 3 Jan 2017 13:29:41 +0100 Subject: [PATCH 7/8] reset the data to zeroValue only when data exists, whatever the validity of the path --- firebase-database-behavior.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index e36e3e9..40edc21 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -90,7 +90,7 @@ }, __pathChanged: function(path, oldPath) { - if (!this.disabled && this.__pathReady(path) && !this.valueIsEmpty()) { + if (!this.disabled && !this.valueIsEmpty()) { this.syncToMemory(function() { this.data = this.zeroValue; this.__needSetData = true; From e610da168038d7073dd519145b8b254590e98b97 Mon Sep 17 00:00:00 2001 From: christophe-g Date: Tue, 3 Jan 2017 13:42:34 +0100 Subject: [PATCH 8/8] fix value is always empty --- firebase-database-behavior.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-database-behavior.html b/firebase-database-behavior.html index 40edc21..127b8cd 100644 --- a/firebase-database-behavior.html +++ b/firebase-database-behavior.html @@ -90,7 +90,7 @@ }, __pathChanged: function(path, oldPath) { - if (!this.disabled && !this.valueIsEmpty()) { + if (!this.disabled && !this.valueIsEmpty(this.data)) { this.syncToMemory(function() { this.data = this.zeroValue; this.__needSetData = true;