@webtides/element-js 1.0.0-alpha.6 → 1.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtides/element-js",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.7",
4
4
  "module": "index.js",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -78,6 +78,9 @@ const processAttribute = (node, name) => {
78
78
  };
79
79
  };
80
80
 
81
+ /** @type {Map<Element, AttributePart>} */
82
+ const attributePartsCache = new WeakMap();
83
+
81
84
  /**
82
85
  * @param {Element} node
83
86
  * @param {String} name
@@ -114,24 +117,27 @@ export class AttributePart extends Part {
114
117
  initialValue = undefined;
115
118
 
116
119
  /**
117
- * @param {Node} node
120
+ * @param {Comment} node
118
121
  * @param {String} name
119
122
  * @param {String} initialValue
120
123
  */
121
124
  constructor(node, name, initialValue) {
125
+ // If we have multiple attribute parts with the same name, it means we have multiple
126
+ // interpolations inside that attribute. Instead of creating a new part, we will return the same
127
+ // as before and let it defer the update until the last interpolation gets updated
128
+ const attributePart = attributePartsCache.get(node.nextElementSibling);
129
+ if (attributePart && attributePart.name === name) {
130
+ attributePart.interpolations++;
131
+ node.__part = attributePart; // add Part to comment node for debugging in the browser
132
+ return attributePart;
133
+ }
134
+
122
135
  super();
123
- node.__part = this; // add Part to comment node for debugging in the browser
124
136
  this.name = name;
125
137
  this.initialValue = initialValue;
126
138
  this.processor = processAttributePart(node.nextElementSibling, this.name);
127
- }
128
-
129
- /**
130
- * @param {Node} node
131
- */
132
- addNode(node) {
133
- this.interpolations++;
134
- // TODO: what about the comment node? It will always be the first for now...
139
+ node.__part = this; // add Part to comment node for debugging in the browser
140
+ attributePartsCache.set(node.nextElementSibling, this);
135
141
  }
136
142
 
137
143
  /**
@@ -101,8 +101,6 @@ export class TemplatePart extends Part {
101
101
  partsCache.set(templateResult.strings, parts);
102
102
  }
103
103
 
104
- /** @type AttributePart */
105
- let previousAttributePart = undefined;
106
104
  this.parts = parts
107
105
  .map((part) => {
108
106
  // We currently need the path because the fragment will be cloned via importNode and therefore the node will be a different one
@@ -114,19 +112,11 @@ export class TemplatePart extends Part {
114
112
  return new ChildNodePart(part.node, templateResult.values[index]);
115
113
  }
116
114
  if (part.type === 'attribute') {
117
- // If we have multiple attribute parts with the same name, it means we have multiple
118
- // interpolations inside that attribute. Instead of creating a new part, we will return the same
119
- // as before and let it defer the update until the last interpolation gets updated
120
- if (previousAttributePart && previousAttributePart.name === part.name) {
121
- previousAttributePart.addNode(part.node);
122
- return previousAttributePart;
123
- }
124
- return (previousAttributePart = new AttributePart(part.node, part.name, part.initialValue));
115
+ return new AttributePart(part.node, part.name, part.initialValue);
125
116
  }
126
117
  if (part.type === 'directive') {
127
118
  return new NodePart(part.node, templateResult.values[index]);
128
119
  }
129
-
130
120
  throw `cannot map part: ${part}`;
131
121
  });
132
122
  this.strings = templateResult.strings;