Skip to content

Commit

Permalink
fix: cache root template evaluation (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Nov 20, 2023
1 parent cfefb6f commit e76ffc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-birds-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rekajs/core': patch
---

Cache root template in component evaluation
34 changes: 28 additions & 6 deletions packages/core/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from 'mobx';

import { ComponentViewEvaluator } from './component';
import { DisposableComputation } from './computation';
import { EachDirectiveEvaluator } from './each';
import { Environment } from './environment';
import { computeExpression } from './expression';
Expand Down Expand Up @@ -42,6 +43,7 @@ export class Evaluator {
private viewObserver: Observer | undefined;
private rootTemplate: t.ComponentTemplate;
private rootTemplateObserver: Observer;
private declare rootTemplateComputation: DisposableComputation<t.View[]>;

private tplToView: WeakMap<t.Template, t.View[]> = new WeakMap();
private tplKeyToClassListComputationCache: Map<string, IComputedValue<void>> =
Expand Down Expand Up @@ -508,18 +510,34 @@ export class Evaluator {
return computeExpression(expr, this.reka, env);
}

computeRootTemplate() {
if (this.rootTemplateComputation) {
return this.rootTemplateComputation.get();
}

this.rootTemplateComputation = new DisposableComputation(
() =>
this.computeTemplate(this.rootTemplate, {
path: ['frame'],
env: this.reka.head.env,
owner: null,
componentStack: [],
}),
{
keepAlive: true,
}
);

return this.rootTemplateComputation.get();
}

computeView() {
if (!this.reka.loaded) {
return;
}

const _compute = () => {
const views = this.computeTemplate(this.rootTemplate, {
path: ['frame'],
env: this.reka.head.env,
owner: null,
componentStack: [],
});
const views = this.computeRootTemplate();

return t.assert(views[0], t.RekaComponentView);
};
Expand All @@ -543,6 +561,10 @@ export class Evaluator {
this.viewObserver.dispose();
}

if (this.rootTemplateComputation) {
this.rootTemplateComputation.dispose();
}

this.rootTemplateObserver.dispose();
this.disposeComponentEvaluators();

Expand Down

1 comment on commit e76ffc3

@vercel
Copy link

@vercel vercel bot commented on e76ffc3 Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

reka – ./

reka-prevwong.vercel.app
reka-git-main-prevwong.vercel.app
rekajs.vercel.app
reka.js.org

Please sign in to comment.