Skip to content

Commit

Permalink
only loop once over styles (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelhan committed Jan 13, 2023
1 parent 1ef844e commit 62bd316
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions addon/modifiers/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ function isObject(o) {
}

export default class StyleModifier extends Modifier {
existingStyles = new Set();

/**
* Returns a two-dimensional array, like:
*
Expand All @@ -31,7 +33,11 @@ export default class StyleModifier extends Modifier {
}

setStyles(element, newStyles) {
const rulesToRemove = this._oldStyles || new Set();
const { existingStyles } = this;
const rulesToRemove = new Set(existingStyles);

// clear cache of existing styles
existingStyles.clear();

newStyles.forEach(([property, value]) => {
assert(
Expand All @@ -58,13 +64,13 @@ export default class StyleModifier extends Modifier {

// should not remove rules that have been updated in this cycle
rulesToRemove.delete(property);

// cache styles that have been set for potential clean-up when argument changes
existingStyles.add(property);
});

// remove rules that were present in last cycle but aren't present in this one
rulesToRemove.forEach((rule) => element.style.removeProperty(rule));

// cache styles that in this rendering cycle for the next one
this._oldStyles = new Set(newStyles.map((e) => e[0]));
}

modify(element, positional, named) {
Expand Down

0 comments on commit 62bd316

Please sign in to comment.