@lwc/template-compiler 7.2.4 → 7.2.6

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/dist/index.js CHANGED
@@ -12839,19 +12839,6 @@ function hasIdAttribute(node) {
12839
12839
  }
12840
12840
  return false;
12841
12841
  }
12842
- function memorizeHandler(codeGen, componentHandler, handler) {
12843
- // #439 - The handler can only be memorized if it is bound to component instance
12844
- const id = getMemberExpressionRoot(componentHandler);
12845
- const shouldMemorizeHandler = !codeGen.isLocalIdentifier(id);
12846
- // Apply memorization if the handler is memorizable.
12847
- // $cmp.handlePress -> _m1 || ($ctx._m1 = b($cmp.handlePress))
12848
- if (shouldMemorizeHandler) {
12849
- const memorizedId = codeGen.getMemorizationId();
12850
- const memorization = assignmentExpression('=', memberExpression(identifier(TEMPLATE_PARAMS.CONTEXT), memorizedId), handler);
12851
- handler = logicalExpression('||', memorizedId, memorization);
12852
- }
12853
- return handler;
12854
- }
12855
12842
  function generateTemplateMetadata(codeGen) {
12856
12843
  const metadataExpressions = [];
12857
12844
  if (codeGen.slotNames.size) {
@@ -13511,7 +13498,7 @@ class CodeGen {
13511
13498
  this.usedApis = {};
13512
13499
  this.usedLwcApis = new Set();
13513
13500
  this.slotNames = new Set();
13514
- this.memorizedIds = [];
13501
+ this.memoizedIds = [];
13515
13502
  this.referencedComponents = new Set();
13516
13503
  this.staticExpressionMap = new WeakMap();
13517
13504
  this.root = root;
@@ -13645,23 +13632,59 @@ class CodeGen {
13645
13632
  genTabIndex(children) {
13646
13633
  return this._renderApiCall(RENDER_APIS.tabindex, children);
13647
13634
  }
13648
- getMemorizationId() {
13635
+ getMemoizationId() {
13649
13636
  const currentId = this.currentId++;
13650
- const memorizationId = identifier(`_m${currentId}`);
13651
- this.memorizedIds.push(memorizationId);
13652
- return memorizationId;
13637
+ const memoizationId = identifier(`_m${currentId}`);
13638
+ this.memoizedIds.push(memoizationId);
13639
+ return memoizationId;
13653
13640
  }
13654
13641
  genBooleanAttributeExpr(bindExpr) {
13655
13642
  return conditionalExpression(bindExpr, literal$1(''), literal$1(null));
13656
13643
  }
13657
13644
  genEventListeners(listeners) {
13658
- const listenerObj = Object.fromEntries(listeners.map((listener) => [listener.name, listener]));
13659
- const listenerObjectAST = objectToAST(listenerObj, (key) => {
13660
- const componentHandler = this.bindExpression(listenerObj[key].handler);
13661
- const handler = this.genBind(componentHandler);
13662
- return memorizeHandler(this, componentHandler, handler);
13663
- });
13664
- return property$1(identifier('on'), listenerObjectAST);
13645
+ let hasLocalListeners = false;
13646
+ const listenerObj = {};
13647
+ for (const { name, handler } of listeners) {
13648
+ const componentHandler = this.bindExpression(handler);
13649
+ const id = getMemberExpressionRoot(componentHandler);
13650
+ const isLocal = this.isLocalIdentifier(id);
13651
+ if (isLocal) {
13652
+ hasLocalListeners = true;
13653
+ }
13654
+ listenerObj[name] = { handler: this.genBind(componentHandler), isLocal };
13655
+ }
13656
+ // Individually memoize a non-local event handler
13657
+ // Example input: <template for:each={list} for:item="task">
13658
+ // <button [...] ontouchstart={foo}>[X]</button>
13659
+ // </template>
13660
+ // Output: [...] touchstart: _m2 || ($ctx._m2 = api_bind($cmp.foo))
13661
+ const memoize = (expr) => {
13662
+ const memoizedId = this.getMemoizationId();
13663
+ return logicalExpression('||', memoizedId, assignmentExpression('=', memberExpression(identifier(TEMPLATE_PARAMS.CONTEXT), memoizedId), expr));
13664
+ };
13665
+ if (hasLocalListeners) {
13666
+ // If there are local listeners, we need to memoize individual handlers
13667
+ // Input: <template for:each={list} for:item="task">
13668
+ // <button onclick={task.delete} ontouchstart={foo}>[X]</button>
13669
+ // </template>
13670
+ // Output:
13671
+ // on: {
13672
+ // click: api_bind(task.delete),
13673
+ // touchstart: _m2 || ($ctx._m2 = api_bind($cmp.foo))
13674
+ // }
13675
+ return property$1(identifier('on'), objectToAST(listenerObj, (k) => {
13676
+ const { isLocal, handler } = listenerObj[k];
13677
+ return isLocal ? handler : memoize(handler);
13678
+ }));
13679
+ }
13680
+ else {
13681
+ // If there are no local listeners, we can memoize the entire `on` object
13682
+ // Input: <template>
13683
+ // <button onclick={create}>New</button>
13684
+ // </template>
13685
+ // Output: on: _m1 || ($ctx._m1 = { click: api_bind($cmp.create) })
13686
+ return property$1(identifier('on'), memoize(objectToAST(listenerObj, (k) => listenerObj[k].handler)));
13687
+ }
13665
13688
  }
13666
13689
  genRef(ref) {
13667
13690
  this.hasRefs = true;
@@ -14615,9 +14638,9 @@ function generateTemplateFunction(codeGen) {
14615
14638
  variableDeclarator(objectPattern(usedApis.map((name) => assignmentProperty(identifier(name), codeGen.usedApis[name]))), identifier(TEMPLATE_PARAMS.API)),
14616
14639
  ]),
14617
14640
  ];
14618
- if (codeGen.memorizedIds.length) {
14641
+ if (codeGen.memoizedIds.length) {
14619
14642
  body.push(variableDeclaration('const', [
14620
- variableDeclarator(objectPattern(codeGen.memorizedIds.map((id) => assignmentProperty(id, id, { shorthand: true }))), identifier(TEMPLATE_PARAMS.CONTEXT)),
14643
+ variableDeclarator(objectPattern(codeGen.memoizedIds.map((id) => assignmentProperty(id, id, { shorthand: true }))), identifier(TEMPLATE_PARAMS.CONTEXT)),
14621
14644
  ]));
14622
14645
  }
14623
14646
  body.push(returnStatement(returnedValue));
@@ -14694,5 +14717,5 @@ function compile(source, filename, config) {
14694
14717
  }
14695
14718
 
14696
14719
  export { ElementDirectiveName, LWCDirectiveDomMode, LWCDirectiveRenderMode, LwcTagName, RootDirectiveName, TemplateDirectiveName, compile, compile as default, kebabcaseToCamelcase, parse, toPropertyName };
14697
- /** version: 7.2.4 */
14720
+ /** version: 7.2.6 */
14698
14721
  //# sourceMappingURL=index.js.map