Skip to content
Snippets Groups Projects
Unverified Commit be243cdd authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire:
Browse files

Re-implement "properties" and "lastProperties" handling (dbp/apps/signature#32)

parent a8c527e6
No related branches found
No related tags found
Loading
Pipeline #16045 passed
...@@ -4,30 +4,32 @@ export class Provider extends HTMLElement { ...@@ -4,30 +4,32 @@ export class Provider extends HTMLElement {
this.callbackStore = []; this.callbackStore = [];
this.root = false; this.root = false;
// Previously we used direct properties like this["lang"] (instead of this.properties["lang"]) for storing the
// properties, but the "lang" property seems to be updated before the event from the MutationObserver, so we
// cannot observe a value change directly (as workaround we use another property (e.g. "langValue") instead of "lang")
this.properties = {};
// We need to store our own "last values" because we cannot be sure what the MutationObserver detects
this.lastProperties = {};
console.log('Provider constructor()'); console.log('Provider constructor()');
} }
/** getProperty(name) {
* The "lang" property seems to be updated before the event from the MutationObserver, so we cannot observe a value change directly return this.properties[name];
* Workaround: use another property (e.g. "langValue") instead of "lang"
*
* @param name
* @returns {string|*}
*/
static getPropertyName(name) {
return name === 'lang' ? 'langValue' : name;
} }
getValue(name) { setProperty(name, value) {
return this[Provider.getPropertyName(name)]; this.lastProperties[name] = value;
this.properties[name] = value;
} }
setValue(name, value) { hasPropertyChanged(name, value) {
this[Provider.getPropertyName(name)] = value; return this.lastProperties[name] !== value;
} }
hasProperty(name) { hasProperty(name) {
return Object.hasOwnProperty.call(this, Provider.getPropertyName(name)); return Object.hasOwnProperty.call(this.properties, name);
} }
connectedCallback() { connectedCallback() {
...@@ -41,7 +43,7 @@ export class Provider extends HTMLElement { ...@@ -41,7 +43,7 @@ export class Provider extends HTMLElement {
console.log('Provider(' + that.id + ') eventListener("subscribe",..) name "' + name + '" found.'); console.log('Provider(' + that.id + ') eventListener("subscribe",..) name "' + name + '" found.');
that.callbackStore.push({name: name, callback: e.detail.callback, sender: e.detail.sender}); that.callbackStore.push({name: name, callback: e.detail.callback, sender: e.detail.sender});
e.detail.callback(that.getValue(name)); e.detail.callback(that.getProperty(name));
e.stopPropagation(); e.stopPropagation();
} }
}, false); }, false);
...@@ -70,7 +72,7 @@ export class Provider extends HTMLElement { ...@@ -70,7 +72,7 @@ export class Provider extends HTMLElement {
if (that.hasProperty(name) || that.root) { if (that.hasProperty(name) || that.root) {
console.log('Provider(' + that.id + ') eventListener("set-property",..) name "' + name + '" found.'); console.log('Provider(' + that.id + ') eventListener("set-property",..) name "' + name + '" found.');
that.setValue(name, value); that.setProperty(name, value);
that.callbackStore.forEach(item => { that.callbackStore.forEach(item => {
if (item.name === name) { if (item.name === name) {
...@@ -93,9 +95,9 @@ export class Provider extends HTMLElement { ...@@ -93,9 +95,9 @@ export class Provider extends HTMLElement {
const name = mutation.attributeName; const name = mutation.attributeName;
const value = that.getAttribute(name); const value = that.getAttribute(name);
if (that.getValue(name) !== value) { if (that.hasPropertyChanged(name, value)) {
console.log('Provider (' + that.id + ') observed attribute "' + name + '" changed'); console.log('Provider (' + that.id + ') observed attribute "' + name + '" changed');
that.setValue(name, value); that.setProperty(name, value);
that.callbackStore.forEach(item => { that.callbackStore.forEach(item => {
if (item.name === name) { if (item.name === name) {
...@@ -121,7 +123,7 @@ export class Provider extends HTMLElement { ...@@ -121,7 +123,7 @@ export class Provider extends HTMLElement {
continue; continue;
} }
this[attrs[i].name] = attrs[i].value; this.setProperty(attrs[i].name, attrs[i].value);
console.log('Provider (' + that.id + ') found attribute "' + attrs[i].name + '" = "' + attrs[i].value + '"'); console.log('Provider (' + that.id + ') found attribute "' + attrs[i].name + '" = "' + attrs[i].value + '"');
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment