Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate attribute value changes logic to a two levels Map. #1763

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 73 additions & 34 deletions ui/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2904,14 +2904,42 @@ var Component = exports.Component = Target.specialize(/** @lends Component.proto
* Stores values that need to be set on the element. Cleared each draw cycle.
* @private
*/
__elementAttributeValues: {
value: null
},
_elementAttributeValues: {
get: function() {
return this.__elementAttributeValues || (this.__elementAttributeValues = {});
}
},
__bufferedElementsAttributeValues: {
value: null
},

_bufferedElementsAttributeValues: {
get: function () {
return this.__bufferedElementsAttributeValues || (this.__bufferedElementsAttributeValues = new Map());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be WeakMap as we don't want to hold on "gone" elements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A weak map can't be iterated.

}
},

_setElementAttributeValue: {
value: function (element, attribute, value) {
var bufferedElementsAttributeValues = this._bufferedElementsAttributeValues,
elementAttributeValues;

if (!(elementAttributeValues = bufferedElementsAttributeValues.get(element))) {
bufferedElementsAttributeValues.set(element, (elementAttributeValues = new Map()));
}

elementAttributeValues.set(attribute, value);
}
},

_getElementAttributeValue: {
value: function (element, attribute) {
var bufferedElementsAttributeValues = this._bufferedElementsAttributeValues,
elementAttributeValues,
value;

if ((elementAttributeValues = bufferedElementsAttributeValues.get(element))) {
value = elementAttributeValues.get(attribute);
}

return value;
}
},

/**
* Stores the descriptors of the properties that can be set on this control
Expand Down Expand Up @@ -3230,11 +3258,12 @@ var Component = exports.Component = Target.specialize(/** @lends Component.proto

descriptor = this._getElementAttributeDescriptor(name, this);
// check if this attribute from the markup is a well-defined attribute of the component
if(descriptor || (typeof this[name] !== 'undefined')) {
if (descriptor || (typeof this[name] !== 'undefined')) {
// only set the value if a value has not already been set by binding
if(typeof this._elementAttributeValues[name] === 'undefined') {
this._elementAttributeValues[name] = value;
if( (typeof this[name] === 'undefined') || this[name] == null) {
if (typeof this._getElementAttributeValue(this.element, name) === 'undefined') {
this._setElementAttributeValue(this.element, name, value);

if ((typeof this[name] === 'undefined') || this[name] === null) {
this[name] = value;
}
}
Expand All @@ -3244,14 +3273,15 @@ var Component = exports.Component = Target.specialize(/** @lends Component.proto

// textContent is a special case since it isn't an attribute
descriptor = this._getElementAttributeDescriptor('textContent', this);
if(descriptor) {

if (descriptor) {
// check if this element has textContent
var textContent = originalElement.textContent;

if (typeof this._getElementAttributeValue(this.element, "textContent") === 'undefined') {
this._setElementAttributeValue(this.element, "textContent", textContent);

if(typeof this._elementAttributeValues.textContent === 'undefined') {
this._elementAttributeValues.textContent = textContent;
if( this.textContent == null) {
if (this.textContent === null) {
this.textContent = textContent;
}
}
Expand Down Expand Up @@ -3279,41 +3309,49 @@ var Component = exports.Component = Target.specialize(/** @lends Component.proto
*/
_draw: {
value: function () {
var element = this.element,
descriptor;

//Buffered/deferred element attribute values
if(this.__elementAttributeValues !== null) {
for(var attributeName in this._elementAttributeValues) {
if(this._elementAttributeValues.hasOwnProperty(attributeName)) {
var value = this[attributeName];
if (this.__bufferedElementsAttributeValues !== null) {
var bufferedElementsAttributeValuesMap = this.__bufferedElementsAttributeValues,
bufferedElementsAttributeValuesIterator = bufferedElementsAttributeValuesMap.keys(),
bufferedElementAttributeValues,
elementAttributeValuesIterator,
elementAttributeValuesMap,
element = this.element,
attributeName,
descriptor,
value;

while (bufferedElementAttributeValues = bufferedElementsAttributeValuesIterator.next().value) {
elementAttributeValuesMap = bufferedElementsAttributeValuesMap.get(bufferedElementAttributeValues);
elementAttributeValuesIterator = elementAttributeValuesMap.keys();

while (attributeName = elementAttributeValuesIterator.next().value) {
value = elementAttributeValuesMap.get(attributeName);
descriptor = this._getElementAttributeDescriptor(attributeName, this);
if(descriptor) {

if(descriptor.dataType === 'boolean') {
if(value === true) {
if (descriptor) {
if (descriptor.dataType === 'boolean') {
if (value === true) {
element[attributeName] = true;
element.setAttribute(attributeName, attributeName.toLowerCase());
} else {
element[attributeName] = false;
element.removeAttribute(attributeName);
}
} else {
if(typeof value !== 'undefined') {
if(attributeName === 'textContent') {
if (typeof value !== 'undefined') {
if (attributeName === 'textContent') {
element.textContent = value;
} else {
//https://developer.mozilla.org/en/DOM/element.setAttribute
element.setAttribute(attributeName, value);
}

}
}

}

delete this._elementAttributeValues[attributeName];
}

bufferedElementsAttributeValuesMap.delete(this.element);
}
}

Expand Down Expand Up @@ -3493,9 +3531,10 @@ var Component = exports.Component = Target.specialize(/** @lends Component.proto
// If the set value is different to the current one,
// update it here, and set it to be updated on the
// element in the next draw cycle.
if((typeof value !== 'undefined') && this[attributeName] !== value) {
if ((typeof value !== 'undefined') && this[attributeName] !== value) {
setter ? setter.call(this,value) : (this[attributeName] = value);
this._elementAttributeValues[name] = value;
this._setElementAttributeValue(this.element, name, value);

if (!fromInput) {
this.needsDraw = true;
}
Expand Down
3 changes: 1 addition & 2 deletions ui/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ var Control = exports.Control = Component.specialize(/** @lends module:montage/u
}

this.callDelegateMethod("didChange", this);
this._setElementAttributeValue(this.element, "value", value);

this._elementAttributeValues["value"] = value;
// if(!this.hasStandardElement || this.elementValue !== value) {
this.needsDraw = true;
//}
Expand Down Expand Up @@ -344,7 +344,6 @@ var Control = exports.Control = Component.specialize(/** @lends module:montage/u
// this._valueSyncedWithElement = true;
// } else {
// this._valueSyncedWithElement = false;
// this._elementAttributeValues[name] = value;
// this.needsDraw = true;
// }
// }
Expand Down