Skip to content

Commit

Permalink
fix: each env cache
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong committed Jul 3, 2024
1 parent 43a7ad2 commit ddf72b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
33 changes: 22 additions & 11 deletions packages/core/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ export class Evaluator {

const prevEachExpr = untracked(() => template.each);

const env = ctx.env.inherit();

const computation = computed(
() => {
/**
Expand All @@ -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 [];
Expand Down Expand Up @@ -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: {},
});
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ddf72b6

Please sign in to comment.