From ddf72b68d849fc86556feccb4ad1de6996bae9ae Mon Sep 17 00:00:00 2001 From: Prev Wong Date: Fri, 21 Jun 2024 16:54:54 +0800 Subject: [PATCH] fix: each env cache --- packages/core/src/each.ts | 2 +- packages/core/src/evaluator.ts | 33 ++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/core/src/each.ts b/packages/core/src/each.ts index 40dcbecd..be8a2e67 100644 --- a/packages/core/src/each.ts +++ b/packages/core/src/each.ts @@ -30,7 +30,7 @@ export class EachDirectiveEvaluator { ); this.eachExpr = template.each; - this.id = template.each.iterator.id; + this.id = template.each.id; this.items = new Map(); } diff --git a/packages/core/src/evaluator.ts b/packages/core/src/evaluator.ts index 4260ded6..fdf22a35 100644 --- a/packages/core/src/evaluator.ts +++ b/packages/core/src/evaluator.ts @@ -284,6 +284,8 @@ export class Evaluator { const prevEachExpr = untracked(() => template.each); + const env = ctx.env.inherit(); + const computation = computed( () => { /** @@ -300,7 +302,7 @@ export class Evaluator { }); if (template.if) { - const bool = this.computeExpr(template.if, ctx.env); + const bool = this.computeExpr(template.if, env); if (!bool) { return []; @@ -331,12 +333,12 @@ export class Evaluator { [] as string[] ); - ctx.env.set(ClassListBindingKey, { + env.set(ClassListBindingKey, { readonly: true, value: classListValue, }); } else { - ctx.env.set(ClassListBindingKey, { + env.set(ClassListBindingKey, { readonly: true, value: {}, }); @@ -354,19 +356,31 @@ export class Evaluator { let viewSet: t.View[] = []; if (template instanceof t.TagTemplate) { - viewSet = this.computeTagTemplate(template, ctx); + viewSet = this.computeTagTemplate(template, { + ...ctx, + env, + }); } if (template instanceof t.ComponentTemplate) { - viewSet = this.computeComponentTemplate(template, ctx); + viewSet = this.computeComponentTemplate(template, { + ...ctx, + env, + }); } if (template instanceof t.SlotTemplate) { - viewSet = this.computeSlotTemplate(template, ctx); + viewSet = this.computeSlotTemplate(template, { + ...ctx, + env, + }); } if (template instanceof t.FragmentTemplate) { - viewSet = this.computeFragmentTemplate(template, ctx); + viewSet = this.computeFragmentTemplate(template, { + ...ctx, + env, + }); } const cachedViewSet = viewSet.map((view) => { @@ -401,10 +415,7 @@ export class Evaluator { if (!template.each) { this.tplToEachComputationCache.delete(template); - return renderTemplate(template, { - ...ctx, - env: ctx.env.inherit(), - }); + return renderTemplate(template, ctx); } this.tplKeyToViewComputationCache.delete(baseKey);